The Artima Developer Community
Sponsored Link

Java Answers Forum
a little challege

2 replies on 1 page. Most recent reply: Feb 19, 2011 9:49 AM by Jordan O

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Travis Gearhart

Posts: 1
Nickname: travis1
Registered: Jan, 2011

a little challege Posted: Jan 4, 2011 4:20 PM
Reply to this message Reply
Advertisement
Design and implement a class Rectangle with overloaded methods getPerimeter and getArea. One set accepts both length and width while the other set only accepts a single side (assuming that it must be a square). Implement a class RectangleTest to demonstrate the capabilities of the Rectangle class.
looks not that hard right? well i got this but its missing something can you fix it to get to to work?
using net beans compiler first part then the test.
public class Rectangle

{



private int length = 0;

private int width = 0;

/**

* Returns the shape parameters

* @return

*/

public String getPerimeter ()

{

int perimeter = (length * 2) + (width * 2);

return String.format("Perimeter is %s\n", perimeter);



}



/**

* when called this method will set both the lenght and width

* of the shape

* @param length

* @param width

*/

public String getArea (int length1, int width1)

{

length = length1;

width = width1;

return String.format("A rectangle is %s\n by %s\n",length, width);



}

/**

* when the getArea method is called for a square it will set the width

* and the length are equal to the width (all sides are equal)

* @param length

*/

public String getArea (int length1)

{

width = length1;

length = length1;

return String.format("A square is %s\n by %s\n",length, width);

}

}



now the test
**

*

* @author

*/

public class RectangleTest

{

/**

* @param args the command line arguments

*/

public static void main(String[] args)

{

//variable declarations

int length1;

int width1;



//creates scanner

Scanner input = new Scanner(System.in);



// creates instance of rectange named myShape

RectangleTest myShape = new RectangleTest();



//gathers the users input for the length of the shape

System.out.println("Please enter the length of the shape: ");

length1 = input.nextInt();

//gathers the users input for the width of the shape

System.out.println("Please enter the width of the shape: ");

width1 = input.nextInt();



// when the width is equal to the length then a square is the shape

if (width1 == length1);

}
}


Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: a little challege Posted: Jan 5, 2011 6:28 AM
Reply to this message Reply
What currently happens when you run the test?

Jordan O

Posts: 7
Nickname: crowbar
Registered: Feb, 2011

Re: a little challege Posted: Feb 19, 2011 9:49 AM
Reply to this message Reply
First off your code is a mess, I recommend working on your coding style so that its easily readable.

It looks like you are having trouble understanding how composition should be used. Some of the code is superfluous but I tried to leave the methods as close to your original as I could.
This code will do what you want it to:

import java.util.Scanner;
/*
* @author
*/
public class RectangleTest{
/**
* @param args the command line arguments
*/
public static void main(String[] args){
// creates instance of rectangle named myShape
Rectangle myShape = new Rectangle();
myShape.setRec();

}
}

class Rectangle {
//creates scanner
Scanner input = new Scanner(System.in);
private int length = 0;
private int width = 0;
public void setRec (){
//gathers the users input for the length of the shape
System.out.println("Please enter the length of the shape: ");
length = input.nextInt();
//gathers the users input for the width of the shape
System.out.println("Please enter the width of the shape: ");
width = input.nextInt();
System.out.println(this.getPerimeter());
if (length != width) {
System.out.println(this.getArea(length, width));
}else{
System.out.println(this.getArea(length));
}
}
/**
* Returns the shape parameters
* @return
*/
public String getPerimeter (){
int perimeter = (length * 2) + (width * 2);
return String.format("Perimeter is %s\n", perimeter);
}
/**
* when called this method will set both the length and width
* of the shape
* @param length
* @param width
*/
public String getArea (int length1, int width1){
length = length1;
width = width1;
return "The rectangle is " + length*width + " units squared";
}
/**
* when the getArea method is called for a square it will set the width
* and the length are equal to the width (all sides are equal)
* @param length
*/
public String getArea (int length1){
width = length1;
length = length1;
return String.format("A square is %s\n by %s\n",length, width);
}

}

Flat View: This topic has 2 replies on 1 page
Topic: execute .class file from .jsp Previous Topic   Next Topic Topic: A question about Java array

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use