Create an instance of the class circle passing value five

Assignment Help JAVA Programming
Reference no: EM131917229

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.

Reference no: EM131917229

Questions Cloud

Evaluation of the articles effectiveness and credibility : MGMT: Critical Thinking and Managerial Decision Making - Assessment: Practical and Written - Based upon the article is there a gap in the literature or some
What steps were taken to overcome the cannibalization : In your own words, identify two corporations that have dealt with cannibalization and what steps were taken to overcome the cannibalization.
Retained earnings versus new common stock : Calculate the cost of retained earnings and the cost of new common stock using the? constant-growth valuation model.
Before-tax cost of debt and after-tax cost of debt : Before-tax cost of debt and after-tax cost of debt-Calculate the after-tax cost of the Sony bond given the corporate tax rate.
Create an instance of the class circle passing value five : 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.
Weak organic acids : Barium hydroxide, often used to titrate weak organic acids, is obtained as the octahydrate, Ba(OH)2. 8H2O.
What is the maximum number of such jobs : What is the maximum number of such jobs that can be produced in a 40-hour week? What is the labor cost per job?
Compute the net present value of project : Compute the net present value of this project assuming a discount rate of 12%.
How the court was able to hold software distributors : Explain how the Court was able to hold software distributors like Grokster liable for the misconduct of others.

Reviews

Write a Review

JAVA Programming Questions & Answers

  Produce a java implementation of the gui

For your Assignment 2 Part 1 submission you are required to produce a Java implementation of the GUI for the Agriculture image viewer component of your overall Agriculture File Storing and Management (FSM) System.

  Create a class that represents a toaster

Create a class that represents a Toaster - Put the bread in the toaster - Turns off automatically

  Write a complete java program called recursivetriangle

Created a regular RecursiveTriangle (method calling itself) to create a Triangle using the number of lines and String I enter -

  What character you should use to create your output figure

The char will be a single character and indicate what character you should use to create your output figure. You will write a program that reads from one file, interprets what it reads and writes to a second fine.

  What are the diagrams defined in the uml standard

What are the diagrams defined in the UML Standard. Give a one or two sentence description of each one. Given the following code, how should the toString methods in the classes H2ClassA and H2ClassB be written to give the indicated output and take a..

  Write a java program to simulate a die

Write a Java program to simulate a die. A die has values of either 1, 2, 3, 4, 5 or 6 on the face. You should use the Math.Random() or the java.util.Random() class to generate the values on the die.

  Finish off the powerball application

Finish off the Powerball application - Enter the number into array - For each value of number

  Obtain a collection of n small images in png or jpg format

Obtain a collection of n small images in PNG or JPG format, where n is an integer between 3 and 10. Index the images with numbers 1,. . . n.

  What is the purpose of using javascript on a website

What is the purpose of using JavaScript(r) on a website? What is a specific example of a JavaScript(r) application that will be beneficial on the site you are creating

  Fundamentals of object-oriented programming

A successful career in software development depends on a thorough understanding of fundamentals of object-oriented programming and best practices for software

  Implements simulation for movie borrowing and return system

For this assignment you need to write a console application in the Java programming language which implements a simulation for a movie borrowing and return system for the video store MovieMaster. The staff at MovieMaster will need to be able to ma..

  Create a system to track the number of tickets available

Create a system to track number of tickets available and allow user to buy tickets. There are two type of tickets - VIP tickets and General Floor tickets.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd