I already did my assignment with setters and getters (I did this with OOD) however I still don't understand what's the purpose of the two Rectangle methods and if ever I remove the empty Rectangle an error will prompt:
P.S. This is not the full code.
// private double length = 25.0;
private double width = 15.5;
public Rectangle(){
}
public Rectangle(double length, double width){
this.length = length;
this.width = width;
}
public void setDimension(double length,double width){
this.length = length;
this.width=width;
}
public double getLength(){
return length;
}
public double getWidth(){
return width;
}
public double area(){
return length * width;
}
public double perimeter(){
return 2 * (length + width);
}
public static void print(){
Rectangle rt = new Rectangle();
Box box = new Box();
System.out.println("The rectangle has a length of " + rt.getLength() + " and a width of " + rt.getWidth() );
System.out.println("The rectangle has an area of "+ rt.area());
System.out.println("The rectangle has a perimeter of "+ rt.perimeter());
box.print();
}
That's the default (no-arg) constructor. Since you have another constructor, Java will not implicitly create it if you don't define it explicitly. Since the first line in your print method calls it, you'll get an error if you remove it.
Related
So, I been trying to figured out how can I able draw the shape of a rectangle by given the height and the width on the main driver, but I was having trouble because somehow I couldn't able to draw the rectangle, instead I was able to put the values inside of the draw() methods.
For the draw() method was supposed to do this description: The draw
method must “draw” the rectangle using System.out.println(). Simply
print width number of asterisks in height number of lines.
The interface Shape represents a closed geometrical shape. It has
three methods.
1. draw(): This has no parameters and returns void. The method draws the shape on
the screen.
2. getArea(): The method has no parameters and returns the area of the shape. The
return type is double.
3. getPerimeter(): The method has no parameters and returns the perimeter of the
shape. The return type is double.
public class Rectangle implements Shape {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
#Override
public void draw() {
/**
* Don't want to use the initialize given height/weight
* Instead use the main driver
*
*/
double height = 3,width = 3;
for(int i=0; i<height;i++) {
for(int w =0; w<width; w++) {
System.out.print("*");
}
System.out.println(" ");
}
}
#Override
public double getArea() {
return width*height;
}
#Override
public double getPerimeter() {
return 2*(height+width);
}
public boolean equals(Object obj)
{
// check that the type of the parameter is Rectangle
if( !(obj instanceof Rectangle) )
return false;
// we already know that obj is of type Rectangle, so it's safe to cast
Rectangle rectangleObject = (Rectangle) obj;
// return true or false depending on whether length and width have the same value
return height == rectangleObject.height && width == rectangleObject.width;
}
public String toString() {
String info = "Area: " + getArea() + "\n" + "Perimeter:" + getPerimeter();
return info;
}
public static void main(String[] args) {
Shape rectangle1 = new Rectangle(5,5);
rectangle1.draw();
}
}
The output should look like:
Shape rectangle1 = new Rectangle(4, 5);
rectangle1.draw();
****
****
****
****
****
Use the height and width member variables that are part of the Rectangle object instance. In your example, the width would be 4 and the height would be 5 based one the object instantiation via:
Shape rectangle1 = new Rectangle(4, 5);
Your draw method would look like:
#Override
public void draw() {
for(int i=0; i<this.height;i++) {
for(int w =0; w<this.width; w++) {
System.out.print("*");
}
System.out.println(" ");
}
}
Everything compiles and works. The program is not reading the Volume value and comes out as 0.0
===========================================================================
Write a superclass encapsulating a rectangle. A rectangle has two attributes representing the width and the height of the rectangle. It has methods returning the perimeter and the area of the rectangle. This class has a subclass, encapsulating a parallelepiped, or box. A parallelepiped has a rectangle as its base, and another attribute, its length; it has two methods that calculate and return its area and volume. You also need to include a client class (with the main method) to test these two classes.
public class Rectangle
{
protected double width;
protected double height;
public Rectangle(double width, double height)
{
this.width = width;
this.height = height;
}
public double getWidth()
{
return width;
}
public void setWidth(double width)
{
this.width = width;
}
public double getHeight()
{
return height;
}
public void setHeight(double height)
{
this.height = height;
}
public double getArea()
{
return width * height;
}
public double getPerimeter()
{
return 2 * (width + height);
}
}
public class Box extends Rectangle
{
protected double length;
public Box(double length)
{
super(length, length);
}
public double getLength()
{
return length;
}
public void setLength(double length)
{
this.length = length;
}
public double getVolume()
{
return width * height * length;
}
}
public class TestRectangle
{
public static void main(String[] args)
{
Rectangle rectangle = new Rectangle(2,4);
Box box = new Box(5);
System.out.println("Rectangle:" );
System.out.println("The area is " + rectangle.getArea());
System.out.println("The perimeter is " +rectangle.getPerimeter());
System.out.println("The volume is " + box.getVolume());
}
}
No errors. Missing value for volume.
Your Box simply needs to set the length field.
public Box(double length)
{
super(length, length);
this.length = length;
}
Although, this assumes your Box is always going to be a cube, and not some arbitrary box shape.
The problem here is you are never assigning any value to the length variable of your box class. The current output of your code in your example would be the following for getVolume(): 5 * 5 * 0 which always returns 0.
Assuming your width, length, height are the same, you still need to assign the length to the variable:
public Box(double length)
{
super(length, length);
this.length = length;
}
I'm learning java on my own and I have this code that I'm trying to work through.
Here are the requirements:
Write a class Rectangle that has two double fields width and height, and a method void setDimensions(double w, double h) that assigns its parameter values to these fields.
Then, write the accessor (“getter”) methods double getArea() and double getPerimeter() that compute and return the area and the perimeter length of the rectangle, and a mutator (“setter”) method void scale(double sf) that multiplies the width and the height of the rectangle by the scaling factor sf.
Finish up by writing a proper toString method in your class.
public class Rectangle {
double width;
double length;
/**
*/
public void setDimensions(double width, double length)
{
width = 5;
length = 10;
}
/*
*/
public static double getPerimeter(double width, double length)
{
double perimeter = 2 * (width + length);
return perimeter;
}
/*
*/
public static double getArea(double width, double length)
{
double area = (width * length);
return area;
}
/**
*
*/
public void scale(double sf)
{
sf *= length;
sf *= width;
}
public String toString()
{
System.out.println("The length is: " + length);
System.out.println("The width is: " + width);
getArea(width, length);
getPerimeter(width, length);
return "The area is:" + area;
return "The perimeter is:" + perimeter;
}
}
With that being said, I am confused with how to format the toString method. Is this toString method simply supposed to print out the values of the area, perimeter, sf, etc? Or am I supposed to return a string of some sort(Sorry I am new I don't really understand completely)
Also, I don't think I have my SF method working as it should be. Should the method have SF initialized in the method?
Thanks
Usually, the toString method is a human readable string outputting the properties/values of your object.
#Override
public String toString() {
String myStr= "{length: " + getLength() + ", width: " + getWidth() +
", perimiter: " + getPerimiter() + ", area: " + getArea() + "}";
return myStr;
}
// will output something like "{length: 2, width: 3, perimiter: 10, area: 6}"
Also, the scale method should increase length and width by the factor provided as argument.
public void scale(double sf)
{
length *= sf;
width *= sf;
}
// better to create getters and setters and use these.
public void setLength(ln){
this.length = ln;
}
public double getLength(){
return this.length;
}
// same for width, then...
public void scale(double sf){
setLength(getLength()*sf);
setWidth(getWidth()*sf);
}
So, I have a twoparter question. The first part I did, which was creating an interface and then utilizing that interface in two different classes. However the second part of the question has me stumped. I'm not understanding the logic aspect of it. It feels as though the question order should be in reverse. This is two things I need to do to finish up the last portion of the assignment. If anyone here could give me just some tiny guidance as to to the direction I should be taking I'd greatly appreciate it.
Part II:
(1)Create an array of Calculatable objects in main and call the sumCalculate method.
For the most part I understand this, I already began doing it but decided to start working on (2) since that was way more difficult for me. It's basically creating an array of Calculatable objects of a certain size ( I chose 5) and populating it with different calculatable objects ( could be rectangles or squares in my example). But the second half of this question confuses me? Am I calling the sumCalculate method that I'm GOING to be making in question 2? Or am I calling it before I even make the (2) method.
(2)Make a method that accepts an array of Calculatable objects and sums up the values that are returned by each object's call to calculate.
What I'm trying to figure out here in question (2) is this. When it asks me to make the method? Does this mean that I'm making a new method in interface called sumCalc for example, that has parameters that accepts an array of calculatable objects? And then as far as summing up the values that are returned. I'd assume that I'd be adding the calculation double that is returned by calculate methods.
I'd ask my professor but this professor I decided to take has made it a habit of being excessively difficult to reach. Sorry to bother you guys with what is most likely an elementary and not difficult question.
interface Calculatable {
public double calculate(int x);
}
class square implements Calculatable {
public double side;
square(double side){
this.side = side;
}
public double getside(){
return side;
}
public double calculate(int x) {
double perimeter = side * 4;
System.out.println("This calculate method will output the perimeter divided by the parameter x");
double calculation = perimeter / x;
System.out.println("The original perimeter was " + perimeter + ". And the calculated perimeter is " + calculation +".");
return calculation;
}
}
public class rectangle implements Calculatable {
public double length;
public double width;
rectangle(double length , double width){
this.length = length;
this.width = width;
}
public double getlength(){
return length;
}
public double getwidth(){
return width;
}
public double calculate(int x) {
double perimeter = 2 * (length + width);
double calculation = 2 * perimeter;
System.out.println("This will return the perimeter of the rectangle times the x paramater");
System.out.println("Your current perimeter is " + perimeter + " and your perimeter after the calculation is " + calculation + ".");
return calculation;
}
public static void main(String [] args){
Calculatable perimeter1 = new rectangle(20.5 , 50);
perimeter1.calculate(5);
Calculatable perimeter2 = new square(10.5);
perimeter2.calculate(2);
Calculatable[] perimetersums = new Calculatable[5];
perimetersums[0] = new rectangle(20.5 , 50);
perimetersums[1] = new rectangle(10 , 25);
}
}
I would create sumCalculate beside main method and be over with it. Lesson is to implement an interface method and use it too.
And beside that I suggest reading Java naming convention and correcting your code accordingly.
The way I understand it you should not change the Interface, especially if the Interface was provided to you!
Just write your sumCalculate below your main method like this
private static double sumCalculate(Calculateable[] c) {
// do your sum up and return the result
}
and call it in your main method like this
double sum = sumCalculate(perimetersums);
few confusion is in my mind.. In the implementation rectangle your are not using the x in the calculate method. I changed the whole class structure a little bit.. Please look.. I think it will help you...
public interface Calculatable {
public double calculate(int x);
public double getPerimeter();
}
public class square implements Calculatable {
public double side;
private double perimeter;
square(double side){
this.side = side;
}
public double getside(){
return side;
}
public double calculate(int x) {
double perimeter = side * 4;
System.out.println("This calculate method will output the perimeter divided by the parameter x");
double calculation = perimeter / x;
System.out.println("The original perimeter was " + perimeter + ". And the calculated perimeter is " + calculation +".");
this.perimeter=calculation;
return calculation;
}
#Override
public double getPerimeter() {
// TODO Auto-generated method stub
return perimeter;
}
}
public class rectangle implements Calculatable {
public double length;
public double width;
private double perimeter;
rectangle(double length , double width){
this.length = length;
this.width = width;
}
public double getlength(){
return length;
}
public double getwidth(){
return width;
}
public double calculate(int x) {
double perimeter = 2 * (length + width);
double calculation = 2 * perimeter;
System.out.println("This will return the perimeter of the rectangle times the x paramater");
System.out.println("Your current perimeter is " + perimeter + " and your perimeter after the calculation is " + calculation + ".");
this.perimeter=calculation;
return calculation;
}
#Override
public double getPerimeter() {
// TODO Auto-generated method stub
return perimeter;
}
}
public class MainMethod {
public static void main(String[] args) {
Calculatable perimeter1 = new rectangle(20.5 , 50);
perimeter1.calculate(5);
Calculatable perimeter2 = new square(10.5);
perimeter2.calculate(2);
Calculatable[] perimetersums = new Calculatable[5];
perimetersums[0] = perimeter1; //new rectangle(20.5 , 50);
perimetersums[1] = perimeter2;// new rectangle(10 , 25);
System.out.println("the sum is= "+sumCalculate(perimetersums));
}
private static double sumCalculate ( Calculatable[] perimetersums)
{
double sum=0.0;
for(int i=0;i<perimetersums.length;i++)
{
Calculatable cal=perimetersums[i];
if(cal!=null)
sum=sum+cal.getPerimeter();
}
return sum;
}
}
I changed the class structure a little bit...
public interface Calculatable {
public double calculate();
}
public class square implements Calculatable {
private final int x=2;
public double side;
square(double side){
this.side = side;
}
public double getside(){
return side;
}
public double calculate() {
double perimeter = side * 4;
System.out.println("This calculate method will output the perimeter divided by the parameter x");
double calculation = perimeter / x;
System.out.println("The original perimeter was " + perimeter + ". And the calculated perimeter is " + calculation +".");
return calculation;
}
}
public class rectangle implements Calculatable {
private final int x=5;
public double length;
public double width;
rectangle(double length , double width){
this.length = length;
this.width = width;
}
public double getlength(){
return length;
}
public double getwidth(){
return width;
}
public double calculate() {
double perimeter = 2 * (length + width);
double calculation = 2 * perimeter;
System.out.println("This will return the perimeter of the rectangle times the x paramater");
System.out.println("Your current perimeter is " + perimeter + " and your perimeter after the calculation is " + calculation + ".");
return calculation;
}
}
public class MainMethod {
public static void main(String[] args) {
Calculatable perimeter1 = new rectangle(20.5 , 50);
// perimeter1.calculate(5);
Calculatable perimeter2 = new square(10.5);
//perimeter2.calculate(2);
Calculatable[] perimetersums = new Calculatable[5];
perimetersums[0] = new rectangle(20.5 , 50);
perimetersums[1] = new rectangle(10 , 25);
System.out.println("the sum is= "+sumCalculate(perimetersums));
}
private static double sumCalculate ( Calculatable[] perimetersums)
{
double sum=0.0;
for(int i=0;i<perimetersums.length;i++)
{
Calculatable cal=perimetersums[i];
if(cal!=null)
{
sum=sum+cal.calculate();
}
}
return sum;
}
}
I have a simple program which read the argument parameters and output them as a result.
for example, if i use:
Rectangle r1 = new Rectangle(1.0, 2.0, "RED", false);
System.out.println(r1);
should return:
1.0 x 2.0, color: RED
But what if I entered the following argument:
Rectangle r8 = new Rectangle();
System.out.println(r8);
How can my program output the following default result when the above argument is used ?
1.0 x 1.0, filled with RED
Here's a part of my code:
public class Rectangle extends Shape {
protected double width;
protected double length;
public Rectangle() {
super();
}
public Rectangle(double width, double length) {
super();
this.width = width;
this.length = length;
}
public Rectangle(double width, double length, String color, boolean filled) {
super(color, filled);
this.width = width;
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getArea() {
return (width * length);
}
public double getPerimeter() {
return (2 * (width + length));
}
public String toString() {
if (super.isFilled()) {
return width + " x " + length + ", " + "filled with RED";
} else {
return width + " x " + length + ", " + "color: RED";
}
}
}
I tried to use if (width = 0){} for width but since it is a primitive type so it doesn't work...can anyone tell me how can I print out the default value if a empty argument is used ? help will be appreciated...Thank you!
You can set the default values in the parameterless constructor :
public Rectangle() {
super("RED", true);
this.width = 1.0;
this.length = 1.0;
}
Or you can assign default values in the declaration of the members :
protected double width = 1.0;
protected double length = 1.0;
and have a similar declaration with default value for the Shape members.
Or you can call a different constructor from the parameterless constructor, as suggested by Seelenvirtuose :
public Rectangle() {
this(1.0, 1.0, "RED", true);
}
You should always set a value in your constructor for the object properties. In your case you can define your default constructor to recall the one with parameters and passing it the default values:
public Rectangle() {
this(1.0, 1.0, "RED", true);
}
You may define defaults in your constructor
public class Rectangle extends Shape {
private final double DEFAULT_WIDTH=1.0;
private final double DEFAULT_LENGTH=1.0;
private final String DEFAULT_COLOUR="RED";
private final boolean DEFAULT_FILLED=true;
protected double width;
protected double length;
public Rectangle() {
this(DEFAULT_WIDTH, DEFAULT_LENGTH, DEFAULT_COLOUR, DEFAULT_FILLED);
}
public Rectangle(double width, double length) {
super();
this.width = width;
this.length = length;
}
public Rectangle(double width, double length, String color, boolean filled) {
super(color, filled);
this.width = width;
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getArea() {
return (width * length);
}
public double getPerimeter() {
return (2 * (width + length));
}
public String toString() {
if (super.isFilled()) {
return width + " x " + length + ", " + "filled with RED";
} else {
return width + " x " + length + ", " + "color: RED";
}
}
}
I don't fully understand the problem , but this might help , if you want something else just say
public Rectangle() {
this(1.0,1.0,"RED",true);
}
public Rectangle(double width, double length) {
this.width = width;
this.length = length;
}