Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 17 of 17

Thread: Help with classes & instance methods

  1. #1
    Junior Member ShakeyJakey's Avatar
    Join Date
    May 2010
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Help with classes & instance methods

    Hi everyone,

    Im still fairly new to java and i've gotten stuck on a small part of exercise. Could anybody help me? Heres the question relating to this section:

    Write a class to encapsulate the notion of a rectangle. A rectangle is represented by two integers representing its length and height, respectively. As well as its instance variables, the class should have instance methods called get() and isSquare(). Method get initialises the instance variables with, respectively, a length and height read from the keyboard. Method isSquare returns a boolean corresponding to whether this rectangle is a square or not. There is no need to write a constructor.

    It's probably really simple but i'm just a bit unsure as to what to write in the isSquare method below.

    This is what i have so far
    public class Rectangle
    {
    	private int length;
    	private int height;
     
    	public void get()
    	{
    		System.out.println("Please enter length and height");
    		int l = Integer.parseInt(Console.readToken());
    		int h = Integer.parseInt(Console.readToken());
     
    	}
     
    	public static boolean isSquare(Rectangle r)
    	{
    		???
    	}
    }

    Any help would be greatly appreciated, thanks


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Help with classes & instance methods

    I'm going to guess that the isSquare method is to test if the rectangle is a square. A rectangle is only a square if it's length and width are equal.

  3. #3
    Junior Member ShakeyJakey's Avatar
    Join Date
    May 2010
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with classes & instance methods

    So you're saying i should have something like this:

     
    public static boolean isSquare(Rectangle r)
    {
             if(l==h)
             {
                    System.out.println("Square");
                    return true;
              }
             else
                    return false;
    }

    What im also wondering is do I have the correct parameter in isSquare?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with classes & instance methods

    do I have the correct parameter in isSquare?
    That would depend on how you want to use the method. Most likely you want to determine if a Rectangle is a square.
    If there are other shapes that you want to test, then the parameter might be changed to allow testing of the other shape.

    About your code: Where are the variables: l and h defined? Are they variables in the Rectangle object passed to the method?

  5. #5
    Junior Member ShakeyJakey's Avatar
    Join Date
    May 2010
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with classes & instance methods

    Yes its just to determine if a rectangle is a square. There are no other shapes involved in the question.
    I dont think I defined them, I just created them in the get() method that would be read from the user, giving the height and length of the rectangle.

  6. #6
    Junior Member
    Join Date
    Jul 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with classes & instance methods

    i would suggest its pretty much the same concept
    minus the multiple returns

    public boolean isSquare(Rectangle r)
    {
    boolean flag = false;
    if(r.getLength() == r.getHeight())
    {
    flag = true;
    }
    return flag;
    }

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with classes & instance methods

    Or
    public boolean isSquare(Rectangle r)
    {
       return (r.getLength() == r.getHeight());
    }

  8. #8
    Junior Member ShakeyJakey's Avatar
    Join Date
    May 2010
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with classes & instance methods

    Thanks for the help I solved it in the end, taking into account what you all suggested. Here's the solution for those interested
    class Rectangle
    {
    	private int length;
    	private int height;
     
    	public void get()
    	{
    		System.out.println("Enter Length and height:");
    		length = Console.readInt();
    		height = Console.readInt();
    	}
     
    	public boolean isSquare()
    	{
    		if(length==height)
    		{
    			System.out.println("Square");
    			return true;
    		}
    		else
    		{
    			System.out.println("Not a square");
    			return false;
    		}
    	}
    }

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with classes & instance methods

    An unusual construct: not to initialize the member variables until get() is called. And ALWAYS to prompt for the sizes every time get() is called. Normally the sizes would be determined outside of the class and passed to the class in its constructor.

    I think this class needs a redesign.

  10. #10
    Junior Member ShakeyJakey's Avatar
    Join Date
    May 2010
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with classes & instance methods

    But the question stated that there is no need to create a constructor. And the whole point of the get() method is to prompt for sizes, because part (ii) of the question is to use this class in a program which will read a series of lines, each containing the length and height of a rectangle, and print out the number of squares.

    An unusual construct: not to initialize the member variables until get() is called.
    I dont quite understand what you mean. Do you mean i should have something like this:
     
    public int getLength()
    {
            return length;
    }
     
    public int getHeight()
    {
           return height;
    }

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with classes & instance methods

    Ok. Your code does follow the assignment statement if you read it that the class does the I/O. Another reading of the assignement could put the I/O outside the class. The intent of the statement: "length and height read from the keyboard" was to say where the data was to come from. It didn't say to do it in the class. Poorly written assignment.

    I'd never seen a simple class like this do I/O to get values. Look at the JDK's similar classes for examples. Values are passed into the class via the constructor.
    Last edited by Norm; July 29th, 2010 at 11:09 AM.

  12. #12
    Junior Member ShakeyJakey's Avatar
    Join Date
    May 2010
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with classes & instance methods

    Oh I see what you mean now, sorry. After doing part (ii), I had to remove the I/O from the class anyway so I understand now Thanks

  13. #13
    Junior Member ShakeyJakey's Avatar
    Join Date
    May 2010
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with classes & instance methods

    Instead of making a seperate thread for a similar question I thought id post it here. Could anybody explain what a default constructor is? Heres the question:

    Write a class called Item to represent an item in a supplier’s stock. Each item has a name (which is a single word), a stock level (an integer), and a price (a double). Include the following methods:

    • A method which reads an item from a single line of input consisting of the item’s name, the stock level, and the price in that order. A typical line looks like:

    toaster 7 23.50

    • A method which prints the item’s name.

    • A method buyIn(int n) which increases the stock level for this item by n units (assume n>0).

    • A method sell(int n) which reduces the stock level for this item by n items (assume n>0). If the stock level is less than n initially, then no action is taken. Return a boolean value to indicate whether or not the stock was reduced.

    • A method value which returns the total value of the stock for this item (price times stock level).

    • A constructor which creates an item with a given name, stock level, and price.

    A default constructor (i.e. one without parameters)


    Im just unsure as to what to do for this default constructor part. Heres what I have so far:

    class Item
    {
    	private String name;
    	private int stock;
    	private double price;
     
    	public void readItem()
    	{
    		name = Console.readToken();
    		stock = Integer.parseInt(Console.readToken());
    		price = Double.parseDouble(Console.readToken());
    	}
     
    	public void printName()
    	{
    		System.out.println(name);
    	}
     
    	public void buyIn(int n)
    	{
    		stock = stock + n;
    	}
     
    	public boolean sell(int n)
    	{
    		if(stock<n)
    		{
    			return true;
    		}
    		else
    		{
    			stock = stock-n;
    			return false;
    		}
    	}
     
    	int value()
    	{
    		return stock;
    	}
     
    	Item(String n, int s, double p)
    	{
    		name = n;
    		stock = s;
    		price = p;
    	}
    }

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with classes & instance methods

    If a constructor has no arguments, then it has no values from the user to store into the classes member variables.
    Its up to the programmer that defines that constructor to put values into those member variables. Its a design decision that depends on the class and application.

  15. #15
    Junior Member
    Join Date
    Jul 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with classes & instance methods

    I don't really agree with how you are getting the length and width of the rectangle here. Java is an object-oriented programming language. So I am wondering, why you aren't using a constructor to create the rectangles?

    Hunter

  16. #16
    Junior Member ShakeyJakey's Avatar
    Join Date
    May 2010
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with classes & instance methods

    Quote Originally Posted by mcmillhj View Post
    I don't really agree with how you are getting the length and width of the rectangle here. Java is an object-oriented programming language. So I am wondering, why you aren't using a constructor to create the rectangles?

    Hunter
    Well i would have, but the question stated that no constructor is needed

  17. #17
    Junior Member
    Join Date
    Jul 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with classes & instance methods

    Was this an assignment for a class?


    Hunter

Similar Threads

  1. [SOLVED] Abstract Classes Help
    By SweetyStacey in forum Object Oriented Programming
    Replies: 10
    Last Post: May 6th, 2010, 06:15 AM
  2. [SOLVED] static variable in an instance method?
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: January 30th, 2010, 03:24 AM
  3. Creating new instance
    By vluong in forum Object Oriented Programming
    Replies: 2
    Last Post: November 28th, 2009, 11:35 PM
  4. class constants instance constants..... etc
    By chronoz13 in forum Java Theory & Questions
    Replies: 1
    Last Post: August 18th, 2009, 03:38 PM
  5. [SOLVED] Java program using two classes
    By AZBOY2000 in forum Object Oriented Programming
    Replies: 7
    Last Post: April 21st, 2009, 06:55 AM

Tags for this Thread