Ask Java Expert


Home >> Java

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.

Java, Programming

  • Category:- Java
  • Reference No.:- M92755892

Have any Question?


Related Questions in Java

Chatbotscreate a small networked chat application that is

Chatbots Create a small, networked chat application that is populated by bots. Introduction On an old server park, filled with applications from the early days of the internet, a few servers still run one of the earliest ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

Assignment game prototypeoverviewfor this assessment task

Assignment: Game Prototype Overview For this assessment task you are expected to construct a prototype level/area as a "proof of concept" for the game that you have designed in Assignment 1. The prototype should function ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

In relation to javaa what is constructor the purpose of

(In relation to Java) A. What is constructor? the purpose of default constructor? B. How do you get a copy of the object but not the reference of the object? C. What are static variables and instance variables? D. Compar ...

Project descriptionwrite a java program to traverse a

Project Description: Write a java program to traverse a directory structure (DirWalker.java) of csv files that contain csv files with customer info. A simple sample in provided in with the sample code but you MUST will r ...

Fundamentals of operating systems and java

Fundamentals of Operating Systems and Java Programming Purpose of the assessment (with ULO Mapping) This assignment assesses the following Unit Learning Outcomes; students should be able to demonstrate their achievements ...

Assessment -java program using array of Assessment -JAVA Program using array of objects

Assessment -JAVA Program using array of objects Objectives This assessment item relates to the course learning outcomes as stated in the Unit Profile. Details For this assignment, you are required to develop a Windowed G ...

Applied software engineering assignment 1 -learning

Applied Software Engineering Assignment 1 - Learning outcomes - 1. Understand the notion of software engineering and why it is important. 2. Analyse the risk factors associated with phases of the software development lif ...

Retail price calculatorwrite a java program that asks the

Retail Price Calculator Write a JAVA program that asks the user to enter an item's wholesale cost and its markup percentage. It should then display the item's retail price. For example: (If an item's wholesale cost is 5. ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As