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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java program help

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
i hava a program that need some help i need to get it to compute and output the area of the perimetrer of a rectangle here is what i have
public class Main
{
private int length;
private int width;

public Rectangle()
{
length = 1;
width = 1;
}

public Rectangle(int l, int w)
{
length = l;
width = w;
}

public void print_info()
{
System.out.println("length = " + length);
System.out.println("width = " + width);
}

public int get_length()
{
return length;
}

public int get_width()
{
return width;
}
}
 
Perimiter = 2(length) + 2(width) or length + length + width + width. All you have to do is this:

Code:
public class Main {

	public static void main(String[] args) {
		int length = 1;
		int width = 1;

		System.out.print("\n\nThe perimiter is " + ((2 * length)
		+ (2 * width)) + ".\n\n\n");
	}
}

But If you want to use a class to do it, then try this:

Code:
public class Main3 {

	public static void main(String[] args) {
		Perimiter one = new Perimiter();

		int perimiter = one.getPerimiter(1, 1);

		System.out.print("\n\nThe perimiter is " + perimiter
		+ ".\n\n\n");
	}
}

class Perimiter {

	public int getPerimiter(int width, int height){
		int perim = (2 * width) + (2 * height);
		return perim;
	}
}

I hope that helps you. X-) "and everything under the sun is in tune
but the sun is eclipsed by the moon." --Pink Floyd: Eclipse


"I'm going to spend eternity
reinstalling Windows." --Reinstalling Windows: by some British guy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top