Create an instance of the class circle passing the value 50


Assignment: Fundamentals of Computer Science

Java Classes, Methods and Parameter Passing

1. Consider the following complete Java class. In Java, PI is the public named constant π (Pi = 3.14159...) and is defined in the Math class. Hence, we refer to it as Math.PI.

public class Circle
{
private double radius;
public Circle (double r)
{
radius = r;
}
public double getArea()
{
double result;
result = Math.PI * radius * radius;
return result;
}
public String toString()
{
return "Circle: radius = " + radius;
}
}

Does this class include an instance variable? If so, what is its name? What type is it?

Do any of the methods have formal parameter(s)? If so, name the method and the formal parameter(s) it has.

Do any of the methods have local variable(s)? If so, name the method and the local variable(s) it has.

Declare a variable named circle1 of Circle type, so it can hold a reference to an object of the Circle class.

Create an instance of the class Circle, passing the value 5.0 as the actual parameter, and assign the reference to the object created to the variable circle1.

Write one print statement that displays the returned value of the toString method when invoked on the circle1 object.

2. Consider the Circle class presented in the question above.

Add a new method named getPerimeter with appropriate return type, but no parameters. The method returns the perimeter of the circle, computed as 2 * π * radius.

Add a getter method named getRadius to the class.

Add a setter method named setRadius, which takes one parameter named radius to assign a new value for the instance variable radius. It returns no value.

Consider the following lines of code in the main method of a class CircleTester:

Circle circle10, circle20;
double radius, perimeter;
radius = 15.00;
circle20 = new Circle (radius + 5.0);
perimeter = circle20.getPerimeter();

In the fourth line of code, a Circle object is being created by invoking the constructor method for the Circle class. Trace the execution of this line of code:

What value is passed as the actual parameter to the constructor?

What value is assigned to formal parameter named r in the constructor?

What does the constructor code do?

What is assigned to the variable circle20?

What value is assigned to perimeter in the fifth line?

3. You need to consult Java SE8 API and answer the following questions.

The Integer class has a static method named parseInt. One version of parseInt parses the string argument as a signed integer in the radix (or base) specified by the second argument.

Suppose hex is a hexadecimal number string (for example, "1A0"). decimalValue is an int variable. We wish to determine the decimal value of the hexadecimal string hex and assign the value to the variable decimalValue. Write an assignment statement to invoke the parseInt method with appropriate arguments (parameters) and assign the retuned value to decimalValue.

The Math class has several useful static methods for mathematical computation. Locate one named sqrt and read its description. Consider four variables a, b, c, and d of type double. Write an assignment statement to compute the square root of the expression b2 - 4ac and assign the result to d. (Use appropriate Java operators. No need to use the pow method to compute b2. Use multiplication instead.)

The String class has a method named indexOf that returns the index within this string of the first occurrence of the specified substring parameter. Given a String str, we wish to determine if starts with the substring "The" and print either "Yes, it starts with The" or "No, it does not start with The". Invoke the indexOf method suitably, and use the returned value to print an appropriate message.

4. Consider the following complete Java class.

public class Die
{
private int faceValue; // valid range 1-6
public Die ()
{
this.faceValue = 3; // arbitrary value
}
public void roll ()
{
double r;
r = Math.random(); // Generate a random r, 0 <= r < 1
faceValue = (int) (6 * r) + 1;
}
public int getFaceValue ()
{
return faceValue;
}
public boolean isAce()
{
return (faceValue == 1);
}
}

What is the name of the class? How many methods (other than the constructor) does it have? Name the methods.

Does this class include an instance variable? If so, what is it? What type is it?

Do any of the methods have formal parameter(s)? If so, name the method(s) and the formal parameter(s) it has (they have).

Do any of the methods have local variable(s)? If so, name the method(s) and the local variable(s) it has (they have).

Do any of the methods in this class invoke another method? If so, name the method that is invoked and the method invoking it.

5. Consider the class presented in Question 5.

Declare a variable named die1 of type Die, so that the variable can hold a reference to an object of the Die class. Initialize the variable to null.

Create an instance of the class Die, passing appropriate parameter(s), and assign the reference to the object created to the variable die1.

When a Die object is first created, what face does it show? See the code and the comments in the Die class.

Write one Java statement to invoke the method roll on the newly created die1 object.

Write one Java statement to invoke the method isAce on the die1 object and assign the returned value to a variable. Be sure to first declare the variable with suitable name and type.

6. We are interested in developing a class named Car that contains three instance variables: make and color, both of which are of type String, and year of type int. The constructor accepts three parameters to initialize the make, color and year. Write Java code for a fully encapsulated Car class showing the instance variables, the constructor method and three getter methods.

Request for Solution File

Ask an Expert for Answer!!
JAVA Programming: Create an instance of the class circle passing the value 50
Reference No:- TGS02712708

Expected delivery within 24 Hours