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 4 of 4

Thread: House/Room Classes

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default House/Room Classes

    Hi there im a newbie to java and having a bit of trouble on an assignment. I have two classes House and Room and House contains an arraylist of rooms.
    This is the part of the assignment which i am mainly having trouble with:

    *Implement a method to read in room information in the House class from the keyboard — this should call the Room constructor as required. Modify the Create a zero-argument House constructor to call this new method.

    Any suggestions would be greatly welcomed

     
    import java.util.Scanner;
    import java.util.ArrayList;
     
    public class House {
     
      private int idNum;
      private static int internalCount = 0;
      // for setting idNum only
     
      private String address;
      private int numRooms;
      private String houseType;
     
     
     
      public House (String address, int numRooms, String houseType) {
        ArrayList<Room> rooms = new ArrayList<Room>();
        idNum = internalCount++;
     
        this.address = address;
        this.numRooms = numRooms;
        this.houseType = houseType;  
       }
     
     
     
      public House () {
        idNum = ++internalCount;
     
        Scanner scan = new Scanner(System.in);
        scan.useDelimiter("\n");
     
        System.out.println("Enter address of house:");
        address = scan.next();
     
        System.out.println("Enter number of rooms:");
        numRooms = scan.nextInt();
     
        System.out.println("Enter type of house:");
        houseType = scan.next();
     
        read =scan.next();
     
      }
     
     
      int getIdNum() {
        return idNum;
      }
     
      public readRoom() {
        read= rooms.add (read);
     
        return read;
      }
     
     
     
     
     
     
     
        @Override
       public String toString() {
         return address + "- Num rooms: " + numRooms + ' ' + houseType + '\n';
       }   
    }
     
     
     
     
     
     
     
     
    import java.util.Scanner;
     
    public class Room {
     
      private String description;
      private double length;
      private double width;
     
      public Room (String description, double length, double width) {
        this.description = description;
        this.length = length;
        this.width = width;     
       }
     
      public Room () {
        Scanner scan = new Scanner(System.in);
        scan.useDelimiter("\n");
     
        System.out.println("Enter description of room:");
        description = scan.next();
     
        System.out.println("Enter length of room:");
        length = scan.nextDouble();
     
        System.out.println("Enter width of room:");
        width = scan.nextDouble();
       }
     
     
       public double getArea () {
         return length*width;
       }
     
     
     
       @Override
       public String toString() {
         return description + "- Length: " + length + "m; Width: " + width + 'm';
       }   
    }


  2. #2
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 Posts

    Default Re: House/Room Classes

    Implement a method to read in room information in the House class from the keyboard — this should call the Room constructor as required. Modify the Create a zero-argument House constructor to call this new method.
    Which part are you having problems with?
    Create a method
    read in room info
    call Room constructor
    create 0 arg House constructor
    call the method

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: House/Room Classes

    "Create a method
    read in room info
    call Room constructor"

    The first three you mentioned.

    Done

  4. #4
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 Posts

    Default Re: House/Room Classes

    See the tutorial about how to define/create a method:
    Defining Methods (The Java Tutorials > Learning the Java Language > Classes and Objects)

    To read input from the console see the Scanner class.
    If you want a dialog window see the JOptionPane class.

    Use the new statement to call a constructor.
    Creating Objects (The Java Tutorials > Learning the Java Language > Classes and Objects)
    Passing Information to a Method or a Constructor (The Java Tutorials > Learning the Java Language > Classes and Objects)

Similar Threads

  1. Full House help?
    By jean28 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 10th, 2012, 01:57
  2. Chat Room in JSP
    By c.P.u1 in forum JavaServer Pages: JSP & JSTL
    Replies: 8
    Last Post: July 7th, 2011, 19:14