class Box
{
// Instance Variables
double length, ipsos;
double width, mikos;
double height, platos;
// Constructors
public Box ( double side )
{
width = side;
height = side;
length = side;
}
public Box ( double x , double y , double z)
{
platos = y;
ipsos = z;
mikos = x;
}
// Methods
double calculate(double praksi)
{
return 2 * ( width * height +
width * length +
height * length ) ;
}
double volume(double emvadon)
{
return platos * ipsos * mikos;
}
#Override
public String toString() {
return "Volume: " + volume(1) + "\n Calculate: " + calculate(1);
}
}
How can we create a class that inherits MyBox class Box and will be used only for cubes by writing a constructor to ensure the creation of a cube using the second constructor of Box that takes three arguments and in case of an error initializing the cube MyBox to print error message?
I am very new at java, so please explain as simple as possible.
I'm a little confused by why Box has two constructors, and six fields. I would have thought that one constructor and three fields would be enough. But assuming you keep the constructor with three parameters, you can call it from your Cube constructor like this.
The line starting super specifies which Box constructor the Cube constructor should call, and what to pass it. This guarantees that a Cube will always have all three dimensions equal.
public class Cube extends Box {
public Cube(double side) {
super(side, side, side);
}
}
Related
I created an interface 'Polygon' that stores the abstract methods: 'area' and 'perimeter'. However, I am not understanding how to effectively use the interface, when the classes that implement Polygon have different computations involved for calculating area and perimeter. In my opinion, I don't even need an interface 'Polygon' since it has no use in my code.
I've tried overriding the method 'area' in the Triangle class, but received the following error:
Triangle is not abstract and does not override abstract method area() in Polygon
since the Triangle area has constructors. I cannot modify the Polygon area method to have the same number of constructors needed for Triangle, because it will not then suit my Rectangle class.
public interface Polygon {
void area();
void perimeter();
}
class Triangle implements Polygon{
private double triangleArea;
private double trianglePerimeter;
public Triangle (){};
public void area(){}; //I've tried overriding method here but get a
compiler error since it is not identical to the Polygon method.
public double area(double base, double height){
triangleArea = base * height * (.5);
return triangleArea;
}
public class Project25 {
public static void main(String[] args) {
Triangle testTriangle = new Triangle();
testTriangle.area(2, 2);
testTriangle.printArea();
I've managed to obtain the answers I need in my code i.e. area and perimeter, but I need to know how to modify my code to utilize inheritance and polymorphism.
The purpose of an interface is to be an abstract view of the common features of the objects.
In case of polygons, you've already identified some things they have on common, i.e. all polygons have an area, and they all have a perimeter.
The abstract view would be to get those common values:
public interface Polygon {
double getArea();
double getPerimeter();
}
Of course, you can only get those values if the polygon is fully defined, e.g. for a triangle the base and the height might be enough to calculate the area, but not the perimeter. Instead, you usually use the lengths of the 3 sides.
public class Triangle implements Polygon {
private final double a;
private final double b;
private final double c;
public Triangle(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
#Override
public double getArea() {
// Using Heron's Formula
double p = (a + b + c) / 2;
return Math.sqrt(p * (p - a) * (p - b) * (p - c));
}
#Override
public double getPerimeter() {
return a + b + c;
}
}
A rectangle is even easier.
public class Rectangle implements Polygon {
private final double width;
private final double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
#Override
public double getArea() {
return width * height;
}
#Override
public double getPerimeter() {
return (width + height) * 2;
}
}
As you can see, since the inputs for calculating area are different, depending on the type of polygon, the area method cannot take a common set of parameters, therefore they must be embedded in the polygon object, so we can get a common area method.
I think,
Methods in Polygon interface should return values (area and perimeter).
Each shape class that implements Polygon should have its own fields and appropriate constructor to initialize them. (length and height for example), which will be used for implementation of area and perimeter.
So,
Class Triangle should have a constructor,
public Triangle (final Double base, final Double height) {
this.base = base;
this.height = height;
};
And your area method should be like,
#Override
public Double area() {
return base * height * (0.5d);
}
And you should use it something like below,
final Polygon polygon = new Triangle(13, 212);
final Double area = polygon.area();
final Double perimeter = polygon.perimeter();
The first problem I see is that you didn't declare the perimeter method in the class that implements the interface, so that would cause a compiler error right there. As to what others said, you're returning a value on both of those methods, so they shouldn't be void methods.
Also, why did you use an overloaded method in the Triangle class?
I am having a problem with abstract classes, the code runs but it won't take any value , it prints "0.0" everywhere, I'm thinking it is an acces problem.I have to make an abstract base class called "Point" where i declare the 3 coordinates of a point, then i have to calculate the area , volume and center point of a cube and a sphere .Also if something doesn't look good, needs formating, or don't understand a word, please tell me.Thank you.
//this is the base class
public abstract class Punct
{
public double x,y,z;
Punct(double x,double y, double z)
{
this.x=x;
this.y=y;
this.z=z;
}
public double getX()
{
return x;
}
public double getY()
{
return y;
}
public double getZ()
{
return z;
}
}
Then the sphere
//This is the sphere class that extends the Point class
public class Sfera extends Punct
{
private double aria,volumul,raza,centrul;
Sfera(double x, double y, double z,double aria,double volumul,
double centrul,double raza)
{
super(x, y, z);
this.aria=aria;
this.volumul=volumul;
this.centrul=centrul;
this.raza=raza;
}
public double getRaza(Punct p1,Punct p2)
{
raza=Math.sqrt(Math.pow(p2.x-p1.x,2)+Math.pow(p2.y-p1.y, 2)+Math.pow(p2.z-p1.z, 2));
return raza;
}
public double getAria()
{
aria=4*Math.PI*raza*raza;
return aria;
}
public double getVolumul()
{
volumul=4/3*Math.PI*raza*raza*raza;
return volumul;
}
public double getCentrul(Punct p1)
{
return centrul;
}
}
The Cube
//this is the cube
public class Cub extends Punct
{
double latura,aria,volumul,centrul;
Cub(double x, double y, double z,double latura, double aria,double volumul, double centrul)
{
super(x, y, z);
this.latura=latura;
this.aria=aria;
this.volumul=volumul;
this.centrul=centrul;
}
public double getLatura(Punct p1,Punct p2)
{
latura=Math.sqrt(Math.pow(p2.x-p1.x,2)+Math.pow(p2.y-p1.y, 2)+Math.pow(p2.z-p1.z, 2));
return latura;
}
public double getAria()
{
aria=6*Math.pow(latura, 2);
return aria;
}
public double getVolumul()
{
volumul=latura*latura*latura;
return volumul;
}
}
And the Test Class where i have the main
//this is the Test class
public class TestTema
{
public static void main(String[] args_)
{
Punct p1=new Punct(2, 2, 2) {};
Sfera obSfera=new Sfera(1,2,3,1,1,1,1);
System.out.println("Raza sfera:" + obSfera.getRaza(p1,p1));
System.out.println("Aria sfera=" + obSfera.getAria());
System.out.println("Volum sfera= " +obSfera.getVolumul());
// System.out.println("Centrul= "+obSfera.getSuprafata());
Cub obCub=new Cub(1,2,3,1,1,1,1);
System.out.println("Lungime latura cub:"+obCub.getLatura(p1,p1));
System.out.println("Aria cubului="+obCub.getAria());
System.out.println("Volumul cubului="+obCub.getVolumul());
// System.out.println("Suprafata="+obCub.getCentrul());
}
}
Your design is really wrong. Sfera has a method getRaza() that computes the distance between two points. There's no reason to use a Sphere to compute the distance betwwen two points. This should be an instance method of Punct, that should take another point as argument.
But there's worse: instead of just computing the distance between two points, it stores this distance in the Sphere, overwriting its previous raza (not sure why a sphere has a distance):
raza=Math.sqrt(Math.pow(p2.x-p1.x,2)+Math.pow(p2.y-p1.y, 2)+Math.pow(p2.z-p1.z, 2));
You made the same mistake in several other methods: get methods should not change the state of the object.
Now, let's see your code. You start by calling
obSfera.getRaza(p1,p1)
That computes the distance between p1 and itself, so the answer is 0, and this distance is stored in the Sphere (as explained above). So, after this line of code, you've set the sphere's raza to 0. You then execute
obSfera.getAria()
and this method does
aria=4*Math.PI*raza*raza;
return aria;
so, once again, instead of just returning the area of the sphere, it overwrites its area with the computed value, which is 0 since you've set raza to 0 before.
Here's how a sphere class could look like:
public class Sphere {
private final double radius;
public Sphere(double radius) {
this.radius = radius;
}
public double getArea() {
return 4 * Math.PI * this.radius * this.radius;
}
public double getVolume() {
return (4.0 / 3) * Math.PI * this.radius * this.radius * this.radius;
}
}
Key points:
its fields are final: they can't change.
there is no need to store the volume and the area as fields, since they are derived from the radius
getters don't try to modify the object. They just compute a value and return it.
You are using the same point (p1) to calculate raza, thus replacing the value you used in the constructor. And then you used raza to calculate the other values in the Sfera class.
In the Cub class you are doing the same, so the programm is doing what it is supposed to do. If you calculate those things between the point and itself it will give you 0.
As a result the values you used in your constructor where overwritten by your methods.
System.out.println("Raza sfera:" + obSfera.getRaza(p1,p1));
Sorry, I can't translate what is Raza, but this method calculates distance between two points.
You pass into getRaza the same point. Distance between two identical points is zero. That is why getRaza returns zero.
You assign distance between points to internal field Sfera::raza inthe method getRaza
public double getRaza(Punct p1,Punct p2)
{
raza=Math.sqrt(Math.pow(p2.x-p1.x,2)+Math.pow(p2.y-p1.y, 2)+Math.pow(p2.z-p1.z, 2));
return raza;
}
So after calling obSfera.getRaza(p1,p1) the field obSfera.raza equals to zero.
All other methods use obSfera.raza in their formulas:
public double getAria()
{
aria=4*Math.PI*raza*raza;
return aria;
}
public double getVolumul()
{
volumul=4/3*Math.PI*raza*raza*raza;
return volumul;
}
Put raza value into your formulas:
volumul = 4/3*Math.PI*raza*raza*raza = 4/3*Math.PI*0*0*0 = 0
aria = 4*Math.PI*raza*raza = aria=4*Math.PI*0*0
The same is happening with the object obCub
create another object for class Punct and rewrite the code like below:
public class TestTema
{
public static void main(String[] args_)
{
Punct p1=new Punct(2, 2, 2) {};
Punct p2=new Punct(3, 3, 3) {};
Sfera obSfera=new Sfera(1,2,3,1,1,1,1);
System.out.println("Raza sfera:" + obSfera.getRaza(p1,p2));
System.out.println("Aria sfera=" + obSfera.getAria());
System.out.println("Volum sfera= " +obSfera.getVolumul());
// System.out.println("Centrul= "+obSfera.getSuprafata());
Cub obCub=new Cub(1,2,3,1,1,1,1);
System.out.println("Lungime latura cub:"+obCub.getLatura(p1,p2));
System.out.println("Aria cubului="+obCub.getAria());
System.out.println("Volumul cubului="+obCub.getVolumul());
// System.out.println("Suprafata="+obCub.getCentrul());
}
}
classes Cub and Sfera both refering the same object p1, so the distance value Math.pow(p2.x-p1.x,2) always be 0.
I am creating a circle class program that checks whether circles have overlapped each other. There are six initial given methods: getter methods for x,y, and radius and setter methods for x,y, and radius(6). There are two additional given methods: getArea method (returns the area's value) and doesOverLap method (returns which circles overlap or do not overlap). There are many demos within this source folder, but the following is the bulk (excuse indentation):
public class MyCircle
{
private double x;
private double y;
private double radius;
private double other_x; //keeps user input for other_x
private double other_y; //keeps user input for other_y
private double other_radius; //keeps user input for other_radius
private double third_x; //keeps user input for third_x
private double third_y; //keeps user input for third_y
private double third_radius; //keeps user input for third_radius
/*
The setX method stores a value in the x field.
# param value The value to store in x.
*/
public void setX(double value)
{
x = value;
}
/*
The setY method stores a value in the y field.
# param value The value to store in y.
*/
public void setY(double value)
{
y = value;
}
/*
The setRadius method stores a value in the radius field.
#param value The value to store in radius.
*/
public void setRadius(double value)
{
radius = value;
}
/*
The getX method returns x's value.
#return The value in the x field.
*/
public double getX()
{
return x; //returns value input for x
}
/*
The getY method return y's value.
#return The value in the y field.
*/
public double getY()
{
return y; //returns value input for y
}
/*
The getRadius method returns radius's value.
#return The value in the radius field.
*/
public double getRadius()
{
return radius; //returns value input for radius
}
/*
The getArea method returns the circle object's area.
#return The product of 3.14159 * radius * radius (area = 3.14159(pie) * radius * radius).
*/
public double getArea()
{
return radius * radius * 3.14159;
}
public boolean doesOverlap(MyCircleTest OtherCircleTest)
{
double distance;
distance = Math.sqrt(Math.pow(other_x - x, 2) + Math.pow(other_y - y, 2));
return distance < other_radius + radius;
if(distance < other_radius + radius)
{
System.out.println("My other circle overlaps my circle");
}
else
{
System.out.println("My other circle does not overlap my circle");
}
}
/*
The setOtherX method stores a value in the x field.
# param value The value to store in x.
*/
public void setOtherX(double value)
{
other_x = value;
}
/*
The setOtherY method stores a value in the y field.
# param value The value to store in y.
*/
public void setOtherY(double value)
{
other_y = value;
}
/*
The setOtherRadius method stores a value in the radius field.
#param value The value to store in radius.
*/
public void setOtherRadius(double value)
{
other_radius = value;
}
/*
The getOtherX method returns x's value.
#return The value in the x field.
*/
public double getOtherX()
{
return other_x; //returns value input for x
}
/*
The getY method return y's value.
#return The value in the y field.
*/
public double getOtherY()
{
return other_y; //returns value input for y
}
/*
The getRadius method returns radius's value.
#return The value in the radius field.
*/
public double getOtherRadius()
{
return other_radius; //returns value input for radius
}
/*
The getArea method returns the circle object's area.
#return The product of 3.14159 * radius * radius (area = 3.14159(pie) * radius * radius).
*/
public double getOtherArea()
{
return other_radius * other_radius * 3.14159;
}
public boolean doesOverlap(MyCircleTest OtherCircleTest)
{
//Equation to see whether circles overlap
double distance_2;
distance_2 = Math.sqrt(Math.pow(other_x - third_x, 2) + Math.pow(other_y - third_y, 2));
return distance < other_radius + third_radius;
if(distance_2 < other_radius + third_radius)
{
System.out.println("My other circle overlaps my third circle");
}
else
{
System.out.println("My other circle does not overlap third circle");
}
}
public void setThirdX(double value)
{
third_x = value;
}
/*
The setThirdY method stores a value in the third y field.
# param value The value to store in third y.
*/
public void setThirdY(double value)
{
third_y = value;
}
/*
The setThirdRadius method stores a value in the third radius field.
#param value The value to store in third radius.
*/
public void setThirdRadius(double value)
{
third_radius = value;
}
/*
The getThirdX method returns third x's value.
#return The value in the third x field.
*/
public double getThirdX()
{
return third_x; //returns value input for third x
}
/*
The getY method return third y's value.
#return The value in the third y field.
*/
public double getThirdY()
{
return third_y; //returns value input for third y
}
/*
The getThirdRadius method returns third radius's value.
#return The value in the third radius field.
*/
public double getThirdRadius()
{
return third_radius; //returns value input for third radius
}
/*
The getArea method returns the circle object's area.
#return The product of 3.14159 * radius * radius (area = 3.14159(pie) * radius * radius).
*/
public double getThirdArea()
{
return third_radius * third_radius * 3.14159;
}
public boolean doesOverlap (MyCircleTest ThirdCircleTest)
{
//Equation to see whether circles overlap
double distance_3;
distance_3 = Math.sqrt(Math.pow(third_x - x, 2) + Math.pow(third_y - y, 2));
return distance_3 < third_radius + radius;
if(distance_3 < third_radius + radius)
{
System.out.println("My third circle overlaps circle");
}
else
{
System.out.println("My third circle does not overlap circle");
}
}
}
I am having trouble on the final method, doesOverLap method, which should state the overall results of the program. It should state whether or not two circles overlap. Also, I am supposed to display the area values of each circle, which isn't in my code.
Requirements are the following:
There are three circle objects, two of them should overlap and two of them should not. The areas of the three circles are to be displayed. The doesOverLap method should indicate which circles over lap and which do not. Any help will be greatly appreciated.
Hinted Given: two circles overlap if the sum of their radius' is greater than the distance between their centers.
First of all, as #JF Meier pointed out in the comments, this is not the OO approach. You should have a class Circle which contains data and logic relevant for only a single circle and then create multiple instances of the Circle class with which you can work.
Also, I don't know if this code template was provided or created by yourself, but the initial data should be passed through a constructor rather than having only setters. Which makes sense, because a circle should be immutable in my opinion.
For example:
public class Circle {
private final double x;
private final double y;
private final double radius;
public Circle(double x, double y, double radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
}
With this, you need only one variant of getArea() and doesOverlap(Circle other) because now each of those methods "belong" to one Circle object alone.
Now you can simply create new instances of multiple circles and use your methods on them.
BTW: If you have parameters in one of your methods like doesOverlap(Circle other) make sure you actually use other inside.
I'm currently undertaking a Java class (one of my final ones for my bachelor, yay) and I'm having a really difficult time trying to understand classes and do this problem below. The textbook I'm currently using is quite confusing and I've tried to use other online resources to figure out what I'm doing wrong but I still seem stuck on the question below. Whenever I try and run the program all I get is 0.00.0 for my answer, is this due to myself incorrectly assigning values to cylinder1? Also, for the toString() class how do I even go about doing this? I'm always getting errors on converting doubles to Strings no matter what I can do.
Any help would be appreciated it.
Thanks.
Prompt
Implement the class called Cylinder shown in UML below. The constructor accepts and initializes the radius and height for the Cylinder, while accessors and mutators allow them to be changed after object construction. The class also include methods that calculate and return the volume and surface area of the Cylinder. Lastly, it contains a toString method that returns the name of the shape, its radius, and its height. Create a main method which instantiates 4 Cylinder objects (any parameters), display them with toString(), change one parameter (your choice) in each, and display them again. [15 points]
UML
Code
import java.util.*;
import java.text.*;
import java.io.*;
import java.lang.*;
class Cylinder
{
private double radius, height, area, volume;
public Cylinder(double height, double radius) {
radius = 0.0;
height = 0.0;
}
public double getRadius() {
return radius;
}
public double getHeight() {
return height;
}
public double getArea() {
double area = (2 * Math.PI * radius * height) + (2 * Math.PI * Math.pow(radius, 2));
return area;
}
public void setRadius(double r) {
radius = r;
}
public void setHeight(double h) {
height = h;
}
public double calcVolume() {
double volume = Math.PI * Math.pow(radius, 2) * height;
return volume;
}
public String toString (){
StringBuilder StBuild = new StringBuilder();
StBuild.append(radius).append(height);
return StBuild.toString();
}
public static void main(String[] args) {
Cylinder cylinder1 = new Cylinder(5, 5);
System.out.println(cylinder1);
}
}
Since this is obviously homework I won't give you the answers, but I'll try to explain a few things.
This:
public Cylinder(double height, double radius) {
radius = 0.0;
height = 0.0;
}
is a constructor. When you create an object (and instance of a class) you call this. You call it by doing:
Cylinder cylinder1 = new Cylinder(5, 5);
But what happens in your class? When you call the constructor are you really saving the values you want?
As for the toString method, you could either call the toString for the double (height.toString) or you could just do what I always end up doing which is just cheat by adding a string to it.
public String toString (){
return "Cylinder [ h: " + height + " - r: " + radius + " - v: " + calcValume() + "]";
}
in class Cylinder change the constructor to:
public Cylinder(double height, double radius) {
this.radius = radius;
this.height = height;
}
In void main() :
Cylinder cylinder1 = new Cylinder(5, 5);
System.out.println(cylinder1.calcVolume());
This will work.
But you should shift the main method to some other class.
In the constructor you are setting the radius and height to 0.0. Try:
public Cylinder(double height, double radius) {
this.radius = radius.
this.height = height;
}
The following code is what I'm having issues understanding:
public class Rectangle {
public Rectangle() {
double width = 1;
double height = 1;
}
public Rectangle(double w, double h) {
double width = w;
double height = h;
}
public double getArea(double w, double h) {
return (w*h);
}
public double getPerimeter(double w, double h) {
return ((2*w)+(2*h));
}
public static void main(String[] args) {
Rectangle oldRectangle = new Rectangle(4, 40);
Rectangle newRectangle = new Rectangle(3.5, 35.9);
double height1 = oldRectangle.height;
double height2 = newRectangle.height;
double width1 = oldRectangle.width;
double width2 = newRectangle.width;
System.out.println("Width of Rectangle 1 is: " + 4);
System.out.println("Height of Rectangle 1 is: " + 40);
System.out.println("Area of Rectangle 1 is: " + oldRectangle.getArea(4, 40));
System.out.println("Perimeter of Rectangle 1 is: " + oldRectangle.getPerimeter(4, 40));
System.out.println("Width of Rectangle 1 is: " + 3.5);
System.out.println("Height of Rectangle 1 is: " + 35.9);
System.out.println("Area of Rectangle 1 is: " + newRectangle.getArea(3.5, 35.9));
System.out.println("Perimeter of Rectangle 1 is: " + newRectangle.getPerimeter(3.5, 35.9));
}
}
I was instructed to create two constructors for a Rectangle class—one with no arguments but assigned a default value of 1 for both variables width and height. The second constructor was to contain parameters that would take in two doubles that would get assigned to their appropriate variable.
I was then told to create two 'get()' methods that returned their respective values—in my case, they were perimeter and area of said Rectangle.
I was then instructed to create two Rectangle instances, one with a width of 4 and a height of 40—and another with a width of 4.5 and a height of 35.9. So, I did just that and made two new Rectangle objects, as you can see.
Lastly, I was instructed to print out the Width, Height, Perimeter, and Area of both Rectangle objects. My issue is that I don't know of a way to reference them. I took a beginners tutorial class for JavaScript and if I'm not mistaken, I recall there was a way to reference a property value of an object by assigning it to a variable. Again, I'm saying "If I'm not mistaken", so I could be wrong. It's been a while...
I do realize that Java and Java Script are entirely different things in their own right. Java Script was a scripting language developed and based off of Java.
Anyway, any help will be grand.
Please feel free to help me understand how I can implement what I'm trying to do by giving examples. You don't have use my exact code, but I'd like to be able to get my code to make more sense.. I'm using Eclipse btw.
You are on the right track. What you are looking to do is something like this:
public double getArea(Rectangle r){
return r.width*r.height;
}
public double getPerimeter(Rectangle r){
return (2*r.width + 2*r.height);
}
For the print statements you are hard-coding in values which you dont have to do.
System.out.println("Width of Rectangle 1 is: " + oldRectangle.width);
System.out.println("Height of Rectangle 1 is: " + oldRectangle.height);
System.out.println("Area of Rectangle 1 is: " + getArea(oldRectangle));
System.out.println("Perimeter of Rectangle 1 is: " + getPerimeter(oldRectangle));
My issue is that I don't know of a way to reference them
As Java is a strong-typed language, to store the references to each Rectangle (or any object) attribute, you must create variables that match the field.
First, you need your rectangle have attributes:
public class Rectangle {
public double width;
public dobule height;
// constructors here...
After, methods to calculate area and perimeter:
public double getArea() {
return (width*height);
}
public double getPerimeter() {
return ((2*width)+(2*height));
}
Now, to reference them (i guess you still mean area and perimeter):
double area1 = getArea(newRectangle);
double area2 = getArea(oldRectangle);
and
double perimeter1 = getPerimeter(newRectangle);
double perimeter2 = getPerimeter(oldRectangle);
There are no width and height properties in your class, they are only defined inside the constructor, declare them in the class and not inside the constructors so you can acces them like this:
public class Rectangle {
double width;
double height;
public Rectangle() {
width = 1;
height = 1;
}
public Rectangle(double w, double h) {
width = w;
height = h;
}
public double getArea(double w, double h) {
return (w*h);
}
public double getPerimeter(double w, double h) {
return ((2*w)+(2*h));
}
//....
}
Note: It's better to declare them as private and use getters and setters to acces them, take a look at Adding Setter and Getter Methods for further information.
EDIT:
In your case you don't need to pass parameters to your methods (because the calculation needs this rectangle width and height), just calculate them using your class fields, like this:
public double getArea() {
return (width*height);
}
public double getPerimeter() {
return ((2*width)+(2*height));
}
Here's what you need to do to acces your variables:
public static void main(String[] args) {
Rectangle oldRectangle = new Rectangle(4, 40);
Rectangle newRectangle = new Rectangle(3.5, 35.9);
//In the following you access all the object variables and methods
System.out.println("Width of Rectangle 1 is: " + oldRectangle.width);
System.out.println("Height of Rectangle 1 is: " + oldRectangle.height);
System.out.println("Area of Rectangle 1 is: " + oldRectangle.getArea());
System.out.println("Perimeter of Rectangle 1 is: " + oldRectangle.getPerimeter());
System.out.println("Width of Rectangle 1 is: " + newRectangle.width);
System.out.println("Height of Rectangle 1 is: " + newRectangle.height);
System.out.println("Area of Rectangle 1 is: " + newRectangle.getArea());
System.out.println("Perimeter of Rectangle 1 is: " + newRectangle.getPerimeter());
}
You can test the DEMO here.
And to answer your question about using constructor, calling the parametrized constructor like this:
Rectangle oldRectangle = new Rectangle(4, 40);
Is equivalent to:
// create the object
Rectangle oldRectangle = new Rectangle();
//And then assigning the values 4 and 40 to its width and height
oldRectangle.width=4;
oldRectangle.height=40;