Mid term
9.1
public class MyRec2 {
private double width;
private double height;
public MyRec2() {
this.width = 1.0;
this.height = 1.0;
public MyRec2(double width, double height) {
this.width = width;
this.height = height;
/** return area **/
public double getArea() {
return this.width * this.height;
/** return perimeter **/
public double getPerimeter() {
return this.width * 2 + this.height * 2;
}
}
public class Exercise_01 {
public static void main(String[] args) {
MyRec2[] rectangles = new MyRec2[2];
rectangles[0] = new MyRec2(4,40);
rectangles[1] = new MyRec2(3.5, 35.9);
for (int i = 0; i < 2; i++){
System.out.println("MyRec2 " + (i+1) +":");
System.out.print("Area = " + rectangles[i].getArea());
System.out.println(" Perimeter = " + rectangles[i].getPerimeter() +"\n");
9.2
stock
public class Stock {
private String symbol;
private String name;
private double previousClosingPrice;
private double currentPrice;
public Stock(String symbol, String name) {
this.symbol = symbol;
this.name = name;
public String getSymbol() {
return symbol;
public void setSymbol(String symbol) {
this.symbol = symbol;
public String getName() {
return name;
public void setName(String name) {
this.name = name;
public double getCurrentPrice() {
return currentPrice;
public void setCurrentPrice(double currentPrice) {
this.previousClosingPrice = this.currentPrice;
this.currentPrice = currentPrice;
public double getPreviousClosingPrice() {
return previousClosingPrice;
public void setPreviousClosingPrice(double previousClosingPrice) {
this.previousClosingPrice = previousClosingPrice;
public double getChangePercent() {
return (currentPrice - previousClosingPrice) / previousClosingPrice;
public class Exercise_02 {
public static void main(String[] args) {
Stock stock1 = new Stock("ORCL", "Oracle Corporation");
stock1.setCurrentPrice(34.5);
stock1.setCurrentPrice(34.35);
System.out.println("Stock name: " + stock1.getName() + " Symbol: " + stock1.getSymbol());
System.out.println("previous price: " + stock1.getPreviousClosingPrice());
System.out.println("current price: " + stock1.getCurrentPrice());
System.out.println("Percent changed: " + stock1.getChangePercent());
9.4
package Chapter_09;
import java.util.Random;
public class Exercise_04 {
public static void main(String[] args) {
Random random = new Random(1000);
for (int i = 0; i < 50; i++) {
System.out.printf("%3d ", random.nextInt(101)); // displays 1-99
if ((i + 1) % 10 == 0) {
System.out.println();
9.5
ackage Chapter_09;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class Exercise_05 {
public static void main(String[] args) {
// current date
System.out.println(new Date().toString());
// display year month day using 1234567898765L from gregorian class
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTimeInMillis(1234567898765L);
// display the year, month, and day
System.out.printf("Year: %d Month: %d Day: %d",
calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE));
9.8
public class Fan {
public static final int SLOW = 1;
public static final int MEDIUM = 2;
public static final int FAST = 3;
private int mSpeed;
private boolean mOn;
private double mRadius;
private String mColor;
public Fan() {
mSpeed = 1;
mRadius = 5;
mColor = "white";
public int getSpeed() {
return mSpeed;
public void setSpeed(int speed) {
mSpeed = speed;
public boolean isOn() {
return mOn;
public void setOn(boolean on) {
mOn = on;
public double getRadius() {
return mRadius;
public void setRadius(double radius) {
mRadius = radius;
public String getColor() {
return mColor;
public void setColor(String color) {
mColor = color;
public String toString() {
if (isOn()) {
return "Speed: " + getSpeed() + " Color: " + getColor() + " Radius: " + getRadius();
} else {
return "Color: " + getColor() + " Radius: " + getRadius() + ". The fan is OFF.";
public class Exercise_08 {
public static void main(String[] args) {
Fan fan1 = new Fan();
fan1.setSpeed(Fan.FAST);
fan1.setOn(true);
fan1.setRadius(10);
fan1.setColor("yellow");
Fan fan2 = new Fan();
fan2.setSpeed(Fan.MEDIUM);
fan2.setRadius(5);
fan2.setColor("blue");
fan2.setOn(false);
System.out.println("Fan #1: "+fan1.toString());
System.out.println("Fan #2: "+fan2.toString());
9.9
public class RegularPolygon {
private int mNumberOfSides;
private double mSideLength;
private double mCenterX;
private double mCenterY;
public RegularPolygon() {
mNumberOfSides = 3;
mSideLength = 1;
mCenterX = 0;
mCenterY = 0;
public RegularPolygon(int numberOfSides, double sideLength) {
this();
mSideLength = sideLength;
mNumberOfSides = numberOfSides;
public RegularPolygon(int numberOfSides, double sideLength, double x, double y) {
this(numberOfSides, sideLength);
mCenterX = x;
mCenterY = y;
public int getNumberOfSides() {
return mNumberOfSides;
public void setNumberOfSides(int numberOfSides) {
mNumberOfSides = numberOfSides;
public double getSideLength() {
return mSideLength;
public void setSideLength(double sideLength) {
mSideLength = sideLength;
public double getCenterY() {
return mCenterY;
public void setCenterY(double centerY) {
mCenterY = centerY;
public double getCenterX() {
return mCenterX;
public void setCenterX(double centerX) {
mCenterX = centerX;
public double getPerimeter(){
return mNumberOfSides * mSideLength;
public double getArea() {
return (mNumberOfSides * mSideLength * mSideLength) / (4.0 * Math.tan(Math.PI /
mNumberOfSides));
public class Exercise_09_09 {
/** Main method */
public static void main(String[] args) {
// Create three RegularPolygon objects
RegularPolygon regularPolygon1 = new RegularPolygon();
RegularPolygon regularPolygon2 = new RegularPolygon(6, 4);
RegularPolygon regularPolygon3 = new RegularPolygon(10, 4, 5.6, 7.8);
// Display perimeter and area of each object
System.out.println("\n--------------------------------------------------");
System.out.println("| Regular Polygon Objects | Perimeter | Area |");
System.out.println("--------------------------------------------------");
System.out.printf( "| Object# 1 |%8.2f |%6.2f |\n",
regularPolygon1.getPerimeter(), regularPolygon1.getArea());
System.out.printf( "| Object# 2 |%8.2f |%6.2f |\n",
regularPolygon2.getPerimeter(), regularPolygon2.getArea());
System.out.printf( "| Object# 3 |%8.2f |%6.2f |\n",
regularPolygon3.getPerimeter(), regularPolygon3.getArea());
System.out.println("--------------------------------------------------");