Results 1 to 7 of 7
Thread: Help with beginners program
- 12-15-2008, 01:38 AM #1
Member
- Join Date
- Dec 2008
- Posts
- 10
- Rep Power
- 0
Help with beginners program
Hi, i am new to java and my assignment is to:
Write a class Rectangle that represents a rectangle with integer width
and height. Include a constructor that builds a rectangle with a given
width and height and another constructor (with one parameter) that
builds a rectangle that is actually a square of a given size. Make sure
these constructors check that the width and height are positive. Add a
constructor that takes no parameters and builds a square of size 1.
My code so far is:
public class Rectangle {
private int width;
private int height;
public Rectangle (){
width = 1;
height = 1;
}
public Rectangle(int square){
}
public Rectangle (int height, int width){
height = height;
width = width
}
}
But im not sure if i am doing it right, also can some one give me advice on how to do the sqaure part, and check if it is a square because that is where i think i messed up. thnkx!Last edited by DanK; 12-15-2008 at 01:42 AM.
- 12-15-2008, 01:47 AM #2
Member
- Join Date
- Oct 2008
- Posts
- 34
- Rep Power
- 0
Did you run this through a compiler? Also your width = width is missing a ; and use height.this and width.this if your going to use the same name outside of the method and inside of the method
- 12-15-2008, 02:03 AM #3
Member
- Join Date
- Dec 2008
- Posts
- 10
- Rep Power
- 0
ok i fixed that but i dont thinks its suppose to return anything yet because i also have to create a client, but my teacher gave me a hint that this file should have two fields, three constuctors, and two methods, but im not sure where the method should go
- 12-15-2008, 02:10 AM #4
Member
- Join Date
- Oct 2008
- Posts
- 34
- Rep Power
- 0
What do you mean by where the methods are supposed to go? If you mean physically in the program then it probably doesn't matter. Also do you know what the methods are supposed to do? I think one is supposed to check if the numbers given are positive.
- 12-15-2008, 02:58 AM #5
Member
- Join Date
- Dec 2008
- Posts
- 10
- Rep Power
- 0
o ok, but yea im not sure what the other method is suppose to do
, maby return void or something?
- 12-15-2008, 05:02 AM #6
Member
- Join Date
- Dec 2008
- Posts
- 10
- Rep Power
- 0
the rest of my directions are:
Add a boolean method isSquare that returns true if and only if the
rectangle is a square. Add a method quadratize that converts this
rectangle into a square with approximately the same area — the closest
possible for a square with an integer side.
Test all your constructors and methods in a simple console application.
Define several rectangles, check which ones among them are squares,
and print appropriate messages. “Quadratize” one of the rectangles,
verify that it becomes a square, and print an appropriate message.
- 12-15-2008, 05:02 AM #7
Member
- Join Date
- Dec 2008
- Posts
- 10
- Rep Power
- 0
right now my code is:
public class Rectangle {
private int width;
private int height;
public Rectangle (){
width = 1;
height = 1;
}
public Rectangle(int square){
}
public Rectangle (int height, int width){
this.height = height;
this.width = width;
}
boolean isSquare;{
if (height==width)
isSquare = true;
else
isSquare = false;
}
public int quadratize()
{
int sideLength = (int) Math.sqrt (width * height);
int area = sideLength*sideLength;
return area;
}
}
Similar Threads
-
Is this book <JAVA in a netshell> good for beginners?
By meili100 in forum New To JavaReplies: 3Last Post: 10-23-2008, 05:11 AM -
what is d best spring book 4 beginners
By shikhin_82@yahoo.co.in in forum Web FrameworksReplies: 3Last Post: 10-17-2008, 04:12 PM -
Executing a program within a program
By gibsonrocker800 in forum New To JavaReplies: 5Last Post: 05-12-2008, 08:24 AM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks