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

  Recursive factorial program

Write a class Array that encapsulates an array and provides bounds-checked access. Create a recursive factorial program that prompts the user for an integer N and writes out a series of equations representing the calculation of N!.

  Hunt the wumpus game

Reprot on Hunt the Wumpus Game has Source Code listing, screen captures and UML design here and also, may include Javadoc source here.

  Create a gui interface

Create GUI Interface in java programing with these function: Sort by last name and print all employees info, Sort by job title and print all employees info, Sort by weekly salary and print all employees info, search by job title and print that emp..

  Plot pois on a graph

Write a JAVA program that would get the locations of all the POIs from the file and plot them on a map.

  Write a university grading system in java

University grading system maintains number of tables to store, retrieve and manipulate student marks. Write a JAVA program that would simulate a number of cars.

  Wolves and sheep: design a game

This project is designed a game in java. you choose whether you'd like to write a wolf or a sheep agent. Then, you are assigned to either a "sheep" or a "wolf" team.

  Build a graphical user interface for displaying the image

Build a graphical user interface for displaying the image groups (= cluster) in JMJRST. Design and implement using a Swing interface.

  Determine the day of the week for new year''s day

This assignment contains a java project. Project evaluates the day of the week for New Year's Day.

  Write a java windowed application

Write a Java windowed application to do online quiz on general knowledge and the application also displays the quiz result.

  Input pairs of natural numbers

Java program to input pairs of natural numbers.

  Create classes implement java interface

Interface that contains a generic type. Create two classes that implement this interface.

  Java class, array, link list , generic class

These 14 questions covers java class, Array, link list , generic class.

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