Creating a Programmer-Defined Class in Java: Hello, I have the program I believe
ID: 3811872 • Letter: C
Question
Creating a Programmer-Defined Class in Java:
Hello, I have the program I believe almost complete, though, I am getting errors about the absence of an identifier for my System.out.println Lines. any idea what i'm missing?
First chunk of code is my class file and second is my program:
class Rectangle
{
// Length of this rectangle.
private double rectangleLength;
// Width of this rectangle.
private double rectangleWidth;
// Write set methods here.
public void setRectangleLength(double length)
{
rectangleLength = length;
return;
}
public void setRectangleWidth(double width)
{
rectangleWidth = width;
return;
}
// Write get methods here.
public double getRectangleLength()
{
return rectangleLength;
}
public double getRectangleWidth()
{
return rectangleWidth;
}
// Write the calculatePerimeter() and
// calculateArea() methods here.
private void calculateRectanglePerimeter()
{
rectanglePerimeter = (length * 2) + (width * 2);
return;
}
private void calculateRectangleArea()
{
rectangleArea = length * width;
return;
}
}
END OF CLASS FILE
PROGRAM:
// This program uses the programmer-defined Rectangle class.
public class MyRectangleClassProgram
{
public static void main(String args[])
{
// Create rectangle1 and rectangle2 objects here.
Rectangle rectangle1 = new Rectangle();
Rectangle rectangle2 = new Rectangle();
// Set the length of rectangle1 to 10.0 here.
double LENGTH1 = 10.0;
// Set the width of rectangle1 to 5.0 here.
double WIDTH2 = 5.0;
// Print the area and perimeter of rectangle1 here.
rectangle1.setRectangleWidth(WIDTH1);
rectangle1.setRectangleLength(LENGTH1);
System.out.println("The area and perimeter of rectangle1 is: " + getRectangleArea() + getRectanglePerimeter().;
// Set the length of rectangle2 to 7.0 here.
double LENGTH2 = 7.0;
// Set the width of rectangle2 to 3.0 here.
double WIDTH2 = 3.0;
// Print the area and perimeter of rectangle2 here.
rectangle2.setRectangleWidth(WIDTH2);
rectangle2.setRectangleLength(LENGTH2);
System.out.println("The area and perimeter of rectangle2 is: " + getRectangleArea() + getRectanglePerimeter().;
System.exit(0);
}
}
Explanation / Answer
Hi
I have updated the code and highlighted the code changes below
MyRectangleClassProgram.java
class Rectangle
{
// Length of this rectangle.
private double rectangleLength;
// Width of this rectangle.
private double rectangleWidth;
// Write set methods here.
public void setRectangleLength(double length)
{
rectangleLength = length;
return;
}
public void setRectangleWidth(double width)
{
rectangleWidth = width;
return;
}
// Write get methods here.
public double getRectangleLength()
{
return rectangleLength;
}
public double getRectangleWidth()
{
return rectangleWidth;
}
// Write the calculatePerimeter() and
// calculateArea() methods here.
public double getRectanglePerimeter()
{
return (rectangleLength * 2) + (rectangleWidth * 2);
}
public double getRectangleArea()
{
return (rectangleLength * rectangleWidth );
}
}
public class MyRectangleClassProgram
{
public static void main(String args[])
{
// Create rectangle1 and rectangle2 objects here.
Rectangle rectangle1 = new Rectangle();
Rectangle rectangle2 = new Rectangle();
// Set the length of rectangle1 to 10.0 here.
double LENGTH1 = 10.0;
// Set the width of rectangle1 to 5.0 here.
double WIDTH1 = 5.0;
// Print the area and perimeter of rectangle1 here.
rectangle1.setRectangleWidth(WIDTH1);
rectangle1.setRectangleLength(LENGTH1);
System.out.println("The area and perimeter of rectangle1 is: " + rectangle1.getRectangleArea()+" "+ rectangle1.getRectanglePerimeter());
// Set the length of rectangle2 to 7.0 here.
double LENGTH2 = 7.0;
// Set the width of rectangle2 to 3.0 here.
double WIDTH2 = 3.0;
// Print the area and perimeter of rectangle2 here.
rectangle2.setRectangleWidth(WIDTH2);
rectangle2.setRectangleLength(LENGTH2);
System.out.println("The area and perimeter of rectangle2 is: " + rectangle2.getRectangleArea() +" "+rectangle2.getRectanglePerimeter());
System.exit(0);
}
}
Output:
The area and perimeter of rectangle1 is: 50.0 30.0
The area and perimeter of rectangle2 is: 21.0 20.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.