writing_classes_practice
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
writing_classes_practice [2022/10/01 12:37] – 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 24: | Line 123: | ||
===== Song ===== | ===== Song ===== | ||
+ | the SongTester class that will test your '' | ||
<code java SongTester.java> | <code java SongTester.java> | ||
Line 34: | Line 134: | ||
* @param args | * @param args | ||
*/ | */ | ||
- | public static void main(String[] args) { | + | public static void main(String[] args) |
- | Song song=new Song(); | + | |
- | Scanner keyboard=new Scanner(System.in); | + | Song song=new Song(); |
- | String input=""; | + | Scanner keyboard=new Scanner(System.in); |
- | while (input.indexOf(" | + | String input=""; |
- | { | + | while (input.indexOf(" |
- | processCommand(keyboard, | + | { |
- | System.out.println(" | + | processCommand(keyboard, |
- | System.out.print(" | + | System.out.println(" |
- | input=keyboard.nextLine(); | + | System.out.print(" |
- | input=input.toLowerCase(); | + | input=keyboard.nextLine(); |
+ | input=input.toLowerCase(); | ||
- | } | + | } |
- | System.out.println(" | + | System.out.println(" |
} | } | ||
public static void processCommand(Scanner keyboard, Song song, String input){ | public static void processCommand(Scanner keyboard, Song song, String input){ | ||
- | if (input.equals(" | + | if (input.equals(" |
- | { | + | { |
- | System.out.print(" | + | System.out.print(" |
- | song.setTitle(keyboard.nextLine()); | + | song.setTitle(keyboard.nextLine()); |
- | } | + | } else if (input.equals(" |
- | else if (input.equals(" | + | { |
- | { | + | System.out.print(" |
- | System.out.print(" | + | song.setArtist(keyboard.nextLine()); |
- | song.setArtist(keyboard.nextLine()); | + | } else if (input.equals(" |
- | } | + | { |
- | else if (input.equals(" | + | System.out.print(" |
- | { | + | int newRank=Integer.parseInt(keyboard.nextLine()); |
- | System.out.print(" | + | song.setRank(newRank); |
- | int newRank=Integer.parseInt(keyboard.nextLine()); | + | } else if (input.equals(" |
- | song.setRank(newRank); | + | { |
- | } | + | System.out.print(" |
- | else if (input.equals(" | + | double newTime=Double.parseDouble(keyboard.nextLine()); |
- | { | + | song.setTime(newTime); |
- | System.out.print(" | + | } else if (input.equals(" |
- | double newTime=Double.parseDouble(keyboard.nextLine()); | + | { |
- | song.setTime(newTime); | + | System.out.print(" |
- | } | + | int newPlays=Integer.parseInt(keyboard.nextLine()); |
- | else if (input.equals(" | + | song.setPlayCount(newPlays); |
- | { | + | |
- | System.out.print(" | + | |
- | int newPlays=Integer.parseInt(keyboard.nextLine()); | + | |
- | song.setPlayCount(newPlays); | + | |
- | } | + | |
} | } |
writing_classes_practice.1664642257.txt.gz · Last modified: 2022/10/01 12:37 by frchris