Foundations of programming using Java

Homework#7

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper

COSC 4304

Submission Instructions

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper

a) Programming Assignment
1) Design a class named Fan that contains:

– Three constants named SLOW, MEDIUM, and FAST with the values 1, 2
and 3 to denote the fan speed.

– A private int data field named speed that specifies the speed of the fan (the
default is SLOW).

– A private boolean data field named on that specifies whether the fan is on (the
default is false).

– A private double data field named radius that specifies the radius of the fan
(the default is 5).

– A string data field named color that specifies the color of the fan (the default
is blue).

– The accessor (get) and mutator (set) methods for all four data fields.
– A no-arg constructor that creates a default fan.
– A method named toString() that returns a string description for the fan. If the

fan is on, the method returns the fan speed, color, and radius in one combined

string. If the fan is not on, the method returns the fan color and radius along

with the string “fan is off” in one combined string.

Draw the UML diagram for the class and then implement the class. Write a test

program that creates two Fan objects. Assign maximum speed, radius 9, color

yellow, and turn it on to the first object. Assign medium speed, radius 4, color blue,

and turn it off to the second object. Display the objects by invoking their toString

method in your main method (not part of the Fan class).

Run your program. Your program must run successfully to receive full credit for

this homework. Take a screenshot of your program. The screenshot should show all

of your source code and the output of your program in the Console window. Resize

the editor and/or console windows as needed so that everything is shown. You may

also need to expand Eclipse to full screen size. If you program is too large you can

take multiple screenshots.

b) Questions

2) In the following code, radius is private in the Circle class, and myCircle is an
object of the Circle class. Does the highlighted code cause any problems? If so,

explain why.

public class Circle {

private double radius = 1;

/** Find the area of this circle */

public double getArea() {

return radius * radius * Math.PI;

}

public static void main(String[] args) {

Circle myCircle = new Circle();

System.out.println(“Radius is ” + myCircle.radius);

}

}

3) Show the output of the following program:

public class Test {

public static void main(String[] args) {

Circle circle1 = new Circle(1);

Circle circle2 = new Circle(2);

swap1(circle1, circle2);

System.out.println(“After swap1: circle1 = ” +

circle1.radius + “circle2 = ” + circle2.radius );

swap2(circle1, circle2);

System.out.println(“After swap2: circle1 = ” +

circle1.radius + ” circle2 = ” + circle2.radius);
}

public static void swap1(Circle x, Circle y) {

Circle temp = x;

x = y;

y = temp;
}

public static void swap2(Circle x, Circle y) {

double temp = x.radius;

x.radius = y.radius;

y.radius = temp;

}
}

class Circle {

double radius;

Circle(double newRadius) {

radius = newRadius;

}
}

4) What is wrong in the following code?

public class Test {

private int id;

public static void m1() {

this.id = 45;

}

public void m2() {

Test.id = 45;

}
}

Also, check the above code if the 2 lines below are changed as follows:
a)

private static int id;

public void m1()

b)
private int id;

public void m1()

5) Is the following class immutable?
public class A {

private int[] values;

public int[] getValues() {

return values;

}

}

Your submission for the Programming Assignment will consist of the following:

 Source code .java files.
 Screenshot of Eclipse showing output of program.
 UML diagrams.

Your submission for the Questions will consist of the either of the following:

 Include the related question, its answer and the required explanation in a “doc”
file.

Include your name and the title of the homework assignment as comments at the top of

your program.

Calculate your order
Pages (275 words)
Standard price: $0.00
Client Reviews
4.9
Sitejabber
4.6
Trustpilot
4.8
Our Guarantees
100% Confidentiality
Information about customers is confidential and never disclosed to third parties.
Original Writing
We complete all papers from scratch. You can get a plagiarism report.
Timely Delivery
No missed deadlines – 97% of assignments are completed in time.
Money Back
If you're confident that a writer didn't follow your order details, ask for a refund.

Calculate the price of your order

You will get a personal manager and a discount.
We'll send you the first draft for approval by at
Total price:
$0.00
Power up Your Academic Success with the
Team of Professionals. We’ve Got Your Back.
Power up Your Study Success with Experts We’ve Got Your Back.

Order your essay today and save 30% with the discount code ESSAYHELP