User Tools

Site Tools


writing_classes_practice

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
writing_classes_practice [2022/10/01 12:46] frchriswriting_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.  You would need to write the class from scratch and use the Tester class to see how your work is progressing.  Usually when you write a class for a Quiz, Test, or Exam, the task will be simpler (since you have no computer).  You learn a lot by finding and fixing mistakes yourself on a computer, which is why this is such great practice.
 +
 +===== 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 ''toString'' method so that it returns a string with the current time.  (For example "12:52:34"
 +   - There should be a method called ''tick'' that advances the seconds by one and sees if the minutes and hours need to be changed.  For example, if the current time is 11:59:59, after the method is called, the time should become "12:00:00";
 +
 +<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  ActionListener
 +{
 + public static int WIDTH=800;
 + public static int HEIGHT=600;
 + private Font titleFont, regularFont, clockFont;
 + private int x;
 + private Timer motion, pulse;
 +    private Clock clock;
 +
 + public ClockTester() 
 + {
 +
 + //initialize variables here...
 + titleFont = new Font("Roman", Font.BOLD, 18);
 + clockFont = new Font("Roman", Font.BOLD, 72);
 + regularFont = new Font("Helvetica", Font.PLAIN, 12);
 + 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("Clock Tester");
 + window.setSize(WIDTH, HEIGHT);
 + 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, 0, getWidth(),getHeight());
 + g.setColor(Color.BLUE);
 + g.setFont(titleFont);
 + g.drawString("Clock Tester", 20, 20);
 + g.setColor(Color.BLACK);
 + g.setFont(regularFont);
 + //g.drawString("pulse = "+x, 20, 40);
 + g.fillOval(x, 200, 10, 10);
 + g.setColor(Color.RED);
 + g.setFont(clockFont);
 + g.drawString(clock.toString(), getWidth()/3, getHeight()/2);
 +
 +
 + }
 +
 +
 + public void actionPerformed(ActionEvent e) {
 +
 +      if (e.getSource()==motion){
 +     x=(x+1)%WIDTH;
 +      
 +      } else {
 +     // call the tick method
 +     clock.tick();
 +      }
 +      repaint();
 + }
 +
 +}
 +</code>
 +
  
  
Line 11: Line 101:
   - You need a ''recordPurchase'' method that added to the amount the customer owes   - You need a ''recordPurchase'' method that added to the amount the customer owes
   - You need an ''enterPayment'' method that records how much the customer has paid   - You need an ''enterPayment'' method that records how much the customer has paid
-  - You need a method ''giveChange'', that return the change and adjusts the amount due and clears the amount the custumer.  +  - You need a method ''giveChange'', that return the change and adjusts the amount due and clears the amount the customer has paid.  
-  +
 <code java CashRegisterTest.java> <code java CashRegisterTest.java>
 /** /**
Line 33: Line 123:
  
 ===== Song ===== ===== Song =====
 +the SongTester class that will test your ''Song'' class. It should be able to keep track of the song's title, artist, rank, time, and number of plays, and it should be able to show and change any of these fields. Finally, overwrite the toString() method so we can print out all the song information
  
 <code java SongTester.java> <code java SongTester.java>
Line 43: 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("q")<0) +    String input=""; 
- +    while (input.indexOf("q")<0) 
- processCommand(keyboard, song, input); +    
- System.out.println("\nCurrently: "+song+"\n"); + processCommand(keyboard, song, input); 
- System.out.print("Command (Title, Artist, Rank, Time, Plays, Quit): "); + System.out.println("\nCurrently: "+song+"\n"); 
- input=keyboard.nextLine(); + System.out.print("Command (Title, Artist, Rank, Time, Plays, Quit): "); 
- input=input.toLowerCase();+ input=keyboard.nextLine(); 
 + input=input.toLowerCase();
  
- +    
- System.out.println("\nThanks.. we exit with the song being:\n"+song);+    System.out.println("\nThanks.. we exit with the song being:\n"+song);
  
  
  }  }
  public static void processCommand(Scanner keyboard, Song song, String input){  public static void processCommand(Scanner keyboard, Song song, String input){
- if (input.equals("title")) +    if (input.equals("title")) 
- +    
- System.out.print("Currently: "+song.getTitle()+"\nNew Title: "); + System.out.print("Currently: "+song.getTitle()+"\nNew Title: "); 
- song.setTitle(keyboard.nextLine()); + song.setTitle(keyboard.nextLine()); 
- } +    } else if (input.equals("artist")) 
- else if (input.equals("artist")) +    
- + System.out.print("Currently: "+song.getArtist()+"\nNew artist name: "); 
- System.out.print("Currently: "+song.getArtist()+"\nNew artist name: "); + song.setArtist(keyboard.nextLine()); 
- song.setArtist(keyboard.nextLine()); +    } else if (input.equals("rank")) 
- } +    
- else if (input.equals("rank")) + System.out.print("Currently: "+song.getRank()+"\nNew rank: "); 
- + int newRank=Integer.parseInt(keyboard.nextLine()); 
- System.out.print("Currently: "+song.getRank()+"\nNew rank: "); + song.setRank(newRank);  
- int newRank=Integer.parseInt(keyboard.nextLine()); +    } else if (input.equals("time")) 
- song.setRank(newRank);  +    
- } + System.out.print("Currently: "+song.getTime()+"\nNew time: "); 
- else if (input.equals("time")) + double newTime=Double.parseDouble(keyboard.nextLine()); 
- + song.setTime(newTime);  
- System.out.print("Currently: "+song.getTime()+"\nNew time: "); +    } else if (input.equals("plays")) 
- double newTime=Double.parseDouble(keyboard.nextLine()); +    
- song.setTime(newTime);  + System.out.print("Currently: "+song.getPlayCount()+"\nNew number of plays: "); 
- } + int newPlays=Integer.parseInt(keyboard.nextLine()); 
- else if (input.equals("plays")) + song.setPlayCount(newPlays);  
- +       }
- System.out.print("Currently: "+song.getPlayCount()+"\nNew number of plays: "); +
- int newPlays=Integer.parseInt(keyboard.nextLine()); +
- song.setPlayCount(newPlays);  +
- }+
  
  }  }
writing_classes_practice.1664642801.txt.gz · Last modified: 2022/10/01 12:46 by frchris

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki