writing_classes_practice
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
writing_classes_practice [2022/10/01 12:35] – created frchris | writing_classes_practice [2022/10/01 14:45] (current) – [Writing Classes Practice] frchris | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Writing Classes Practice ====== | ====== Writing Classes Practice ====== | ||
+ | |||
+ | These are designed to be done with an actual computer. | ||
+ | |||
+ | ===== Clock ===== | ||
+ | |||
+ | - The Clock class should have three fields: hours minutes and seconds | ||
+ | - There should be a constructor that takes three values and initializes the fields | ||
+ | - There should be a '' | ||
+ | - There should be a method called '' | ||
+ | |||
+ | <code java ClockTester.java> | ||
+ | import java.awt.Color; | ||
+ | import java.awt.Font; | ||
+ | import java.awt.Graphics; | ||
+ | import java.awt.event.ActionEvent; | ||
+ | import java.awt.event.ActionListener; | ||
+ | import java.awt.event.KeyEvent; | ||
+ | import java.awt.event.KeyListener; | ||
+ | import javax.swing.JFrame; | ||
+ | import javax.swing.JPanel; | ||
+ | import javax.swing.Timer; | ||
+ | |||
+ | public class ClockTester extends JPanel implements | ||
+ | { | ||
+ | public static int WIDTH=800; | ||
+ | public static int HEIGHT=600; | ||
+ | private Font titleFont, regularFont, | ||
+ | private int x; | ||
+ | private Timer motion, pulse; | ||
+ | private Clock clock; | ||
+ | |||
+ | public ClockTester() | ||
+ | { | ||
+ | |||
+ | // | ||
+ | titleFont = new Font(" | ||
+ | clockFont = new Font(" | ||
+ | regularFont = new Font(" | ||
+ | x=0; | ||
+ | motion = new Timer(2, this); //1000=1 seconds | ||
+ | pulse = new Timer(1000, this); //1000=1 seconds | ||
+ | clock =new Clock(11, 59, 50); | ||
+ | |||
+ | motion.start(); | ||
+ | pulse.start(); | ||
+ | |||
+ | } | ||
+ | public static void main(String[] args) { | ||
+ | ClockTester app= new ClockTester(); | ||
+ | JFrame window = new JFrame(" | ||
+ | window.setSize(WIDTH, | ||
+ | window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
+ | window.getContentPane().add(app); | ||
+ | window.setVisible(true); | ||
+ | |||
+ | } | ||
+ | public void paintComponent(Graphics g){ | ||
+ | super.paintComponent(g); | ||
+ | g.setColor(Color.WHITE); | ||
+ | g.fillRect(0, | ||
+ | g.setColor(Color.BLUE); | ||
+ | g.setFont(titleFont); | ||
+ | g.drawString(" | ||
+ | g.setColor(Color.BLACK); | ||
+ | g.setFont(regularFont); | ||
+ | // | ||
+ | g.fillOval(x, | ||
+ | g.setColor(Color.RED); | ||
+ | g.setFont(clockFont); | ||
+ | g.drawString(clock.toString(), | ||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | public void actionPerformed(ActionEvent e) { | ||
+ | |||
+ | if (e.getSource()==motion){ | ||
+ | x=(x+1)%WIDTH; | ||
+ | |||
+ | } else { | ||
+ | // call the tick method | ||
+ | clock.tick(); | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | |||
===== Cash Register ===== | ===== Cash Register ===== | ||
Use the following tester class to figure out how to make your own '' | Use the following tester class to figure out how to make your own '' | ||
+ | |||
+ | Hints: | ||
+ | |||
+ | - You probably need to keep track of the total of the purchase, and how much the customer has already paid, | ||
+ | - You need a constructor to initialize the fields. | ||
+ | - You need a '' | ||
+ | - You need an '' | ||
+ | - You need a method '' | ||
+ | |||
<code java CashRegisterTest.java> | <code java CashRegisterTest.java> | ||
/** | /** | ||
Line 9: | Line 108: | ||
*/ | */ | ||
public class CashRegisterTester | public class CashRegisterTester | ||
- | { | + | { has paid |
| | ||
{ | { | ||
Line 21: | Line 120: | ||
} | } | ||
} | } | ||
+ | </ | ||
+ | |||
+ | ===== Song ===== | ||
+ | the SongTester class that will test your '' | ||
+ | |||
+ | <code java SongTester.java> | ||
+ | import java.util.Scanner; | ||
+ | |||
+ | |||
+ | public class SongTester { | ||
+ | |||
+ | /** | ||
+ | * @param args | ||
+ | */ | ||
+ | public static void main(String[] args) | ||
+ | { | ||
+ | Song song=new Song(); | ||
+ | | ||
+ | | ||
+ | while (input.indexOf(" | ||
+ | { | ||
+ | processCommand(keyboard, | ||
+ | System.out.println(" | ||
+ | System.out.print(" | ||
+ | input=keyboard.nextLine(); | ||
+ | input=input.toLowerCase(); | ||
+ | |||
+ | } | ||
+ | | ||
+ | |||
+ | |||
+ | } | ||
+ | public static void processCommand(Scanner keyboard, Song song, String input){ | ||
+ | if (input.equals(" | ||
+ | { | ||
+ | System.out.print(" | ||
+ | song.setTitle(keyboard.nextLine()); | ||
+ | } else if (input.equals(" | ||
+ | { | ||
+ | System.out.print(" | ||
+ | song.setArtist(keyboard.nextLine()); | ||
+ | } else if (input.equals(" | ||
+ | { | ||
+ | System.out.print(" | ||
+ | int newRank=Integer.parseInt(keyboard.nextLine()); | ||
+ | song.setRank(newRank); | ||
+ | } else if (input.equals(" | ||
+ | { | ||
+ | System.out.print(" | ||
+ | double newTime=Double.parseDouble(keyboard.nextLine()); | ||
+ | song.setTime(newTime); | ||
+ | } else if (input.equals(" | ||
+ | { | ||
+ | System.out.print(" | ||
+ | int newPlays=Integer.parseInt(keyboard.nextLine()); | ||
+ | song.setPlayCount(newPlays); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | } | ||
+ | |||
</ | </ |
writing_classes_practice.1664642119.txt.gz · Last modified: 2022/10/01 12:35 by frchris