Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to make executable programs from these questions

Status
Not open for further replies.

cgikanga

MIS
Jun 3, 2001
2
US
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(&quot;Menu&quot;);
for (int i=0; i<lastItem; i++) {
System.out.println(i+&quot;. &quot;+items);
}
}

void readOption() {
option= KB.getString(&quot;\nPlease enter an option number: &quot;).charAt(0);
}

char getOption() {
return this.option;
}
}
 
Hi,

I hope you did try to write the program yourself or else you wouldn't learn much. Try to understand the program below as well. I left out certain things (the diagonal length and drawing of rectangle). I think it would be easy for you to add in the codes for the 2 methods.

The example is for question 1 only. I think you will be able to code question2 yourself now?

import java.io.*;
public class Rectangle
{
private int width;
private int height;
public Rectangle(int width, int height)
{
this.width = width;
this.height = height;
}
public int getWidth()
{
return width;
}
public int getHeight()
{
return height;
}
public int getPerimeter()
{
return 2*(width+height);
}
public int getArea()
{
return width*height;
}
public int getDiagonal()
{
return 0;
}
public static void main(String[] args)
{
Rectangle r = new Rectangle(10,10);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String temp=&quot;&quot;;

while (!temp.equals(&quot;7&quot;))
{
System.out.println(&quot;Rectangle Menu&quot;);
System.out.println(&quot;1) Print Width&quot;);
System.out.println(&quot;2) Print Height&quot;);
System.out.println(&quot;3) Print Area&quot;);
System.out.println(&quot;4) Print Perimeter&quot;);
System.out.println(&quot;5) Print Diagonal&quot;);
System.out.println(&quot;6) Draw Rectangle&quot;);
System.out.println(&quot;7) Exit&quot;);
System.out.print(&quot;Enter your choice : &quot;);
try
{
temp = (br.readLine()).trim();
int choice = (new Integer(temp)).intValue();
switch (choice)
{
case 1:
System.out.println(&quot;Width is : &quot;+r.getWidth());
break;
case 2:
System.out.println(&quot;Height is : &quot;+r.getHeight());
break;
case 3:
System.out.println(&quot;Area is : &quot;+r.getArea());
break;
case 4:
System.out.println(&quot;Perimeter is : &quot;+r.getPerimeter());
break;
case 5:
System.out.println(&quot;Diagonal is : &quot;+r.getDiagonal());
break;
case 6:
break;
}
if (choice < 1 || choice > 7)
System.out.println(&quot;Invalid choice.&quot;);
}
catch (IOException e)
{
System.out.println(e);
}
catch (NumberFormatException e)
{
System.out.println(&quot;Invalid Input&quot;);
}
}
}
}

Regards,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top