Foothill Community College Online Portfolio
CIS27A Beginning Java Programming
Text Version of homework assignments with instructor comments

Instructor comments are noted by the repeating @ symbol. (@@@@@@@@)

1.1 Student ID: bouyssounouse6788 (Bouyssounouse_Janine) Time Stamp: Date: 6/26/2004, Time: 1:41 PM ---------------- Assignment submitted by Bouyssounouse_Janine: /** This is a Java application which prints one line @author Janine Bouyssounouse */ public class Hello //class definition { public static void main(String[] args) //main method { System.out.println("Hello Teacher"); //does the printing } //end main method } //end class Hello C:\j>java Hello Hello Teacher @@@@@@@@@@@ perfect! @@@@@@@@@@ 1.2 Student ID: bouyssounouse6788 (Bouyssounouse_Janine) Time Stamp: Date: 6/26/2004, Time: 1:42 PM ---------------- Assignment submitted by Bouyssounouse_Janine: /** This is a Java application which prints six lines @author Janine Bouyssounouse */ public class AboutMe //class definition { public static void main(String[] args) //main method { System.out.println("Janine Bouyssounouse"); //printing System.out.println("I like to make quilts."); System.out.println("I like to teach math."); System.out.println("I live in South Lake Tahoe."); System.out.println("I grew up in Southern California."); System.out.println("I like to watch the snow fall in the winter."); } //end main method } //end class AboutMe C:\j>java AboutMe Janine Bouyssounouse I like to make quilts. I like to teach math. I live in South Lake Tahoe. I grew up in Southern California. I like to watch the snow fall in the winter. 1.3 Student ID: bouyssounouse6788 (Bouyssounouse_Janine) Time Stamp: Date: 6/26/2004, Time: 1:45 PM ---------------- Assignment submitted by Bouyssounouse_Janine: /** This is a Java application that prints out a face. @author Janine Bouyssounouse */ public class SquareFace //class definition { public static void main(String[] args) //main method { System.out.println(" //////"); //printing hair System.out.println(" | o o |"); //printing eyes System.out.println("(| ^ |)"); //printing ears and nose System.out.println(" | \\ / |"); //printing mouth System.out.println(" | ~ |"); //printing the bottom of the mouth System.out.println(" ---v---"); //printing the chin line } //end main method } //end class SquareFace C:\j>java SquareFace ////// | o o | (| ^ |) | \ / | | ~ | ---v--- @@@@@@@@@@@ goof job @@@@@@@@@@ 2.1 Student ID: bouyssounouse6788 (Bouyssounouse_Janine) Time Stamp: Date: 6/27/2004, Time: 3:10 PM ---------------- Assignment submitted by Bouyssounouse_Janine: import java.awt.Rectangle; /** Prints two rectangles and calculates the area where they overlap. @author Janine Bouyssounouse */ public class Intersect //class definition { public static void main(String[] args) //main method { //Create two rectangles Rectangle rectangle1 = new Rectangle(10, 10, 20, 20); Rectangle rectangle2 = new Rectangle(20, 10, 20, 20); //Print two rectangles System.out.println(rectangle1); System.out.println(rectangle2); //Calculate the overlapping rectangle Rectangle rectangle3 = rectangle1.intersection(rectangle2); //Print the overlapping rectangle System.out.println(rectangle3); } //end main method } //end class Intersect C:\j>java Intersect java.awt.Rectangle[x=10,y=10,width=20,height=20] java.awt.Rectangle[x=20,y=10,width=20,height=20] java.awt.Rectangle[x=20,y=10,width=10,height=20] When the rectangles do not intersect, the width and height are negative numbers that represent how far the distance is between the two rectangles. C:\j>java -version java version "1.4.2_04" Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_04-b05) Java HotSpot(TM) Client VM (build 1.4.2_04-b05, mixed mode) @@@@@@@@@@@ great @@@@@@@@@@@@ 2.2 Student ID: bouyssounouse6788 (Bouyssounouse_Janine) Time Stamp: Date: 6/27/2004, Time: 3:12 PM ---------------- Assignment submitted by Bouyssounouse_Janine: /** This is a class that uses the Bank Account class to make deposits, withdrawals and prints the resulting balance. @author Janine Bouyssounouse */ public class Bank //class defintion { /** Uses the methods of the BankAccount class. @param args not used */ public static void main(String[] args) //main method { //Creates a bank account with an initial zero balance BankAccount myChecking = new BankAccount(); //Deposits and withdrawals to the bank account myChecking.deposit(2000); myChecking.withdraw(600); myChecking.deposit(100); //Prints the resulting balance System.out.print("The balance of the bank account is "); System.out.print(myChecking.getBalance()); System.out.println(" dollars."); } //end main method } //end class Bank C:\j>java Bank The balance of the bank account is 1500.0 dollars. @@@@@@@@@@@ exactly right @@@@@@@@ 2.3 Student ID: bouyssounouse6788 (Bouyssounouse_Janine) Time Stamp: Date: 6/27/2004, Time: 3:16 PM ---------------- Assignment submitted by Bouyssounouse_Janine: /** A square with no width as the default then sets a new width. @author Janine Bouyssounouse */ public class Square //class definition { /** Constructs a square with a width of zero. */ public Square() { width = 0; } /** Sets a new width. @param newWidth the new width to be set */ public void setWidth(double NewWidth) { width = NewWidth; } /** Gets the width of the square. @return the width */ public double getWidth() { return width; } /** Gets the area of the square. @return the area */ public double getArea() { return width * width; } /** Gets the perimeter of the square. @return the perimeter */ public double getPerimeter() { return width * 4; } private double width; } /** This program tests the Square class. @author Janine Bouyssounouse */ public class SquareTest //defines the class { /** Uses the methods of the Square class. @param args not used */ public static void main(String[] args) //main method { //creates a square with a width of zero Square square1 = new Square(); //prints out the width, area, and perimeter of the square System.out.print("The square just constructed has a width of "); System.out.println(square1.getWidth()); System.out.print("The area of the square is "); System.out.println(square1.getArea()); System.out.print("The perimeter of the square is "); System.out.println(square1.getPerimeter()); System.out.println(" "); //Sets the square with a new width square1.setWidth(5); //prints out the width, area, and perimeter of the square System.out.print("The square has a new width of "); System.out.println(square1.getWidth()); System.out.print("The area of the square is "); System.out.println(square1.getArea()); System.out.print("The perimeter of the square is "); System.out.println(square1.getPerimeter()); } //end main method } //end class SquareTest C:\j>java SquareTest The square just constructed has a width of 0.0 The area of the square is 0.0 The perimeter of the square is 0.0 The square has a new width of 5.0 The area of the square is 25.0 The perimeter of the square is 20.0 @@@@@@@@@@ Good. One thing that would make your Square class more usable is if you also provided a constructor that took the width as a parameter. Then the user could create a Square with a given width in one step when desired. Having a default constructor like you do is also a good idea. @@@@@@@@@@ 3.1 Student ID: bouyssounouse6788 (Bouyssounouse_Janine) Time Stamp: Date: 7/2/2004, Time: 5:44 PM ---------------- Assignment submitted by Bouyssounouse_Janine: /** This class accepts input for a pair of integers and adds, subtracts, multiplies, finds the average, the minimum and maximum, as well as finding the absolute value of the difference of them (the distance). @author Janine Bouyssounouse */ public class Pair { /** Constructs a Pair @param aFirst the first value of the pair @param aSecond the second value of the pair */ public Pair(int aFirst, int aSecond) { first = aFirst; second = aSecond; } /** Computes the sum of the values of this pair @return the sum of the first and second values */ public int getSum() { return first + second; } /** Computes the difference of the values of this pair @return the difference of the first and second values */ public int getDifference() { return first - second; } /** Computes the product of the values of this pair @return the product of the first and second values */ public int getProduct() { return first * second; } /** Computes the average of the values of this pair @return the average of the first and second values */ public double getAverage() { return (first + second) / 2.0; } /** Computes the distance (absolute value) of the values of this pair @return the distance of the first and second values */ public int getDistance() { return Math.abs(first - second); } /** Computes the maximum of the values of this pair @return the maximum of the first and second values */ public int getMaximum() { return Math.max(first, second); } /** Computes the minimum of the values of this pair @return the minimum of the first and second values */ public int getMinimum() { return Math.min(first, second); } private int first; private int second; } //end class Pair -------- import java.io.*; /** This is a class that uses the Pair class to add, subtract, multiply, find the average, find the absolute value, find the maximum, and the minimum values based on two integers. @author Janine Bouyssounouse */ public class PairTest //class definition { /** Uses the methods of the Pair class. @param args not used */ public static void main(String[] args) throws IOException { InputStreamReader keyIn = new InputStreamReader(System.in); BufferedReader reader = new BufferedReader(keyIn); String input; int first; int second; //get first int input from the user System.out.print("Enter the first number (an integer): "); input = reader.readLine(); first = Integer.parseInt(input); //get secund int input from the user System.out.print("Enter the second number (an integer): "); input = reader.readLine(); second = Integer.parseInt(input); //constructs a Pair object Pair myPair = new Pair(first, second); //gets the sum of the integers and prints it System.out.print("Sum of " + first + " and " + second); System.out.println(" is " + myPair.getSum()); //gets the difference of the integers and prints it System.out.print("Difference between " + first + " and " + second); System.out.println(" is " + myPair.getDifference()); //gets the product of the integers and prints it System.out.print("Product of " + first + " and " + second); System.out.println(" is " + myPair.getProduct()); //gets the average of the integers and prints it System.out.print("Average of " + first + " and " + second); System.out.println(" is " + myPair.getAverage()); //gets the distance of the integers and prints it System.out.print("Distance between " + first + " and " + second); System.out.println(" is " + myPair.getDistance()); //gets the maximum of the integers and prints it System.out.print("Maximum of " + first + " and " + second); System.out.println(" is " + myPair.getMaximum()); //gets the minimum of the integers and prints it System.out.print("Minimum of " + first + " and " + second); System.out.println(" is " + myPair.getMinimum()); } //end main method } //end class PairTest C:\j>java PairTest Enter the first number (an integer): 12 Enter the second number (an integer): 7 Sum of 12 and 7 is 19 Difference between 12 and 7 is 5 Product of 12 and 7 is 84 Average of 12 and 7 is 9.5 Distance between 12 and 7 is 5 Maximum of 12 and 7 is 12 Minimum of 12 and 7 is 7 C:\j>java PairTest Enter the first number (an integer): -2 Enter the second number (an integer): 8 Sum of -2 and 8 is 6 Difference between -2 and 8 is -10 Product of -2 and 8 is -16 Average of -2 and 8 is 3.0 Distance between -2 and 8 is 10 Maximum of -2 and 8 is 8 Minimum of -2 and 8 is -2 @@@@@@@@@@ good @@@@@@@@@ 3.2 Student ID: bouyssounouse6788 (Bouyssounouse_Janine) Time Stamp: Date: 7/2/2004, Time: 5:48 PM ---------------- Assignment submitted by Bouyssounouse_Janine: /** Cashier class computes the change to be given to a customer @author Janine Bouyssounouse */ public class Cashier { /** Constructs a default Cashier */ public Cashier() { amountDue = 0; amountReceived = 0; } /** Constructs a Cashier @param anAmountDue the amount due from the customer @param anAmountReceived the amount given to pay the amount due */ public Cashier(double anAmountDue, double anAmountReceived) { amountDue = anAmountDue; amountReceived = anAmountReceived; } /** Sets the amount due from the customer @param anAmountDue the amount due from the customer */ public void setAmountDue(double anAmountDue) { amountDue = anAmountDue; } /** Sets the amount received from the customer @param anAmountReceived the amount due from the customer */ public void setAmountReceived(double anAmountReceived) { amountReceived = anAmountReceived; } /** Computes the change to be given to the customer @return the change to be given to the customer */ public double getChange() { change = amountReceived - amountDue; return change; } /** Computes the dollars to be given to the customer @return the dollars to be given to the customer */ public int getDollars() { dollars = (int)change; return dollars; } /** Computes the cents in pennies to be given to the customer @return the cents to be given to the customer */ public int getCents() { cents = (int)Math.round(100 * (change - dollars)); return cents; } /** Computes the quarters to be given to the customer @return the quarters to be given to the customer */ public int getQuarters() { quarters = cents / PENNIES_IN_QUARTERS; return quarters; } /** Computes the dimes to be given to the customer @return the dimes to be given to the customer */ public int getDimes() { changeLeft = cents - (quarters * PENNIES_IN_QUARTERS); dimes = changeLeft / PENNIES_IN_DIMES; return dimes; } /** Computes the nickels to be given to the customer @return the nickels to be given to the customer */ public int getNickels() { changeLeft = cents - (quarters * PENNIES_IN_QUARTERS) - (dimes * PENNIES_IN_DIMES); nickels = changeLeft / PENNIES_IN_NICKELS; return nickels; } /** Computes the pennies to be given to the customer @return the pennies to be given to the customer */ public int getPennies() { changeLeft = cents - (quarters * PENNIES_IN_QUARTERS) - (dimes * PENNIES_IN_DIMES) - (nickels * PENNIES_IN_NICKELS); return changeLeft; } private static final int PENNIES_IN_QUARTERS = 25; private static final int PENNIES_IN_DIMES = 10; private static final int PENNIES_IN_NICKELS = 5; private double amountDue; private double amountReceived; private double change; private int dollars; private int cents; private int quarters; private int changeLeft; private int dimes; private int nickels; } //end class Cashier --------------- import java.io.*; /** This is a class that uses the Cashier class to compute the change to be given to a customer @author Janine Bouyssounouse */ public class CashierTest //class definition { /** Uses the methods of the Cashier class. @param args not used */ public static void main(String[] args) throws IOException { InputStreamReader keyIn = new InputStreamReader(System.in); BufferedReader reader = new BufferedReader(keyIn); String input; double amountDue; double amountReceived; //get the amount due from the customer System.out.print("Enter the amount due: "); input = reader.readLine(); amountDue = Double.parseDouble(input); //get the amount received from the customer System.out.print("Enter the amount received: "); input = reader.readLine(); amountReceived = Double.parseDouble(input); //constructs a Cashier object Cashier myCashier = new Cashier(); //sets the amount due from the customer myCashier.setAmountDue(amountDue); //sets the amount received from the customer myCashier.setAmountReceived(amountReceived); //gets the change due to the customer and prints it System.out.print("The amount of change is "); System.out.println(myCashier.getChange()); //gets the dollars due to the customer and prints it System.out.print("The number of dollars in change is "); System.out.println(myCashier.getDollars()); //gets the cents due to the customer and prints it System.out.print("The number of cents in change is "); System.out.println(myCashier.getCents()); //gets the quarters due to the customer and prints it System.out.print("The number of quarters in change is "); System.out.println(myCashier.getQuarters()); //gets the dimes due to the customer and prints it System.out.print("The number of dimes in change is "); System.out.println(myCashier.getDimes()); //gets the nickels due to the customer and prints it System.out.print("The number of nickels in change is "); System.out.println(myCashier.getNickels()); //gets the pennies due to the customer and prints it System.out.print("The number of pennies in change is "); System.out.println(myCashier.getPennies()); //computing the change in opposite order System.out.println("...and now in reverse..."); //gets the pennies due to the customer and prints it System.out.print("The number of pennies in change is "); System.out.println(myCashier.getPennies()); //gets the nickels due to the customer and prints it System.out.print("The number of nickels in change is "); System.out.println(myCashier.getNickels()); //gets the dimes due to the customer and prints it System.out.print("The number of dimes in change is "); System.out.println(myCashier.getDimes()); //gets the quarters due to the customer and prints it System.out.print("The number of quarters in change is "); System.out.println(myCashier.getQuarters()); //gets the dollars due to the customer and prints it System.out.print("The number of dollars in change is "); System.out.println(myCashier.getDollars()); } //end main method } //end class CashierTest C:\j>java CashierTest Enter the amount due: 14.32 Enter the amount received: 20.00 The amount of change is 5.68 The number of dollars in change is 5 The number of cents in change is 68 The number of quarters in change is 2 The number of dimes in change is 1 The number of nickels in change is 1 The number of pennies in change is 3 ...and now in reverse... The number of pennies in change is 3 The number of nickels in change is 1 The number of dimes in change is 1 The number of quarters in change is 2 The number of dollars in change is 5 @@@@@@@@@@ printing in the reverse order only works because you printed in forward order first. If you construct a new Chasier with the same values and then getCents first you will see that you get all 0 values. You should not save cents, quarters etc. They should each be calculated from the base amount each time @@@@@@@@@@@@ 4.1 Student ID: bouyssounouse6788 (Bouyssounouse_Janine) Time Stamp: Date: 7/6/2004, Time: 11:51 AM ---------------- Assignment submitted by Bouyssounouse_Janine: import java.awt.*; import java.applet.*; import java.awt.geom.*; /** This applet draws an ellipse that resizes as the window changes. @author Janine Bouyssounouse */ public class EllipseApplet extends Applet { public void paint (Graphics g) { Graphics2D g2 = (Graphics2D)g; //Changes the background color, but only works after rectangle is cleared g2.setBackground(Color.yellow); //Allows the new background color to work when ellipse is drawn g2.clearRect(0, 0, getWidth(), getHeight()); g2.setColor(Color.cyan); //Creates the ellipse using the applet window width and height Ellipse2D.Double egg = new Ellipse2D.Double(0, 0, getWidth(), getHeight()); g2.draw(egg); //fills the ellipse with the same color as the draw color g2.fill(egg); } //end paint } //end class EllipseApplet ------ @@@@@@@@@@@@ This is a creative way to solve your problem. When I copy your applet and change g2.setBackground(Color.yellow); to setBackground(Color.yellow); The code works, too 4.2 Student ID: bouyssounouse6788 (Bouyssounouse_Janine) Time Stamp: Date: 7/9/2004, Time: 9:55 PM ---------------- Assignment submitted by Bouyssounouse_Janine: import java.awt.Graphics2D; import java.awt.Color; import java.awt.geom.Ellipse2D; import java.awt.geom.Line2D; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; /** A car shape that can be positioned anywhere on the screen. @author Janine Bouyssounouse */ public class Car //defines class { /** Constructs a car with a given top left corner and color. @param x the x-coordinate of the top left corner @param y the y-coordinate of the top left corner @param c the color of the car */ public Car(double x, double y, Color c) { xLeft = x; yTop = y; color = c; } /** Constructs a default car */ public Car() { xLeft = 0; yTop = 0; color = Color.black; } /** Draws the car. @param g2 the graphics context */ public void draw(Graphics2D g2) { Rectangle2D.Double body = new Rectangle2D.Double(xLeft, yTop + 10, 60, 10); Ellipse2D.Double frontTire = new Ellipse2D.Double(xLeft + 10,yTop + 20, 10, 10); Ellipse2D.Double rearTire = new Ellipse2D.Double(xLeft + 40, yTop + 20, 10, 10); //the bottom of the windshield Point2D.Double r1 = new Point2D.Double(xLeft + 10, yTop + 10); //the front of the roof Point2D.Double r2 = new Point2D.Double(xLeft + 20, yTop); //the rear of the roof Point2D.Double r3 = new Point2D.Double(xLeft + 40, yTop); //the bottom of the rear window Point2D.Double r4 = new Point2D.Double(xLeft + 50, yTop + 10); Line2D.Double frontWindshield = new Line2D.Double(r1, r2); Line2D.Double roofTop = new Line2D.Double(r2, r3); Line2D.Double rearWindow = new Line2D.Double(r3, r4); g2.setColor(color); g2.draw(body); g2.draw(frontTire); g2.draw(rearTire); g2.draw(frontWindshield); g2.draw(roofTop); g2.draw(rearWindow); } private double xLeft; private double yTop; private Color color; } //end class Car -------- import java.applet.Applet; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; /** This applet draws three cars each with a different color. @author Janine Bouyssounouse */ public class CarApplet extends Applet { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; Car car1 = new Car(10, 10, Color.blue); Car car2 = new Car(90, 90, Color.red); Car car3 = new Car(200, 200, Color.green); car1.draw(g2); car2.draw(g2); car3.draw(g2); } } //end CarApplet -------- 4.3 Student ID: bouyssounouse6788 (Bouyssounouse_Janine) Time Stamp: Date: 7/9/2004, Time: 10:08 PM ---------------- Assignment submitted by Bouyssounouse_Janine: import java.awt.Graphics2D; import java.awt.Color; import java.awt.geom.Ellipse2D; import java.awt.geom.Line2D; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; /** A truck shape that can be positioned anywhere on the screen. @author Janine Bouyssounouse */ public class Truck //defines class { /** Constructs a truck with a given top left corner and color. @param x the x-coordinate of the top left corner @param y the y-coordinate of the top left corner @param c the color of the car */ public Truck(double x, double y, Color c) { xLeft = x; yTop = y; color = c; } /** Constructs a default truck */ public Truck() { xLeft = 0; yTop = 0; color = Color.black; } /** Draws the car. @param g2 the graphics context */ public void draw(Graphics2D g2) { Rectangle2D.Double body = new Rectangle2D.Double(xLeft, yTop + 10, 80, 10); Ellipse2D.Double frontTire = new Ellipse2D.Double(xLeft + 10,yTop + 20, 10, 10); Ellipse2D.Double rearTire = new Ellipse2D.Double(xLeft + 60, yTop + 20, 10, 10); //the bottom of the windshield Point2D.Double r1 = new Point2D.Double(xLeft + 10, yTop + 10); //the front of the roof Point2D.Double r2 = new Point2D.Double(xLeft + 20, yTop); //the rear of the roof Point2D.Double r3 = new Point2D.Double(xLeft + 30, yTop); //the bottom of the rear window Point2D.Double r4 = new Point2D.Double(xLeft + 40, yTop + 10); Line2D.Double frontWindshield = new Line2D.Double(r1, r2); Line2D.Double roofTop = new Line2D.Double(r2, r3); Line2D.Double rearWindow = new Line2D.Double(r3, r4); g2.setColor(color); g2.draw(body); g2.draw(frontTire); g2.draw(rearTire); g2.draw(frontWindshield); g2.draw(roofTop); g2.draw(rearWindow); } private double xLeft; private double yTop; private Color color; } //end class Truck --------------- import java.applet.Applet; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; /** This applet draws two cars and two trucks each with a different color. @author Janine Bouyssounouse */ public class TruckApplet extends Applet { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; Car car1 = new Car(10, 10, Color.blue); Car car2 = new Car(90, 90, Color.red); Truck truck1 = new Truck(150, 150, Color.green); Truck truck2 = new Truck(200, 200, Color.pink); car1.draw(g2); car2.draw(g2); truck1.draw(g2); truck2.draw(g2); } } //end TruckApplet ------------------- @@@@@@@@@@@@@@@ The direction said to call this VehicleApplet 5.1 Student ID: bouyssounouse6788 (Bouyssounouse_Janine) Time Stamp: Date: 7/10/2004, Time: 5:19 PM ---------------- Assignment submitted by Bouyssounouse_Janine: import java.awt.Graphics2D; import java.awt.geom.Ellipse2D; /** This class implements a circle class with two circles using radii from user input @author Janine Bouyssounouse */ public class Circle { /** Constructs a circle which actually has two circles included in it @param aRadius1 the radius of the first circle @param aRadius2 the radius of the second circle */ public Circle(double aRadius1, double aRadius2) { radius1 = aRadius1; radius2 = aRadius2; } /** Creates and draws two circles @param g2 the graphics content */ public void draw(Graphics2D g2) { Ellipse2D.Double circle1 = new Ellipse2D.Double(CIRCLE1X , CIRCLE1Y, 2 * radius1, 2 * radius1); Ellipse2D.Double circle2 = new Ellipse2D.Double(CIRCLE2X , CIRCLE2Y, 2 * radius2, 2 * radius2); g2.draw(circle1); g2.draw(circle2); } private static final double CIRCLE1X = 100; private static final double CIRCLE2X = 200; private static final double CIRCLE1Y = 200; private static final double CIRCLE2Y = 100; private double radius1; private double radius2; } //end class Circle @@@@@@@@@@@@@ Have your Circle class define one Circle with a variable center and radius. If you make the center x and y parameters rather than hard-coding them, the class could be resured to solve problems with other centers. @@@@@@@@@@@ --------------------- import java.applet.Applet; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Point2D; import java.awt.geom.Ellipse2D; import javax.swing.JOptionPane; /** This class tests class Circle to determine if the circles intersect. @author Janine Bouyssounouse */ public class CircleTest extends Applet { public void init() //asks for the two radii { String input = JOptionPane.showInputDialog("What is the radius of the first circle?"); radius1 = Integer.parseInt(input); input = JOptionPane.showInputDialog("What is the radius of the second circle?"); radius2 = Integer.parseInt(input); } //end init public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; Circle circle = new Circle(radius1, radius2); //computes the distance between the centers of the circles double distance = Math.sqrt(((CIRCLE2X + radius2) - (CIRCLE1X + radius1)) * ((CIRCLE2X + radius2) - (CIRCLE1X + radius1)) + ((CIRCLE2Y + radius2) - (CIRCLE1Y + radius1)) * ((CIRCLE2Y + radius2) - (CIRCLE1Y + radius1))); double sumRadius = radius1 + radius2; String message; //determines if circles intersect or not if(sumRadius >= distance) message = "Circles intersect."; else message = "Circles don't intersect."; //doesn't draw anything if either of the radii is negative if(radius1 > 0 && radius2 > 0) { g2.drawString(message, WORDSX, WORDSY); circle.draw(g2); } //end if } //end paint private static final double CIRCLE1X = 100; private static final double CIRCLE2X = 200; private static final double CIRCLE1Y = 200; private static final double CIRCLE2Y = 100; private static final int WORDSX = 50; private static final int WORDSY = 50; private double radius1; private double radius2; } //end class CircleTest --------------------- 5.2 Student ID: bouyssounouse6788 (Bouyssounouse_Janine) Time Stamp: Date: 7/10/2004, Time: 6:14 PM ---------------- Assignment submitted by Bouyssounouse_Janine: /** A Grade class that returns the numerical grade that corresponds to the letter grade. @author Janine Bouyssounouse */ public class Grade //class definition { /** Constructs a Grade with input @param aLetterGrade the letter grade */ public Grade(String aLetterGrade) { letterGrade = aLetterGrade; } /** Gets the numeric grade for a given letter grade @return the numeric grade */ public double getNumericGrade() { if(letterGrade.equalsIgnoreCase("A+")) numericGrade = 4; if(letterGrade.equalsIgnoreCase("A")) numericGrade = 4; if(letterGrade.equalsIgnoreCase("A-")) numericGrade = 3.7; if(letterGrade.equalsIgnoreCase("B+")) numericGrade = 3.3; if(letterGrade.equalsIgnoreCase("B")) numericGrade = 3; if(letterGrade.equalsIgnoreCase("B-")) numericGrade = 2.7; if(letterGrade.equalsIgnoreCase("C+")) numericGrade = 2.3; if(letterGrade.equalsIgnoreCase("C")) numericGrade = 2; if(letterGrade.equalsIgnoreCase("C-")) numericGrade = 1.7; if(letterGrade.equalsIgnoreCase("D+")) numericGrade = 1.3; if(letterGrade.equalsIgnoreCase("D")) numericGrade = 1; if(letterGrade.equalsIgnoreCase("D-")) numericGrade = .7; if(letterGrade.equalsIgnoreCase("F")) numericGrade = 0; return numericGrade; } //end getNumericGrade @@@@@@@@@@@@ Using equalsIgnoreCase is a nice touch @@@@@@@@@@ private double numericGrade; private String letterGrade; } //end class Grade ------------------- /** This is a class that uses the Grade class to display the numerical grade for a letter grade @author Janine Bouyssounouse */ public class GradeTest //class definition { /** Uses the methods of the Grade class. @param args not used */ public static void main(String[] args) throws IOException { InputStreamReader keyIn = new InputStreamReader(System.in); BufferedReader reader = new BufferedReader(keyIn); String letterGrade; //get the letter grade System.out.print("Enter the letter grade: "); letterGrade = reader.readLine(); //constructs a Grade object Grade theGrade = new Grade(letterGrade); //gets the numerical grade System.out.print("The numerical grade for the letter grade "); System.out.print(letterGrade + " is "); System.out.println(theGrade.getNumericGrade()); } //end main method } //end class GradeTest --------------------- C:\j>java GradeTest Enter the letter grade: C+ The numerical grade for the letter grade C+ is 2.3 @@@@@@@@@@@@@@@@@ This works fine. You just need to verify that with the testing. Does A+, F-, X, B, D- give the correct results? 6.1 Student ID: bouyssounouse6788 (Bouyssounouse_Janine) Time Stamp: Date: 7/11/2004, Time: 1:36 AM ---------------- Assignment submitted by Bouyssounouse_Janine: import java.applet.Applet; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Ellipse2D; import javax.swing.JOptionPane; import java.util.Random; /** This applet draws random circles on the window based on the input number of circles to be drawn @author Janine Bouyssounouse */ public class RandomCirclesApplet extends Applet { public void init() //asks for the number of circles to be drawn { boolean done = false; while (!done) //requires input until response is > 0 { String input = JOptionPane.showInputDialog("How many circles to you want to be drawn?"); numCircles = Integer.parseInt(input); if(numCircles > 0) done = true; } } //end init public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; Random generator = new Random(); for(int i = 1; i <= numCircles; i++) { int CircleX = 1 + generator.nextInt(300); int CircleY = 1 + generator.nextInt(300); int radius = 1 + generator.nextInt(100); @@@@@@@@@@@ variable names should start with lowercase - by convention @@@@@@@@@@ Ellipse2D.Double circle = new Ellipse2D.Double(CircleX , CircleY, 2 * radius, 2 * radius); g2.draw(circle); } } //end paint private int numCircles; } //end RandomCirclesApplet ------------------------------------- @@@@@@@@@@@ This is good @@@@@@@@@@ 6.2 Student ID: bouyssounouse6788 (Bouyssounouse_Janine) Time Stamp: Date: 7/11/2004, Time: 1:55 PM ---------------- Assignment submitted by Bouyssounouse_Janine: import java.awt.Graphics; import java.awt.Graphics2D; import java.util.Random; import java.awt.geom.*; /** A ten by ten grid. @author Janine Bouyssounouse */ public class Grid //defines class { /** Constructs a default grid 10 by 10 */ public Grid() { gridX = 50; gridY = 50; } /** Draws the 10 by 10 grid */ public void draw(Graphics2D g2) { for(int i = 0; i <= 10; i++) { //horizontal line Point2D.Double p1 = new Point2D.Double(gridX, (gridY + 40 * i)); Point2D.Double p2 = new Point2D.Double((gridX + GRID_WIDTH), (gridY + 40 * i)); Line2D.Double line = new Line2D.Double(p1, p2); g2.draw(line); } for(int i = 0; i <= 10; i++) { //vertical line Point2D.Double p1 = new Point2D.Double((gridX + 40 * i), gridY); Point2D.Double p2 = new Point2D.Double((gridX + 40 * i), (gridY + GRID_WIDTH)); Line2D.Double line = new Line2D.Double(p1, p2); g2.draw(line); } @@@@@@@@@@@@@ indent all of the statements in the for lop by the same amount. Don't let your lines wrap @@@@@@@@@@@@@@ } private static final double GRID_WIDTH = 400; private double gridX; private double gridY; } //end class Grid ------------------------------- import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.*; import java.util.Random; /** This applet plots a random path on the wondow based on 100 moves to be made @author Janine Bouyssounouse */ public class DrunkPath //defines class { public DrunkPath() //creates DrunkPath { numMoves = 100; } public void draw(Graphics2D g2) //draws drunk path { Random generator = new Random(); double moveX = 0; double moveY = 0; for(int i = 0; i <= numMoves; i++) { //picks the random direction for the drunk to move int direction = 1 + generator.nextInt(4); //calculates how the drunk will move based on the random direction switch(direction) { case 1: moveX = moveX - 40; break; case 2: moveY = moveY - 40; break; case 3: moveX = moveX + 40; break; case 4: moveY = moveY + 40; break; default: moveX = moveX; moveY = moveY; break; } // draw the point where the drunk is now Ellipse2D.Double point = new Ellipse2D.Double(DRUNK_STARTX + moveX - 1, DRUNK_STARTY + moveY - 1, 3, 3); g2.draw(point); } // draw the line from start to finish of the path Point2D.Double start = new Point2D.Double(DRUNK_STARTX, DRUNK_STARTY); Point2D.Double end = new Point2D.Double((DRUNK_STARTX + moveX), (DRUNK_STARTY + moveY)); Line2D.Double line = new Line2D.Double(start, end); g2.draw(line); } //end paint private static final double DRUNK_STARTX = 270; private static final double DRUNK_STARTY = 270; private int numMoves; } //end class DrunkPath @@@@@@@@@@@ YOu should have a class that represents the drunkard not the whole drunkard path. The drunkard will have a location and a draw method and a move method. The loop control will then be in the applet which will create a Drunkard, then call its move and draw method 10 times @@@@@@@@@@@@ -------------------------------------- import java.awt.*; import java.applet.*; import java.awt.geom.*; /** This program tests the DrunkPath and Grid classes. @author Janine Bouyssounouse */ public class DrunkPathTest extends Applet { // creates and draws grid and path public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; Grid myGrid = new Grid(); myGrid.draw(g2); DrunkPath myDrunkPath = new DrunkPath(); myDrunkPath.draw(g2); } //end paint } //end class DrunkPathTest --------------------------------- 7.1 Student ID: bouyssounouse6788 (Bouyssounouse_Janine) Time Stamp: Date: 7/24/2004, Time: 6:18 PM ---------------- Assignment submitted by Bouyssounouse_Janine: import java.awt.geom.Point2D; /** This class provides a method that determines the distance between two points specified by the user. @author Janine Bouyssounouse */ public class Geometry { /** Computes if a point is inside an ellipse @param p a point @param q a point @return distance between points */ public static double distance(Point2D.Double p, Point2D.Double q) { return Math.sqrt( (p.getX() - q.getX()) * (p.getX() - q.getX()) + (p.getY() - q.getY()) * (p.getY() - q.getY()) ); } } ------------------- import java.io.*; import java.awt.geom.Point2D; /** This is a class that uses the Geometry class to compute the distance between two points input by the user @author Janine Bouyssounouse */ public class TwoPoints //class definition { /** Uses the methods of the Geometry class. @param args not used */ public static void main(String[] args) throws IOException { InputStreamReader keyIn = new InputStreamReader(System.in); BufferedReader reader = new BufferedReader(keyIn); String input; double x1; double y1; double x2; double y2; //get the first x value from the user System.out.print("Enter the x value of the first point: "); input = reader.readLine(); x1 = Double.parseDouble(input); //get the first y value from the user System.out.print("Enter the y value of the first point: "); input = reader.readLine(); y1 = Double.parseDouble(input); //get the second x value from the user System.out.print("Enter the x value of the second point: "); input = reader.readLine(); x2 = Double.parseDouble(input); //get the second y value from the user System.out.print("Enter the y value of the second point: "); input = reader.readLine(); y2 = Double.parseDouble(input); //convert the input values to points Point2D.Double p = new Point2D.Double(x1, y1); Point2D.Double q = new Point2D.Double(x2, y2); //gets the distance between the points and prints it System.out.print("The distance between the points is "); System.out.println(Geometry.distance(p, q)); } //end main method } //end class TwoPoints Output: C:\j>java TwoPoints Enter the x value of the first point: 0 Enter the y value of the first point: 0 Enter the x value of the second point: 3 Enter the y value of the second point: 4 The distance between the points is 5.0 It makes sense to use a static method because the distance formula is just manipulating numbers and doesn’t use any instance variables 7.2 Student ID: bouyssounouse6788 (Bouyssounouse_Janine) Time Stamp: Date: 7/24/2004, Time: 9:11 PM ---------------- Assignment submitted by Bouyssounouse_Janine: import java.awt.Graphics2D; import java.awt.geom.Ellipse2D; import java.awt.geom.Line2D; import java.awt.geom.Point2D; /** This class draws the letters H, E, L and O. @author Janine Bouyssounouse */ public class StaticLetters { /** Draws the letter H @param g2 the graphics context @param p the top left corner point */ public static void drawH(Graphics2D g2, Point2D.Double p) { double x = p.getX(); double y = p.getY(); Line2D.Double left = new Line2D.Double(x, y, x, y + HEIGHT); Line2D.Double right = new Line2D.Double(x + WIDTH, y, x + WIDTH, y + HEIGHT); Line2D.Double middle = new Line2D.Double(x, y + (HEIGHT / 2), x + WIDTH, y + (HEIGHT / 2)); g2.draw(left); g2.draw(right); g2.draw(middle); } /** Draws the letter E @param g2 the graphics context @param p the top left corner point */ public static void drawE(Graphics2D g2, Point2D.Double p) { double x = p.getX(); double y = p.getY(); Line2D.Double left = new Line2D.Double(x, y, x, y + HEIGHT); Line2D.Double top = new Line2D.Double(x, y, x + WIDTH, y); Line2D.Double middle = new Line2D.Double(x, y + (HEIGHT / 2), x + WIDTH, y + (HEIGHT / 2)); Line2D.Double bottom = new Line2D.Double(x, y + HEIGHT, x + WIDTH, y + HEIGHT); g2.draw(left); g2.draw(top); g2.draw(middle); g2.draw(bottom); } /** Draws the letter L @param g2 the graphics context @param p the top left corner point */ public static void drawL(Graphics2D g2, Point2D.Double p) { double x = p.getX(); double y = p.getY(); Line2D.Double left = new Line2D.Double(x, y, x, y + HEIGHT); Line2D.Double bottom = new Line2D.Double(x, y + HEIGHT, x + WIDTH, y + HEIGHT); g2.draw(left); g2.draw(bottom); } /** Draws the letter O @param g2 the graphics context @param p the top left corner point */ public static void drawO(Graphics2D g2, Point2D.Double p) { double x = p.getX(); double y = p.getY(); Ellipse2D.Double o = new Ellipse2D.Double(x, y, WIDTH, HEIGHT); g2.draw(o); } private static final int WIDTH = 10; private static final int HEIGHT = 30; } ------------------------------------ import java.applet.Applet; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Ellipse2D; import java.awt.geom.Line2D; import java.awt.geom.Point2D; /** This programs tests the StaticLetters class. @author Janine Bouyssounouse */ public class StaticLettersApplet extends Applet { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; final int WIDTH = 10; final int HEIGHT = 30; final int SPACE = 3; double x = 50; double y = 50; StaticLetters letters = new StaticLetters(); @@@@@@@@@@@@@@ Don't create an instance of the class to call static methods @@@@@@@@@@@@@ letters.drawH(g2, new Point2D.Double(x, y)); x = x + WIDTH + SPACE; letters.drawE(g2, new Point2D.Double(x, y)); x = x + WIDTH + SPACE; letters.drawL(g2, new Point2D.Double(x, y)); x = x + WIDTH + SPACE; letters.drawL(g2, new Point2D.Double(x, y)); x = x + WIDTH + SPACE; letters.drawO(g2, new Point2D.Double(x, y)); x = 60; y = y + HEIGHT + SPACE; letters.drawH(g2, new Point2D.Double(x, y)); x = x + WIDTH + SPACE; letters.drawO(g2, new Point2D.Double(x, y)); x = x + WIDTH + SPACE; letters.drawL(g2, new Point2D.Double(x, y)); x = x + WIDTH + SPACE; letters.drawE(g2, new Point2D.Double(x, y)); } } --------------------------------- 8.1 Student ID: bouyssounouse6788 (Bouyssounouse_Janine) Time Stamp: Date: 7/30/2004, Time: 3:10 PM ---------------- Assignment submitted by Bouyssounouse_Janine: import java.awt.geom.Point2D; import java.awt.geom.Line2D; import java.awt.Graphics2D; import java.util.ArrayList; /** This class creates and draws a polygon by connecting the vertices given to it by another program through the add method.Then the polygon is drawn through the draw method. @author Janine Bouyssounouse */ public class Polygon { /** Constructs a polygon to be filled with points through the add method. */ public Polygon() { vertices = new ArrayList(); } /** Adds a vertex of the polygon. @param aVertex the vertex of the polygon */ public void add(Point2D.Double aVertex) { vertices.add(aVertex); } // end add /** Draws the polygon. @param g2 the graphics context */ public void draw(Graphics2D g2) { for (int i = 0; i < vertices.size(); i++) { Point2D.Double start = (Point2D.Double)vertices.get(i); Point2D.Double end = (Point2D.Double)vertices.get((i + 1)); g2.draw(new Line2D.Double(start, end)); } } // end draw private ArrayList vertices; } // end class Polygon ---------------------------------- import java.applet.Applet; import java.awt.Graphics; import java.awt.Graphics2D; import java.util.Random; import java.awt.geom.Point2D; /** This applet uses the polygon class to draw a square, a pentagon and an octagon. @author Janine Bouyssounouse */ public class PolygonApplet extends Applet { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; Random generator = new Random(); double x = 0; double y = 0; Polygon square = new Polygon(); x = 10 + 10 * generator.nextDouble(); y = 10 + 10 * generator.nextDouble(); square.add(new Point2D.Double(x, y)); square.add(new Point2D.Double(x + SQUARE_WIDTH, y)); square.add(new Point2D.Double(x + SQUARE_WIDTH, y + SQUARE_WIDTH)); square.add(new Point2D.Double(x, y + SQUARE_WIDTH)); Polygon pentagon = new Polygon(); x = 100 + 20 * generator.nextDouble(); y = 100 + 20 * generator.nextDouble(); pentagon.add(new Point2D.Double(x, y)); pentagon.add(new Point2D.Double(x + 40, y + 30)); pentagon.add(new Point2D.Double(x + 10, y + 70)); pentagon.add(new Point2D.Double(x - 40, y + 70)); pentagon.add(new Point2D.Double(x - 70, y + 30)); Polygon octagon = new Polygon(); x = 200 + 30 * generator.nextDouble(); y = 200 + 30 * generator.nextDouble(); octagon.add(new Point2D.Double(x, y)); octagon.add(new Point2D.Double(x + 50, y)); octagon.add(new Point2D.Double(x + 90, y + 30)); octagon.add(new Point2D.Double(x + 90, y + 80)); octagon.add(new Point2D.Double(x + 50, y + 110)); octagon.add(new Point2D.Double(x, y + 110)); octagon.add(new Point2D.Double(x - 40, y + 80)); octagon.add(new Point2D.Double(x - 40, y + 30)); square.draw(g2); pentagon.draw(g2); octagon.draw(g2); } public static final double SQUARE_WIDTH = 50; } // end PolygonApplet -------------------------------- @@@@@@@@@@@@ Your code generates this exception java.lang.IndexOutOfBoundsException: Index: 4, Size: 4 That means you are trying to access an element in the ArrayList that does not exist. These two lines in the stack trace at Polygon.draw(PolygonApplet.java:50) at PolygonApplet.paint(PolygonApplet.java:110) tells you where in your program the error occurred. It So look at line 50 in the draw mwthod of your Polygon class. You are trying to connect the last point to a point that doesn't exist rather than to the first point 8.2 Student ID: bouyssounouse6788 (Bouyssounouse_Janine) Time Stamp: Date: 7/30/2004, Time: 3:46 PM ---------------- Assignment submitted by Bouyssounouse_Janine: import java.util.Random; import java.io.*; /** This program creates an array and fills it with random numbers, then asks the user for a number. The program checks to see if the user's number is in the array. @author Janine Bouyssounouse */ public class RandomArray { public static void main(String[] args) throws IOException { int[] myList = new int[LIST_LENGTH]; Random generator = new Random(); // fill the array for (int i = 0; i < LIST_LENGTH; i++) { myList[i] = 1 + generator.nextInt(LIST_LENGTH); } InputStreamReader keyIn = new InputStreamReader(System.in); BufferedReader reader = new BufferedReader(keyIn); String input; int userNumber; boolean foundNumber = false; // get the number from the user System.out.println("Enter an integer between 1 and " + LIST_LENGTH); input = reader.readLine(); userNumber = Integer.parseInt(input); // check the array for the user's number for (int i = 0; i < LIST_LENGTH; i++) { if(myList[i] == userNumber) { foundNumber = true; } } if(foundNumber) { System.out.println("Your number is in the list."); } else { System.out.println("Your number is not in the list."); } } public static final int LIST_LENGTH = 50; } output: C:\j>java RandomArray Enter an integer between 1 and 50 2 Your number is in the list. C:\j>java RandomArray Enter an integer between 1 and 50 2 Your number is not in the list. @@@@@@@@@ good website created by Janine Bouyssounouse. Last updated 02/05/07