JAVA CODING: 1. Consider the following class declaration: public class Circle {
ID: 3758220 • Letter: J
Question
JAVA CODING:
1. Consider the following class declaration:
public class Circle
{
private double radius;
public Circle(double r)
{
radius = r;
}
public double getArea()
{
return Math.PI * radius * radius;
}
public double getRadius()
{
return radius;
}
}
a. Write a toString method for this class. The method should return a string contain- ing the radius and area of the circle.
b. Write an equals method for this class. The method should accept a Circle object as an argument. It should return true if the argument object contains the same data as the calling object, or false otherwise.
c. Write a greaterThan method for this class. The method should accept a Circle object as an argument. It should return true if the argument object has an area that is greater than the area of the calling object, or false otherwise.
2. Consider the following class declaration:
public class Thing
{
private int x;
private int y;
private static int z = 0;
public Thing()
{
x = z; y = z;
}
static void putThing(int a)
{
z = a;
}
}
Assume a program containing the class declaration defines three Thing objects with the following statements: Thing Thing();
Thing two = new Thing();
Thing three = new Thing();
a. How many separate instances of the x member exist?
b. How many separate instances of the y member exist?
c. How many separate instances of the z member exist?
d. What value will be stored in the x and y members of each object?
e. Write a statement that will call the putThing method.
3. A pet store sells dogs, cats, birds, and hamsters. Write a declaration for an enumerated data type that can represent the types of pets the store sells.
Explanation / Answer
import java.io.*;
import java.util.*;
public class Circle{
private double radius;
public Circle(double r){
radius = r;
}
public double getArea(){
return Math.PI * radius * radius;
}
public double getRadius(){
return radius;
}
public String toString(){
return "Radius is "+radius+" and Area is "+area+' ';
}
public boolean equals(Circle c){
if (this.radius == c.radius)
return true;
return false;
}
public boolean greaterThan(Circle c){
if (this.radius < c.radius)
return true;
return false;
}
}
Question 2
a) How many separate instances of the x member exist?
Three, each for one class
b) How many separate instances of the y member exist?
Three, each for one class
c) How many separate instances of the z member exist?
Three, each for one class
d) What value will be stored in the x and y members of each object?
Zero as We call default Constructor, where z is 0
e) Write a statement that will call the putThing method.
int b = 5;
Thing four = new Thing(b);
Question 3
public enum Pets{
dogs, cats, birds, hamsters;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.