Question:
How do I write this Java program in NetBeans?
VG Master
2008-11-04 06:38:32 UTC
LAB ASSIGNMENT A4.2

Rectangle


Background:

1. It is always important to have a well-designed class before writing down any code. Having a class laid out on paper before writing the code allows programmers to see any design flaws before they have coded those flaws into their classes. Determining which classes are needed, what data those classes hold, and how those classes behave are the main objectives of OOP.

2. The specifications of a class that models a rectangular shape would be:

Variables
private double myX; // the x coordinate of the rectangle
private double myY; // the y coordinate of the rectangle
private double myWidth; // the width of the rectangle
private double myHeight; // the height of the rectangle
private DrawingTool pen;
private SketchPad paper;
// Creates a 500 x 500 SketchPad with a DrawingTool, pen, that is used
// to display Rectangle objects. The DrawingTool is declared static
// so that multiple Rectangle objects can be drawn on the SketchPad
// at the same time.

Constructors
// Creates a default instance of a Rectangle object with all dimensions
// set to zero.
Rectangle()

// Creates a new instance of a Rectangle object with the left and right
// edges of the rectangle at x and x + width. The top and bottom edges
// are at y and y + height.
Rectangle(double x, double y, double width, double height)

Methods
// Calculates and returns the perimeter of the rectangle
public double getPerimeter()

// Calculates and returns the area of the rectangle
public double getArea()

// Draws a new instance of a Rectangle object with the left and right
// edges of the rectangle at x and x + width. The top and bottom edges
// are at y and y + height.
public void draw()

Assignment:

1. Implement a Rectangle class with the following properties.

a. A Rectangle object is specified in the constructor with the left and right edges of the rectangle at x and x + width. The top and bottom edges are at y and y + height.
b. A method getPerimeter calculates and returns the perimeter of the Rectangle.
c. A method getArea calculates and returns the area of the Rectangle.
d. A method draw displays a new instance of a Rectangle object. Refer to Handout A1.1 DrawingTool Specifications, for details on DrawingTool methods.

2. Try your rectangle with both the default constructor and with a constructor that can take the x and y coordinates, the length of the rectangle, and the width. Here are some sample constructor calls:

Rectangle rectA = new Rectangle();
Rectangle rectB = new Rectangle(0, -80, 400, 160);
Rectangle rectC = new Rectangle(100, -100, 20, 300);
Three answers:
Steven V
2008-11-04 08:57:02 UTC
How do you use NetBeans? You can use the tutorial below.





How do you write the program?



Break the problems down....



How would you implement a class named Rectangle? You know it has 4 member variables. It has two constructors in this case. An empty constructor and one that takes all 4 values.



How do you had a method to a class? Forget about the contents of the class. How do you write a method? Don't forget you'll have to add a return statement like "return 0.0;"



If you get that far, then I'm sure your teacher/TA will help you. Get the Rectangle class created.



public class Rectangle {

//member variables

// constructors

// methods.



}
deonejuan
2008-11-04 13:07:23 UTC
This is pretty much line for line of the assignment. What you want to do with NetBeans is start a new project. File --> New Project --> Java --> Java Application [ NEXT >].



Name it ... hmmm. Name it MultiRects. Save it to somewhere. On my computer I have a gigantic folder called NetBean_projects. I would put the MultiRects inside NetBean_projects. Uncheck Main Project. Uncheck Make Main.class. [ Finish ]



Click on the MultiRects in the Project Tree Window. Right-click --> new Java Package. multirects would be a good name. And now right-click that multirects package and new Java Class, name it Rectangle. A side note here. The java API has java.awt.Rectangle (and others). What makes our Rectangle class different is that package name. Our Rectangle is: multirects.Rectangle;



public class Rectangle {

// cut-n-paste the Variables from your assingment here



// the constructors

public Rectangle() {

myX = 0;

myY = 0;

myWidth = 0;

myHeight = 0;

}

// the next constructor

// note a constructor has the same name, but no keyword 'class'

public Rectangle(double x, double y, double width, double height) {

myX = x;

myY = y;

myWidth = width;

// you figure out the last var???

}



// now the methods

public double getPerimeter() {

double p = 2*myWidth + 2* myHeight;

return p;

}

public double getArea() {

double a = myWidth * myHeight;

return a;

}

} // end class Rectangle



right-click the package multirects --> new class



public class TestRectangles {



public static void main( String[] args ) {

Rectangle r1 = new Rectangle();

System.out.println( "r1 perimeter: " + r1.getPerimeter() );

Rectangle r2 = new Rectangle(0.0, 0.0, 122.0, 44.0 );

System.out.println( "r2 area: " + r2.getArea() );

}

}

// a side note. NetBeans does not complain do you mean java.awt.Rectangle or do you need multirects.Rectangle? Because java looks into package first and then the API libraries.



I had to comment out the DrawingTool and the SketchPad. I do not have those class definitions. But, if I did, they would go into multirects package .... or their own package, maybe named rectangletools. If that was the case, I should need to add at the top of class Rectangles:



import rectangletools.DrawingTool;



good luck, I hope I got you started and I hope you stay with NetBeans. I really like NetBeans and java.
anonymous
2016-03-19 04:36:02 UTC
Java does not produce exe files. This is an artifact of it's 'platfom indepedant' nature. If your program is only one class you can run it from command prompt with C> java filename this assumes that it's in the default package, ie, no package statement If it's a multiple class program then you can run the jar file: C> java -jar filename.jar


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...