6 Replies Latest reply on Aug 16, 2008 9:25 PM by 843785

    Arrays

    843785
      Hey!
      Am new in Java and still confused about lot of thing here (a weard universe) Have an excersise:
      The class Staircase represents all the owners in one staircase. Each staircase has a street name, street number and a number of owners.
      Design and implement the class Staircase(use an array to manage floors and owners).
      Imlement a constructor which takes a String , an int and an int to initialize the street name, street number and the number of owners (Hint: it can be helpful to use another integer to store how many that is used in the array).
      Method addOwner(Floor owner), checks if the array is empty or full before inserting. Returns true if the owner could be inserted, false otherwise.
      And other methods.

      There is a class Floor, with fields String floor, String ownerName, int apartmentNo, double apartmentPrice, all have set and get methods. This i have made. But have trouble with Staircase class:

      public class Staircase
      {
      private String streetName;
      private int streetNumber;
      private int numberOfOwners[];
      private Floor[] Floors;
      private int size;
      private int used;
      /** Creates a new instance of Staircase */
      public Staircase(String name, int number)
      {
      streetName = name;
      streetnumber = number;
      numberOfOwners = nrOwnersArray;
      floors = new Floor[size];
      this.size = size;
      used = 0;
      }

      }
        • 1. Re: Arrays
          843785
          at_la wrote:
          ...This i have made. But have trouble with Staircase class:
          Unfortunately, this tells us nothing. You should specify as precisely as possible exactly what your trouble is. In other words, exactly what is your question?
          • 2. Re: Arrays
            843785
            this is one of the most worthless exercises I've seen in a long time.

            a Staircase has multiple owners? Staircases have street names, numbers, and an owner count?

            For a practice exercise, just make a class with an array in it.. and methods to add Strings to the array and remove them. Make the array grow when needed.

            Thats all the 'Java' this exercise is trying to teach you, the rest is just needlessly confusing.

            -brillo
            • 3. Re: Arrays
              843785
              brillohead wrote:
              this is one of the most worthless exercises I've seen in a long time.

              a Staircase has multiple owners? Staircases have street names, numbers, and an owner count?

              For a practice exercise, just make a class with an array in it.. and methods to add Strings to the array and remove them. Make the array grow when needed.

              Thats all the 'Java' this exercise is trying to teach you, the rest is just needlessly confusing.

              -brillo
              I concur. Rather try [this.|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html]
              • 4. Re: Arrays
                800302
                size field is an instance variable wich is instantiated to 0 by default because you are not assigning it a value. you are constructing the array with the size as an argument, which instantiates the array with 0 elements thus you cannot put any floors in the array.

                Change this, than your code will function correctly. Assign a value to size, and do this before instantiating the array.

                Edited by: arian on Aug 14, 2008 3:01 PM
                • 5. Re: Arrays
                  843785
                  be careful saying "then your code will function correctly". Honestly, there are so many little things wrong with that tiny snippet of code that the OP needs to be following a better tutorial then he is.

                  A link was provided earlier in a response, follow that and all other related tutorials instead of the book you are using now.
                  • 6. Re: Arrays
                    843785
                    Hey!
                    Thanks for your time! Here the whole excersise and the methods i have trouble with.
                    /*
                    * Floor.java
                    *
                    * Created on August 14, 2008, 11:45 AM
                    *
                    * To change this template, choose Tools | Template Manager
                    * and open the template in the editor.
                    */

                    package first;

                    /**
                    *
                    * @author student
                    */
                    public class Floor
                    {
                    private String floor;
                    private String ownerName;
                    private int apartmentNo;
                    private double apartmentPrice;

                    /** Creates a new instance of Floor */
                    public Floor(String fl, String name, int no, double price)
                    {
                    floor = fl;
                    ownerName = name;
                    apartmentNo = no;
                    apartmentPrice = price;
                    }
                    public void setFloor(String fl)
                    {
                    floor = fl;
                    }
                    public String getFloor()
                    {
                    return floor;
                    }
                    public void setOwnerName(String name)
                    {
                    ownerName = name;
                    }
                    public String getOwnerName()
                    {
                    return ownerName;
                    }
                    public void setApartmentNo(int no)
                    {
                    apartmentNo = no;
                    }
                    public int getApartmentNo()
                    {
                    return apartmentNo;
                    }
                    public void setApartmentPrice(double price)
                    {
                    apartmentPrice = price;
                    }
                    public double getApartmentPrice()
                    {
                    return apartmentPrice;
                    }
                    }


                    /*
                    * Staircase.java
                    *
                    * Created on August 14, 2008, 12:07 PM
                    *
                    * To change this template, choose Tools | Template Manager
                    * and open the template in the editor.
                    */

                    package first;

                    /**
                    *
                    * @author student
                    */
                    public class Staircase
                    {
                    private String streetName;
                    private int streetNumber;
                    private int[] numberOfOwners;
                    private Floor[] floors;
                    private int size;
                    private int used;
                    /** Creates a new instance of Staircase */
                    public Staircase(String name, int number)
                    {
                    streetName = name;
                    streetNumber = number;
                    numberOfOwners = new int[size];
                    floors = new Floor[size];
                    this.size = size;
                    used = 0;
                    }

                    public boolean addOwner(int a)
                    {
                    if(size < numberOfOwners.length)
                    {
                    numberOfOwners[used] = a;
                    used++;
                    return true;
                    }
                    else
                    {
                    return false;
                    }
                    }

                    public int findOwner(String ownerName) //This method isn?t working, i guess wrong!!
                    {
                    for(int i = 0; i < used; i++)
                    {
                    if(numberOfOwners.getOwnerName().equals(String ownerName))
                    return numberOfOwners[i].getFloor();
                    }
                    }

                    public void listOwners()
                    {
                    for(int i=0; i<Floor.length; i++)
                    {
                    System.out.println("Owner name:"+Floor[i].getOwnerNAme());
                    System.out.println("Appartment number:" +Floor[i].getApartmentNo());
                    System.out.println("Apartment price:"+ Floor[i].getApartmentPrice());
                    }
                    }
                    public double getStaircseValue()
                    {
                    return //??? my problem is arrays-to use them!!!
                    }
                    }

                    HAve done this far. Method findOwners don't work for me - there is a mistake somewhere i guess.
                    Method double getStaircaseValue(). This method should total the value of all appartments of a staircase in a variable and return it.
                    Am getting annoyed. I guess it should be easy, bet still tricks me. Java is new to me, so am still fighting.
                    Please help if you have time.

                    Best regards