1.Write a Rectangle class that will include:
a. Instance variables for width and height(at a minimum)
b. A constructor that will initialize the rectangle to a certain width and height
c. Instance methods to
i. Return the width
ii.Return the height
iii.Return the perimeter
iv.Return the lengh of the diagonal
v.Draw the rectangle using '*'
d. A main method that will create a rectangle object then display a menu to the user allowing them to:
i. Print the width
ii. Print the height
iii. Print the Area
iv. Print the perimeter
v. print the length of the diagonal
vi. Draw the rectangle
vii. Exit
2. Modify yout Rectangle application to:
a. Remove the main method from your Rectangle class.
b. Have a seperate RectangleTest application that will use the Rectangle class. This RectangleTest class will only have a main method.
c. Use MenuArray (found after these questions) to create and use a main menu in your RectangleTest class.
d. Use KB (found after these questions)to input the rectangle size or other data you need to input.
e. Write an instance method called compareTo(), that will compare the current rectangle with another rectangle and returns a positive number if the area of the current rectangle is larger, a negative if the area of the other rectangle is larger, and zero if they both have the same area.
g. Modify your draw()method to draw the rectangle border only, not having the inside area filled with stars.
3. Modify the MenuArray by adding proper comments to describe the class and its methods.
4. Write a java application to sort the values in the array in descending order. Print out the before and after sorting results.
/******************************************************************
* KB
*
* This class will demonstrate how to read from the keyboard.
*
*****************************************************************/
import java.io.*;
class KB {
/******************************************************************
*
* getString
*
* The getString method will display a prompt to standard output and read
* a line of text. getString will return the line of text as a String.
*
*****************************************************************/
public static String getString(String prompt) {
String inputLine;
System.out.print(prompt);
BufferedReader stdIn = new BufferedReader( new InputStreamReader (System.in));
try {
inputLine=stdIn.readLine();
}
catch (IOException e) {
inputLine=null;
}
return inputLine;
}
/******************************************************************
*
* getInt
*
* The getInt method will display a prompt to standard output and read
* a line of text using getString(). It will then return the integer
* value read or zero if it was an illegal integer.
*
*****************************************************************/
public static int getInt (String prompt) {
int iNum;
try {
iNum=Integer.parseInt(getString(prompt));
}
catch (NumberFormatException e) {
iNum=0;
}
return iNum;
}
/******************************************************************
*
* getDouble
*
* The getDouble method will display a prompt to standard output and read
* a line of text using getString(). It will then return the double
* value read or zero if it was an illegal double.
*
*****************************************************************/
public static double getDouble (String prompt) {
double dNum;
try {
dNum=Double.parseDouble(getString(prompt));
}
catch (NumberFormatException e) {
dNum=0;
}
return dNum;
}
} // KB
class MenuArray {
private final static int MAXITEMS = 10;
private int lastItem;
private String[] items;
private char option;
MenuArray() {
items= new String[MAXITEMS];
lastItem=0;
}
void add(String item) {
if (lastItem < items.length)
items[lastItem++]=item;
}
void show() {
System.out.println("Menu"
for (int i=0; i<lastItem; i++) {
System.out.println(i+". "+items);
}
}
void readOption() {
option= KB.getString("\nPlease enter an option number: ".charAt(0);
}
char getOption() {
return this.option;
}
}
a. Instance variables for width and height(at a minimum)
b. A constructor that will initialize the rectangle to a certain width and height
c. Instance methods to
i. Return the width
ii.Return the height
iii.Return the perimeter
iv.Return the lengh of the diagonal
v.Draw the rectangle using '*'
d. A main method that will create a rectangle object then display a menu to the user allowing them to:
i. Print the width
ii. Print the height
iii. Print the Area
iv. Print the perimeter
v. print the length of the diagonal
vi. Draw the rectangle
vii. Exit
2. Modify yout Rectangle application to:
a. Remove the main method from your Rectangle class.
b. Have a seperate RectangleTest application that will use the Rectangle class. This RectangleTest class will only have a main method.
c. Use MenuArray (found after these questions) to create and use a main menu in your RectangleTest class.
d. Use KB (found after these questions)to input the rectangle size or other data you need to input.
e. Write an instance method called compareTo(), that will compare the current rectangle with another rectangle and returns a positive number if the area of the current rectangle is larger, a negative if the area of the other rectangle is larger, and zero if they both have the same area.
g. Modify your draw()method to draw the rectangle border only, not having the inside area filled with stars.
3. Modify the MenuArray by adding proper comments to describe the class and its methods.
4. Write a java application to sort the values in the array in descending order. Print out the before and after sorting results.
/******************************************************************
* KB
*
* This class will demonstrate how to read from the keyboard.
*
*****************************************************************/
import java.io.*;
class KB {
/******************************************************************
*
* getString
*
* The getString method will display a prompt to standard output and read
* a line of text. getString will return the line of text as a String.
*
*****************************************************************/
public static String getString(String prompt) {
String inputLine;
System.out.print(prompt);
BufferedReader stdIn = new BufferedReader( new InputStreamReader (System.in));
try {
inputLine=stdIn.readLine();
}
catch (IOException e) {
inputLine=null;
}
return inputLine;
}
/******************************************************************
*
* getInt
*
* The getInt method will display a prompt to standard output and read
* a line of text using getString(). It will then return the integer
* value read or zero if it was an illegal integer.
*
*****************************************************************/
public static int getInt (String prompt) {
int iNum;
try {
iNum=Integer.parseInt(getString(prompt));
}
catch (NumberFormatException e) {
iNum=0;
}
return iNum;
}
/******************************************************************
*
* getDouble
*
* The getDouble method will display a prompt to standard output and read
* a line of text using getString(). It will then return the double
* value read or zero if it was an illegal double.
*
*****************************************************************/
public static double getDouble (String prompt) {
double dNum;
try {
dNum=Double.parseDouble(getString(prompt));
}
catch (NumberFormatException e) {
dNum=0;
}
return dNum;
}
} // KB
class MenuArray {
private final static int MAXITEMS = 10;
private int lastItem;
private String[] items;
private char option;
MenuArray() {
items= new String[MAXITEMS];
lastItem=0;
}
void add(String item) {
if (lastItem < items.length)
items[lastItem++]=item;
}
void show() {
System.out.println("Menu"
for (int i=0; i<lastItem; i++) {
System.out.println(i+". "+items);
}
}
void readOption() {
option= KB.getString("\nPlease enter an option number: ".charAt(0);
}
char getOption() {
return this.option;
}
}