CSIS 10A Assignment 9 Solutions Read: Chapter 9 Choose and complete any 10 points from the problems below, which are all included in the download file on the website. Use BlueJ to complete the assignment, then export JAR file and upload to the server using your pass code. You may do more than 10 points of work but the max award will be 11 points. Part I: Exam Prep Exercises--7 points total To be answered in the Exercises file Algorithm Workbench 1. Given the Circle class included in the Exercises file,
a) public String tostring() return "Circle: radius = " + radius + ", area = " + getarea() ); b) public boolean equals(circle other) return (this.radius == other.radius); // parenthesis optional, 'this' is also optional // note that we can refer to other.radius // directly since we share the same class (Circle) c) public boolean greaterthan(circle other) return this.getradius > other.getradius; Part 3: Programming Challenges 3 points each 1) Create classes P1LandTract and P1LandTractDemo and solve the following problem: In reading the problem statement you can determine that a UML of the LandTract class would look like this: LandTract -length: double -width: double + LandTract() + LandTract(l: double, w: double) + getarea(): double + equals(landtract other): boolean + tostring(): String Here is the solution, and a Demo program: LandTract.java public class LandTract // instance variables - replace the example below with your own private double length; private double width; * Constructor for objects of class LandTract public LandTract()
length = 0; width = 0; public LandTract(double l,double w) length = l; width = w; public double getarea() return length*width; public String tostring() String str; str = ("length: "+length+"\nwidth: "+width+"\narea: "+this.getarea()); return str; public boolean equals(landtract L) boolean status; if(this.getarea()==l.getarea()) status = true; status = false; return status; P1LandTractDemo.java * Write a description of class P1LandTractDemo here. * * @author (your name) * @version (a version number or a date) import java.util.scanner; public class P1LandTractDemo static LandTract here; static LandTract there; public static void main(string[] args) defineland(); displayarea(); determineequality(); public static void defineland() double length; double width; Scanner keyboard = new Scanner(System.in);
System.out.println("enter the length for here:"); length = keyboard.nextdouble(); System.out.println("enter the width for here:"); width = keyboard.nextdouble(); here = new LandTract(length,width); System.out.println("enter the length for there:"); length = keyboard.nextdouble(); System.out.println("enter the width for there:"); width = keyboard.nextdouble(); there = new LandTract(length,width); public static void displayarea() double area; area = here.getarea(); System.out.println("The area here is: "+area); area = there.getarea(); System.out.println("The area there is: "+area); public static void determineequality() if(here.equals(there)) System.out.println("here and there are equal areas"); System.out.println("here and there are different areas"); 2) Create new classes called P2FuelGauge, P2Odometer, and P2CarSimulator that solves the following problem
This problem is not well defined, so we have some flexibility in how we set it up. The FuelGauge class needs to know how many gallons are currently in the tank. Therefore we need a member field called "gallons" to keep track of this information. We also need a way to add a gallon, and remove a gallon. Similarly, the odometer needs to know the mileage, report on the mileage, and increment the mileage by one gallon. From the problem statement the Odometer needs to have access to the FuelGauge object. There are at least two ways to do this: 1) We can give it access through the use of "Aggregation" like we learned in the lab -- that means including the car's FuelGauge object in the Odometer class's list of member fields and including a FuelGauge in the Odometer constructor; OR 2) we can pass our FuelGauge object to the incrementmileage method and let it change the FuelGauge whenever we've driven 24 miles. We'll go with number 1) for this solution. Why? Well, it seems like the Odometer needs to be connected to a particular FuelGauge for its entire existence. If we have to pass a FuelGauge to the Odometer every time we drive a mile, we might pass the "wrong" fuel gauge at some point (if we are simulating two cars for example). If we give the FuelGauge to the Odometer constructor, the odometer will always be able to change the FuelGauge object it started out with by referring to its own member field for it We'll also need some way to keep track of when we've driven enough to invoke the "decrementgallons" method so for this we'll create another field in Odometer to keep track of this mileage called setpoint. Here are two UML diagrams for the two classes described: With our classes defined as above, we can write the main app that runs the simulation: Chapter 9, Progrmming Challenge 10 Car Instrument Simulator public class CarInstrumentSimulator public static void main(string[] args)
// Create a FuelGuage object. FuelGauge fuel = new FuelGauge(); // Create an Odometer object to work with the FuelGauge object. Odometer odometer = new Odometer(0, fuel); // Fill the car up with gas. for (int i = 0; i < fuel.max_gallons; i++) fuel.incrementgallons(); // Drive the car until it runs out of gas. while (fuel.getgallons() > 0) // Drive a mile. odometer.incrementmileage(); // Display the mileage. System.out.println("Mileage: " + odometer.getmileage()); // Display the amount of fuel. System.out.println("Fuel level: " + fuel.getgallons() + " gallons"); System.out.println("------------------------------"); FuelGauge class public class FuelGauge // Constant for the maximum number of gallons final int MAX_GALLONS = 15; // Field for the amount of fuel, in gallons private int gallons; No-arg constructor public FuelGauge() gallons = 0; Constructor @param g The initial number of gallons. public FuelGauge(int g) // Set gallons to g, but no more than // the maximum amount. if (g <= MAX_GALLONS) gallons = g; gallons = MAX_GALLONS;
getgallons method @return The number of gallons of fuel. public int getgallons() return gallons; The incrementgallons method increments the value of gallons. If gallons exceeds the maximum amount, a message is displayed incicating the fuel is overflowing. public void incrementgallons() if (gallons < MAX_GALLONS) gallons++; System.out.println("FUEL OVERFLOWING!!!"); The decrementgallons method decrements the value of gallons. If gallons is at 0 then a message is displayed indicating we are out of fuel. public void decrementgallons() if (gallons > 0) gallons--; System.out.println("OUT OF FUEL!!!"); Odometer class public class Odometer // Constant for the maximum mileage public final int MAX_MILEAGE = 999999; // Constant for the miles-per-gallon public final int MPG = 24; // Field for the current mileage private int mileage; // Field for the mileage set-point to // remember when the FuelGuage gallons // were decremented. private int setpoint; // Field to reference a FuelGauge object private FuelGauge fuelgauge; Constructor @param m Initial mileage.
@param fg A reference to a FuelGauge object. public Odometer(int m, FuelGauge fg) mileage = m; setpoint = m; fuelgauge = fg; getmileage method @returns The mileage. public int getmileage() return mileage; The incrementmileage method increments the mileage field. If mileage exceeds the maximum amount, it rolls over to 0. public void incrementmileage() // Increment the mileage, but rollover // if we go past the maximum amount. if (mileage < MAX_MILEAGE) mileage++; // we are rolling over the odometer back to 0 mileage = 0; setpoint = setpoint MAX_MILEAGE; // change setpoint to refuel right // See if we have burned a gallon of gas. This // happens every MPG miles. // if ( (mileage - setpoint) >= MPG ) fuelgauge.decrementgallons(); setpoint = mileage;