Question: Create 3 classes called Circle, Rectangle, Sphere with attributes . Create classes named TestCircle, TestRectange whose main() method declares several objects. Use the setRadius() methods
Create 3 classes called Circle, Rectangle, Sphere with attributes.
Create classes named TestCircle, TestRectange whose main() method declares several objects. Use the setRadius() methods like setRadius(), getRadius(), setLength() etc.
to assign and extract separate values for the different objects. Display all the created objects. Your program should use METHODS
I tried Circle and Sphere so far but they are wrong
public class Circle{
// Save as "Circle.java"
// private instance variable, not accessible from outside this class
private double radius;
private String color;
// The default constructor with no argument.
// It sets the radius and color to their default value.
public Circle() {
radius = 1.0;
color = "red";
}
// 2nd constructor with given radius, but color default
public Circle(double r) {
radius = r;
color = "red";
}
// A public method for retrieving the radius
public double getRadius() {
return radius;
}
// A public method for computing the area of circle
public double getArea() {
return radius*radius*Math.PI;
}
}
----------------------------------------------------------------------------------------------------------------
import java.awt.*;
import java.util.Random;
public class Sphere{
private Matrix transform;
private Renderer renderer;
private Geometry geometry;
private double radius;
private Color color;
Random gen = new Random();
private Vec3 position, velocity, acceleration, rotation, scale;
}
public Sphere(Renderer r){
renderer = r;
geometry = new Geometry(20, 20);
geometry.buildSphere();
transform = new Matrix();
position = new Vec3(0, 0, 0);
velocity = new Vec3(0, 0, 0);
acceleration = new Vec3(0, 0, 0);
rotation = new Vec3(0, 0, 0);
scale = new Vec3(1, 1, 1);
color = new Color(Math.abs(gen.nextInt()) % 200, Math.abs(gen.nextInt()) % 200, Math.abs(gen.nextInt()) % 200);
}
public Sphere(Renderer r, double radius){
this(r);
this.radius = radius;
scale = new Vec3(radius, radius, radius);
}
public Sphere(Renderer r, int longi, int lati, double radius){
this(r, radius);
geometry = new Geometry(longi, lati);
geometry.buildSphere();
}
public void setVelocity(Vec3 v){
this.velocity = v;
}
public Vec3 getVelocity(){
return this.velocity;
}
public void setAccel(Vec3 v){
this.acceleration = v;
}
public Vec3 getAccel(){
return this.acceleration;
}
public void setPosition(Vec3 pos){
this.position = pos;
}
public Vec3 getPosition(){
return this.position;
}
public double getRadius(){
return this.radius;
}
public void rotate(Vec3 rot){
this.rotation = rot;
}
public void update(){
velocity = velocity.add(acceleration.mul(.01));
position = position.add(velocity.mul(.01));
}
public void draw(){
transform.identity();
transform.rotate(rotation);
transform.scale(scale);
transform.translate(position);
renderer.renderGeometry(geometry, transform, color);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
