






















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!























Computer Science 121 Lecture 1
public person (String n, int a) { name = n; age = a; } // constructor // Method that displays information about a person. public void displayAge ( ) { System.out.println (name + "'s age is " + age); } // method displayAge } // class person Lecture 3
} // class person // Application for a clerk in a store. import java.io.; import java.text.; // Main class that declares a store clerk and calls the methods. public class Store { public static void main (String [] args) { Clerk storeClerk = new Clerk (); storeClerk.getPrice (); storeClerk.calculateTax (); storeClerk.displayTotal (); } // method main } // class Store // Class that defines a store clerk. class Clerk { private double price, tax, total; // Method that reads the price of a product from the keyboard. public void getPrice () { System.out.print ("Enter the price: "); price = readDouble (); } // method getPrice // Method that calculates the tax. public void calculateTax () { final double taxRate = 0.0825; tax = price * taxRate; } // method calculateTax // Method that calculates and then displays the total bill. public void displayTotal () { total = tax + price; System.out.println ("The total bill is " + decimals (total)); } // method displayTotal // Reads a double from the keyboard and returns it to the calling method. public static double readDouble () { double num; BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in)); try
num = Double.valueOf (stdin.readLine ()).doubleValue (); } catch (IOException e) { num = 0;} return num; } // method readDouble // Formats a double for string output with two decimal places. public static String decimals (double num) { DecimalFormat decFor = new DecimalFormat (); decFor.setMaximumFractionDigits (2); decFor.setMinimumFractionDigits (2); return decFor.format (num); } // method decimals } // class Clerk Lecture 7
Lecture 8
Price = Reader.readInteger (); total = total + price; } // fir System.out.println ("The total bill is " + total ); } // method main } // class prices no_prices count price total 5 0 1.95 1. 1 3.15 5. 2 2.98 8. 3 .55 8. 4 2.15 10. Lecture 9
} // method drawTopBottom // Method to draw the middle of the box. public void drawMiddle () { for (int row = 0; row < height-2; row ++) { System.out.print (''); for (int col = 0; col < width-2; col ++) System.out.print (' '); System.out.println (''); } // for } // method drawMiddle } // class Box Lecture 11
// Class that defines a store clerk. class Clerk { private double price, tax, total; // Method that reads the price of a product from the keyboard. public void getPrice () { System.out.print ("Enter the price: "); price = readDouble (); } // method getPrice // Method that calculates the tax. public void calculateTax () { final double taxRate = 0.0825; tax = price * taxRate; } // method calculateTax // Method that calculates and then displays the total bill. public void displayTotal () { total = tax + price; System.out.println ("The total bill is " + decimals (total)); } // method displayTotal // Reads a double from the keyboard and returns it to the calling method. public static double readDouble () { double num; BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in)); try { num = Double.valueOf (stdin.readLine ()).doubleValue (); } catch (IOException e) { num = 0;} return num; } // method readDouble // Formats a double for string output with two decimal places. public static String decimals (double num) { DecimalFormat decFor = new DecimalFormat (); decFor.setMaximumFractionDigits (2); decFor.setMinimumFractionDigits (2); return decFor.format (num); } // method decimals } // class Clerk
System.out.print ("Price: "); price = readDouble (); sum = sum + price; } // for } // method getOrder // Method that calculates the tax. public double calculateTax () { final double taxRate = 0.0825; tax = sum * taxRate; return tax; } // method calculateTax // Method that calculates and then displays the total bill. public void displayTotal () { tax = calculateTax (); total = tax + sum; System.out.println ("The total bill is " + decimals (total)); } // method displayTotal // Reads a double from the keyboard and returns it to the calling method. public static double readDouble () { double num; BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in)); try { num = Double.valueOf (stdin.readLine ()).doubleValue (); } catch (IOException e) { num = 0;} return num; } // method readDouble // Formats a double for string output with two decimal places. public static String decimals (double num) { DecimalFormat decFor = new DecimalFormat (); decFor.setMaximumFractionDigits (2); decFor.setMinimumFractionDigits (2); return decFor.format (num); } // method decimals // Reads an integer from the keyboard and returns it to the calling method. public static int readInteger () { int num; BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
try { num = Integer.parseInt (stdin.readLine ()); } catch (IOException e) { num = 0;} return num; } // method readInteger } // class Clerk Lecture 13 Second clerk example // Application for a clerk in a store. import java.io.; import java.text.; // Main class that declares a store clerk and calls the methods. public class Store { public static void main (String [] args) { Clerk storeClerk = new Clerk (); storeClerk.getOrder (); storeClerk.displayTotal (); } // method main } // class Store // Class that defines a store clerk. class Clerk { private double sum, tax, total; int noPrices; public Clerk () { sum = 0; noPrices = 0; } // constructor // Method to read prices from the keyboard, sum them and store the total value. public void getOrder () { double price; System.out.println ("Enter prices, terminate with a zero."); System.out.print ("Price: "); price = readDouble ();
Drawing figures - rectangles, lines, ovals, colors
Doubles and floats, integers, characters, boolean public class TestDoubles { public static void main (String [] args) { double value = 1.0; while (value < 11) { System.out.println ("" + value); value = value + 0.1; } } // method main
Lecture 16
public boolean makeChoice () { boolean quit = false; System.out.print ("Enter your choice: "); choice = Reader.readChar (); switch (choice) { case 'b': case 'B': System.out.println ("Balance"); break; case 'd': case 'D': System.out.println ("Deposit"); break; case 'w': case 'W': System.out.println ("Withdrawal"); break; case 'm': case 'M': displayMenu (); break; case 'q': case 'Q': quit = true; break; default: System.out.println ("Invalid entry - try again"); } // switch return quit; } // method makeChoice; public void makeSeveralChoices () { boolean quit = false; do { displayMenu (); quit = makeChoice (); }while (!quit); System.out.println ("Thank you, come again."); } // method makeSeveralChoices } // class Menu class Reader { // Reads a character from the keyboard and returns it to the calling method. public static char readChar () { char ch; BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in)); try { ch = (char)stdin.read (); } catch (IOException e) { ch = ' ';} return ch; } // method readChar () } // public class Reader Lecture 18