Problem adding a object to an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AZRebelCowgirl73
    New Member
    • Nov 2006
    • 47

    #1

    Problem adding a object to an array

    here is the instructiions I am to use

    Ask the user to input the number of effective cars in the shop (this number should be between 1 and 100, inclusively). In a loop, controlled by the inputted number of effective cars, ask the user to input each car data (i.e. make, model, year and price). You can assume that all input data is correct and no data validation is necessary. As soon as all input data for a car is entered, build a Car object by the constructor with parameters and then save the Car object in the array.

    Here is what i have so far:

    /* UMUC - CMIS 141 - Project 4
    * File: Car.java
    * Author: Annie Ideus-Balboa
    * Date: May 13th, 2007
    * Complete: May 17th, 2007
    * Modified for Project 4: May 29th, 2007
    * Use indications: Run the program from a command window
    */


    import java.util.*;
    import java.net.*;
    import java.lang.*;

    public class Car{

    public static String make;
    public static String model;
    public static int year;
    public static int price;


    //Car(); constructor with parameters
    public Car(String m, String d, int y, int p) {
    make = m;
    model = d;
    year = y;
    price = p;
    }//end constructor

    // return make of car (make accessor)
    public String getMake() {
    return make;
    }//end method

    //return model of car (model accessor)
    public String getModel() {
    return model;
    }//end method

    //return year of car (year accessor)
    public int getYear() {
    return year;
    }//end method

    //return price of car (price accessor)
    public int getPrice() {
    return price;
    }//end method

    //print method
    public void print() {
    System.out.prin tln("Make = " + getMake() + "," + " Model = " + getModel() + "," + " Year = " + getYear() + "," + " Price = $" + getPrice());
    }//end method
    }

    that is Car.java file used to create new Cars

    /* UMUC - CMIS 141 - Project 4
    * File: CarDealerApp.ja va
    * Author: Annie Ideus-Balboa
    * Date: May 29thth, 2007
    * Use indications: Run the program from a command window
    */


    import java.util.*;
    import java.net.*;
    import java.lang.*;

    public class CarDealerApp{



    public static void main(String[] args) {

    //declare array of Car objects. Array name is carShop
    final int MAX_SIZE = 100;
    Car carShop[] = new Car[MAX_SIZE];

    Scanner stdin = new Scanner(System. in);

    //self intro
    System.out.prin tln();
    System.out.prin tln(" CMIS 141 Project 3");
    System.out.prin tln(" File: Car.java");
    System.out.prin tln(" Created by: Annie (ALI) Ideus-Balboa");
    System.out.prin tln(" Due: June 3rd, 2007");
    System.out.prin tln();

    System.out.prin t("Please input the number of effective cars currently in the shop?: ");
    int numberOfCars = stdin.nextInt() ;

    for(int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {

    System.out.prin tln();
    System.out.prin tln("Your Car (Car c3):");
    System.out.prin t("What is the make of your car? ");
    String m = stdin.nextLine( );
    System.out.prin t("What is the model of your car? ");
    String d = stdin.nextLine( );
    System.out.prin t("What is the Year of your car? ");
    int y = stdin.nextInt() ;
    System.out.prin t("What is the Price of your car? $");
    int p = stdin.nextInt() ;
    Car C = new Car(m, d, y, p);
    carShop[i] = C;
    }
    for (int i= 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
    System.out.prin tln(carShop[i]);
    }
    }
    }

    now when I run this the print comes out as
    Car@14318bb
    Car@ca0b6

    Can anyone help me with this? Thanks in Advance ALI :P
  • nomad
    Recognized Expert Contributor
    • Mar 2007
    • 664

    #2
    Originally posted by AZRebelCowgirl7 3
    here is the instructiions I am to use

    Ask the user to input the number of effective cars in the shop (this number should be between 1 and 100, inclusively). In a loop, controlled by the inputted number of effective cars, ask the user to input each car data (i.e. make, model, year and price). You can assume that all input data is correct and no data validation is necessary. As soon as all input data for a car is entered, build a Car object by the constructor with parameters and then save the Car object in the array.

    Here is what i have so far:

    /* UMUC - CMIS 141 - Project 4
    * File: Car.java
    * Author: Annie Ideus-Balboa
    * Date: May 13th, 2007
    * Complete: May 17th, 2007
    * Modified for Project 4: May 29th, 2007
    * Use indications: Run the program from a command window
    */


    import java.util.*;
    import java.net.*;
    import java.lang.*;

    public class Car{

    public static String make;
    public static String model;
    public static int year;
    public static int price;


    //Car(); constructor with parameters
    public Car(String m, String d, int y, int p) {
    make = m;
    model = d;
    year = y;
    price = p;
    }//end constructor

    // return make of car (make accessor)
    public String getMake() {
    return make;
    }//end method

    //return model of car (model accessor)
    public String getModel() {
    return model;
    }//end method

    //return year of car (year accessor)
    public int getYear() {
    return year;
    }//end method

    //return price of car (price accessor)
    public int getPrice() {
    return price;
    }//end method

    //print method
    public void print() {
    System.out.prin tln("Make = " + getMake() + "," + " Model = " + getModel() + "," + " Year = " + getYear() + "," + " Price = $" + getPrice());
    }//end method
    }

    that is Car.java file used to create new Cars

    /* UMUC - CMIS 141 - Project 4
    * File: CarDealerApp.ja va
    * Author: Annie Ideus-Balboa
    * Date: May 29thth, 2007
    * Use indications: Run the program from a command window
    */


    import java.util.*;
    import java.net.*;
    import java.lang.*;

    public class CarDealerApp{



    public static void main(String[] args) {

    //declare array of Car objects. Array name is carShop
    final int MAX_SIZE = 100;
    Car carShop[] = new Car[MAX_SIZE];

    Scanner stdin = new Scanner(System. in);

    //self intro
    System.out.prin tln();
    System.out.prin tln(" CMIS 141 Project 3");
    System.out.prin tln(" File: Car.java");
    System.out.prin tln(" Created by: Annie (ALI) Ideus-Balboa");
    System.out.prin tln(" Due: June 3rd, 2007");
    System.out.prin tln();

    System.out.prin t("Please input the number of effective cars currently in the shop?: ");
    int numberOfCars = stdin.nextInt() ;

    for(int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {

    System.out.prin tln();
    System.out.prin tln("Your Car (Car c3):");
    System.out.prin t("What is the make of your car? ");
    String m = stdin.nextLine( );
    System.out.prin t("What is the model of your car? ");
    String d = stdin.nextLine( );
    System.out.prin t("What is the Year of your car? ");
    int y = stdin.nextInt() ;
    System.out.prin t("What is the Price of your car? $");
    int p = stdin.nextInt() ;
    Car C = new Car(m, d, y, p);
    carShop[i] = C;
    }
    for (int i= 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
    System.out.prin tln(carShop[i]);
    }
    }
    }

    now when I run this the print comes out as
    Car@14318bb
    Car@ca0b6

    Can anyone help me with this? Thanks in Advance ALI :P
    I'm not to sure if you can use a array this way. I think you need to use a Arraylist. You can make an index stop at 100 if an if statement but why?
    When you have this code
    Code:
     Car C = new Car(m, d, y, p);
    For example you can have an array of intergers, an array of string Objects, or an array of arrays, but you can not have an array that contains both String Objects and integers.

    Also you have a mistake in.
    Code:
     
    System.out.print("What is the make of your car? ");	
    			String m = stdin.nextLine();
    			System.out.print("What is the model of your car? ");
    			String d = stdin.nextLine();
    Delete the Line, should look like this:
    Code:
     String m = stdin.next();
    otherwise you will get
    What is the make of your car?What is the model of your car?
    and that not what you want.

    nomad
    PS if you do not know how to make a arraylist please read my Article on Arraylist

    Comment

    • blazedaces
      Contributor
      • May 2007
      • 284

      #3
      Originally posted by AZRebelCowgirl7 3
      here is the instructiions I am to use

      Ask the user to input the number of effective cars in the shop (this number should be between 1 and 100, inclusively). In a loop, controlled by the inputted number of effective cars, ask the user to input each car data (i.e. make, model, year and price). You can assume that all input data is correct and no data validation is necessary. As soon as all input data for a car is entered, build a Car object by the constructor with parameters and then save the Car object in the array.

      Here is what i have so far:

      /* UMUC - CMIS 141 - Project 4
      * File: Car.java
      * Author: Annie Ideus-Balboa
      * Date: May 13th, 2007
      * Complete: May 17th, 2007
      * Modified for Project 4: May 29th, 2007
      * Use indications: Run the program from a command window
      */


      import java.util.*;
      import java.net.*;
      import java.lang.*;

      public class Car{

      public static String make;
      public static String model;
      public static int year;
      public static int price;


      //Car(); constructor with parameters
      public Car(String m, String d, int y, int p) {
      make = m;
      model = d;
      year = y;
      price = p;
      }//end constructor

      // return make of car (make accessor)
      public String getMake() {
      return make;
      }//end method

      //return model of car (model accessor)
      public String getModel() {
      return model;
      }//end method

      //return year of car (year accessor)
      public int getYear() {
      return year;
      }//end method

      //return price of car (price accessor)
      public int getPrice() {
      return price;
      }//end method

      //print method
      public void print() {
      System.out.prin tln("Make = " + getMake() + "," + " Model = " + getModel() + "," + " Year = " + getYear() + "," + " Price = $" + getPrice());
      }//end method
      }

      that is Car.java file used to create new Cars

      /* UMUC - CMIS 141 - Project 4
      * File: CarDealerApp.ja va
      * Author: Annie Ideus-Balboa
      * Date: May 29thth, 2007
      * Use indications: Run the program from a command window
      */


      import java.util.*;
      import java.net.*;
      import java.lang.*;

      public class CarDealerApp{



      public static void main(String[] args) {

      //declare array of Car objects. Array name is carShop
      final int MAX_SIZE = 100;
      Car carShop[] = new Car[MAX_SIZE];

      Scanner stdin = new Scanner(System. in);

      //self intro
      System.out.prin tln();
      System.out.prin tln(" CMIS 141 Project 3");
      System.out.prin tln(" File: Car.java");
      System.out.prin tln(" Created by: Annie (ALI) Ideus-Balboa");
      System.out.prin tln(" Due: June 3rd, 2007");
      System.out.prin tln();

      System.out.prin t("Please input the number of effective cars currently in the shop?: ");
      int numberOfCars = stdin.nextInt() ;

      for(int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {

      System.out.prin tln();
      System.out.prin tln("Your Car (Car c3):");
      System.out.prin t("What is the make of your car? ");
      String m = stdin.nextLine( );
      System.out.prin t("What is the model of your car? ");
      String d = stdin.nextLine( );
      System.out.prin t("What is the Year of your car? ");
      int y = stdin.nextInt() ;
      System.out.prin t("What is the Price of your car? $");
      int p = stdin.nextInt() ;
      Car C = new Car(m, d, y, p);
      carShop[i] = C;
      }
      for (int i= 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
      System.out.prin tln(carShop[i]);
      }
      }
      }

      now when I run this the print comes out as
      Car@14318bb
      Car@ca0b6

      Can anyone help me with this? Thanks in Advance ALI :P
      First of all, from now on please try to use [ code ][ /code ] (without spaces) brackets around your code.

      I'm curious... what do you want it to print? All of the data in car, one at a time? All I know is that right now you're trying to print a car(m, d, y, p). Since this is not a string it simply prints the classname@locat ionInMemory, understand? If you want it to print everything (since you already have a print method in car) you should replace the following line:

      "System.out.pri ntln(carShop[i]);"

      with

      "carShop[i].print();"


      Hope that solved your problem,
      -blazed

      Comment

      • shanakard
        New Member
        • May 2007
        • 13

        #4
        Code:
        System.out.println(carShop[ i ]);
        in the above line u have given a Object of type Car to the println() function
        this will run the "println(Ob ject v)" overload of the println function and take the given parameter as a "Object" type parameter.

        when any object 'x' is given to println() function it will print the output string of the x.toString() function to the console.

        since every class is derived from the Object class and Object Class has a toString() method that prints the class Type of the object and it's memory address it will be output. (like Car@14318bb).

        if U want to output the car information for that object U should Overload the toString() method in your Car class.

        (for more information read the Object class's toString() function of the java documentation)

        Hopes this solves ur problem.

        Comment

        • AZRebelCowgirl73
          New Member
          • Nov 2006
          • 47

          #5
          Problems adding a object to an array

          Ok here is what I have So far:

          //this is the car.java file that must be used when running the CarDealerApp.ja va
          //file
          import java.util.*;
          import java.net.*;
          import java.lang.*;

          public class Car{
          public static String make;
          public static String model;
          public static int year;
          public static int price;
          //Car(); constructor with parameters
          public Car(String m, String d, int y, int p) {
          make = m;
          model = d;
          year = y;
          price = p;
          }//end constructor
          // return make of car (make accessor)
          public String getMake() {
          return make;
          }//end method
          //return model of car (model accessor)
          public String getModel() {
          return model;
          }//end method
          //return year of car (year accessor)
          public int getYear() {
          return year;
          }//end method
          //return price of car (price accessor)
          public int getPrice() {
          return price;
          }//end method
          //print method
          public void print() {
          System.out.prin tln("Make = " + getMake() + "," + " Model = " + getModel() + "," + " Year = " + getYear() + "," + " Price = $" + getPrice());
          }//end method
          }

          // This is the CarDealerApp.ja va file
          import java.util.*;
          import java.net.*;
          import java.lang.*;
          public class CarDealerApp{
          public static void main(String[] args) {
          //declare array of Car objects. Array name is carShop
          final int MAX_SIZE = 100;
          Car carShop[] = new Car[MAX_SIZE];
          Scanner stdin = new Scanner(System. in);
          System.out.prin tln();
          System.out.prin t("Please input the number of effective cars currently in the shop?: ");
          int numberOfCars = stdin.nextInt() ;
          for(int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
          System.out.prin tln();
          System.out.prin t("What is the make of your car? ");
          String m = stdin.next();
          System.out.prin t("What is the model of your car? ");
          String d = stdin.next();
          System.out.prin t("What is the Year of your car? ");
          int y = stdin.nextInt() ;
          System.out.prin t("What is the Price of your car? $");
          int p = stdin.nextInt() ;
          Car C = new Car(m, d, y, p);
          carShop[i] = C;
          System.out.prin tln();
          }
          for (int i= 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
          carShop[i].print();
          }
          }
          }

          Ok now when I run this program I get it to print out each object within the carShop[] array, however when the loop is running it duplicates the last run loop for each object in the array. what I mean to say is this. Say I run the loop 2 times for 2 cars, and i put honda, accord, 1999, $14000 for the first run of the loop for the first car and i put toyota, corolla, 2001 $17000 for the send run of the loop for the second car. When the array dat prints, carShop[0] shows toyota, corolla, 2001, $17000 and carShop[1] shows toyota, corolla, 2001, $17000, it skips the first instance of the loop.

          I am not allowed to change the Car.java file.
          I also can not use anything but the main() method in the CarDealerApp.ja va file.

          Can anyone help me with what I may be missing! Thanks in advance ALI :P

          Comment

          • nomad
            Recognized Expert Contributor
            • Mar 2007
            • 664

            #6
            Originally posted by AZRebelCowgirl7 3
            Ok here is what I have So far:

            //this is the car.java file that must be used when running the CarDealerApp.ja va
            //file
            import java.util.*;
            import java.net.*;
            import java.lang.*;

            public class Car{
            public static String make;
            public static String model;
            public static int year;
            public static int price;
            //Car(); constructor with parameters
            public Car(String m, String d, int y, int p) {
            make = m;
            model = d;
            year = y;
            price = p;
            }//end constructor
            // return make of car (make accessor)
            public String getMake() {
            return make;
            }//end method
            //return model of car (model accessor)
            public String getModel() {
            return model;
            }//end method
            //return year of car (year accessor)
            public int getYear() {
            return year;
            }//end method
            //return price of car (price accessor)
            public int getPrice() {
            return price;
            }//end method
            //print method
            public void print() {
            System.out.prin tln("Make = " + getMake() + "," + " Model = " + getModel() + "," + " Year = " + getYear() + "," + " Price = $" + getPrice());
            }//end method
            }

            // This is the CarDealerApp.ja va file
            import java.util.*;
            import java.net.*;
            import java.lang.*;
            public class CarDealerApp{
            public static void main(String[] args) {
            //declare array of Car objects. Array name is carShop
            final int MAX_SIZE = 100;
            Car carShop[] = new Car[MAX_SIZE];
            Scanner stdin = new Scanner(System. in);
            System.out.prin tln();
            System.out.prin t("Please input the number of effective cars currently in the shop?: ");
            int numberOfCars = stdin.nextInt() ;
            for(int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
            System.out.prin tln();
            System.out.prin t("What is the make of your car? ");
            String m = stdin.next();
            System.out.prin t("What is the model of your car? ");
            String d = stdin.next();
            System.out.prin t("What is the Year of your car? ");
            int y = stdin.nextInt() ;
            System.out.prin t("What is the Price of your car? $");
            int p = stdin.nextInt() ;
            Car C = new Car(m, d, y, p);
            carShop = C;
            System.out.prin tln();
            }
            for (int i= 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
            [i]carShop.print();
            }
            }
            }

            Ok now when I run this program I get it to print out each object within the carShop[] array, however when the loop is running it duplicates the last run loop for each object in the array. what I mean to say is this. Say I run the loop 2 times for 2 cars, and i put honda, accord, 1999, $14000 for the first run of the loop for the first car and i put toyota, corolla, 2001 $17000 for the send run of the loop for the second car. When the array dat prints, carShop[0] shows toyota, corolla, 2001, $17000 and carShop[1] shows toyota, corolla, 2001, $17000, it skips the first instance of the loop.

            I am not allowed to change the Car.java file.
            I also can not use anything but the main() method in the CarDealerApp.ja va file.

            Can anyone help me with what I may be missing! Thanks in advance ALI :P


            you are reinitialized the arrary
            This will print after each loop
            Delete this part

            Code:
             carShop = C; 
            System.out.println();
            } 
            for (int i= 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
            carShop[i].print();[/i]
            [i]} [/i]
            [i]}[/i]
            [i]} [/i]
            [i][/i]




            add this part
            carShop[i] = C;
            System.out.prin tln();
            Car.Carprint();
            }
            }
            }



            nomad

            Comment

            • AZRebelCowgirl73
              New Member
              • Nov 2006
              • 47

              #7
              Originally posted by nomad
              [/i]

              you are reinitialized the arrary
              This will print after each loop
              Delete this part

              Code:
               carShop = C; 
              System.out.println();
              } 
              for (int i= 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
              carShop[i].print();[/i]
              [i]} [/i]
              [i]}[/i]
              [i]} [/i]
              [i][/i]




              add this part
              carShop[i] = C;
              System.out.prin tln();
              Car.Carprint();
              }
              }
              }


              nomad
              After the loop is finished is when I am suppose to create a instance that prints the contents of the carShop[] array? adding the section you suggested would print each loop as it finished and that would be insufficient for the exercise! Any other ideas and Thanks so much for the help! ALI :P

              Comment

              • shanakard
                New Member
                • May 2007
                • 13

                #8
                Code:
                public class Car{
                public static String make;
                public static String model;
                public static int year;
                public static int price;
                //Car(); constructor with parameters
                public Car(String m, String d, int y, int p) {
                make = m; ......
                U see the above code all the attributes of the class Car are "static" (i.e:- public static String make)

                the thing with static variables are it only keeps one memory location to keep it's data for all the instances of that class. this could be used in cases to count the number of instences of a class (as shown in bellow code) like thing or many other uses.... Actually static variables are an important concept when we wan't to share data among all instances of the same class;

                Code:
                public class Test{
                   private static int count;
                   
                   public Test(){
                       count+=1;
                   }
                   
                   public void finalize(){
                       count--;
                   }
                   
                   public static int getCount(){
                        return count;
                   }
                
                   public static void main(String[] args){
                       Test a,b,c;
                       
                       System.out.println("Count before any object is created is "+Test.getCount());     //don't need a instance of the class to access static variables
                       a=new Test();
                       
                       System.out.println("Count after 'a=new Test()' is "+Test.getCount());
                       b= new Test();
                       c=new Test();
                       
                       System.out.println("Count after 2 new objects are created is "+c.getCount());    //In java we can also use a instance of the class to access static members
                       //if u ever used Integer.parseint() method haven't u wondered where the Integer part came from, obviously it's not any object u created. actually it's a static method in Integer class'
                   
                       a=null;
                       System.gc();  //invokes the garbage collector
                       
                       System.out.println("Count after object pointed to by 'a' is destroied is "+Test.getCount());
                   }
                            
                }
                in ur example since the Car classes members are static they only keep a single set of data for all the instances of the car objects.
                so unless u change the car class what u request is not possible in java !!!

                hope this helps U ;-)

                Comment

                • AZRebelCowgirl73
                  New Member
                  • Nov 2006
                  • 47

                  #9
                  Originally posted by shanakard
                  Code:
                  public class Car{
                  public static String make;
                  public static String model;
                  public static int year;
                  public static int price;
                  //Car(); constructor with parameters
                  public Car(String m, String d, int y, int p) {
                  make = m; ......
                  U see the above code all the attributes of the class Car are "static" (i.e:- public static String make)

                  the thing with static variables are it only keeps one memory location to keep it's data for all the instances of that class. this could be used in cases to count the number of instences of a class (as shown in bellow code) like thing or many other uses.... Actually static variables are an important concept when we wan't to share data among all instances of the same class;

                  in ur example since the Car classes members are static they only keep a single set of data for all the instances of the car objects.
                  so unless u change the car class what u request is not possible in java !!!

                  hope this helps U ;-)
                  You are my HERO :)) Thanks so much! But now I face a new problem!
                  Thanks so MUCH!!!! ALI :P

                  Comment

                  • AZRebelCowgirl73
                    New Member
                    • Nov 2006
                    • 47

                    #10
                    Here is my problem:

                    I have two java files:

                    One named Car.java and the other named CarDealerApp.ja va: In the CarDealerApp program, I read in through user input the make, model, year and price of a car and put them into an array called carShop[]. This part of the program is running fine. Every thing in the CarDealerApp.ja va file runs correctly except for the last loop. I need to read in through user input the make of a car and then search the array carShop[] for the make and subtract $1000 from the price listed for all instances of that particular make within the array. I cant seem to make the code work, the user input part does but the for loop does not! Can anyone help! Here is what I have so far: Thanks in advance!! ALI :P

                    import java.util.*;
                    import java.net.*;
                    import java.lang.*;
                    public class Car{

                    public String make;
                    public String model;
                    public int year;
                    public int price;

                    //Car(); constructor with parameters
                    public Car(String m, String d, int y, int p) {
                    make = m;
                    model = d;
                    year = y;
                    price = p;
                    }//end constructor

                    // return make of car (make accessor)
                    public String getMake() {
                    return make;
                    }//end method

                    //return model of car (model accessor)
                    public String getModel() {
                    return model;
                    }//end method

                    //return year of car (year accessor)
                    public int getYear() {
                    return year;
                    }//end method

                    //return price of car (price accessor)
                    public int getPrice() {
                    return price;
                    }//end method

                    //set new price of car (price mutator)
                    public void setPrice(int iPrice) {
                    price = iPrice;
                    }//end method

                    //print method
                    public void print() {
                    System.out.prin tln("Make = " + getMake() + "," + " Model = " + getModel() + "," + " Year = " + getYear() + "," + " Price = $" + getPrice());
                    }//end method
                    }

                    import java.util.*;
                    import java.net.*;
                    import java.lang.*;

                    public class CarDealerApp{

                    public static void main(String[] args) {

                    //declare array of Car objects. Array name is carShop
                    final int MAX_SIZE = 100;
                    Car carShop[] = new Car[MAX_SIZE];

                    Scanner stdin = new Scanner(System. in);

                    System.out.prin t("Please input the number of effective cars currently in the shop?: ");
                    int numberOfCars = stdin.nextInt() ;

                    for(int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
                    System.out.prin tln();
                    System.out.prin t("What is the make of your car? ");
                    String m = stdin.next();
                    System.out.prin t("What is the model of your car? ");
                    String d = stdin.next();
                    System.out.prin t("What is the Year of your car? ");
                    int y = stdin.nextInt() ;
                    System.out.prin t("What is the Price of your car? $");
                    int p = stdin.nextInt() ;
                    Car C = new Car(m, d, y, p);
                    carShop[i] = C;
                    }
                    System.out.prin tln();
                    System.out.prin tln(" The effective cars currently in the shop are: ");
                    System.out.prin tln();
                    for (int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
                    carShop[i].print();
                    }
                    System.out.prin tln("The Price of all effective cars currently in the shop is: ");
                    int prices[] = new int[numberOfCars];
                    int dataSum = 0;
                    for (int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
                    prices[i]= carShop[i].getPrice();
                    dataSum += prices[i];
                    }
                    System.out.prin tln(" Total Price = $" + dataSum);
                    System.out.prin tln(" Search the shop for car below a certain price. ");
                    System.out.prin tln();
                    System.out.prin t("Please enter the price to search for: $ ");
                    int searchPrice = stdin.nextInt() ;
                    System.out.prin tln("All cars in the shop with a price of: $" + searchPrice + " or lower are: ");
                    for (int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
                    if (carShop[i].getPrice() <= searchPrice) {
                    carShop[i].print();
                    }
                    }
                    System.out.prin tln(" Search the shop for car of a certain Make ");
                    System.out.prin tln(" and reduce their price by $1000. ");
                    System.out.prin tln();
                    System.out.prin t("Please enter the make to search for: ");
                    String searchMake = stdin.next();
                    System.out.prin tln();
                    for (int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
                    if (carShop[i].getMake() == searchMake) {
                    int iPrice = carShop[i].getPrice() - 1000;
                    carShop[i].setPrice(iPric e);
                    carShop[i].print();

                    }
                    }
                    }
                    }

                    Comment

                    • nomad
                      Recognized Expert Contributor
                      • Mar 2007
                      • 664

                      #11
                      Originally posted by AZRebelCowgirl7 3
                      Here is my problem:

                      I have two java files:

                      One named Car.java and the other named CarDealerApp.ja va: In the CarDealerApp program, I read in through user input the make, model, year and price of a car and put them into an array called carShop[]. This part of the program is running fine. Every thing in the CarDealerApp.ja va file runs correctly except for the last loop. I need to read in through user input the make of a car and then search the array carShop[] for the make and subtract $1000 from the price listed for all instances of that particular make within the array. I cant seem to make the code work, the user input part does but the for loop does not! Can anyone help! Here is what I have so far: Thanks in advance!! ALI :P

                      import java.util.*;
                      import java.net.*;
                      import java.lang.*;
                      public class Car{

                      public String make;
                      public String model;
                      public int year;
                      public int price;

                      //Car(); constructor with parameters
                      public Car(String m, String d, int y, int p) {
                      make = m;
                      model = d;
                      year = y;
                      price = p;
                      }//end constructor

                      // return make of car (make accessor)
                      public String getMake() {
                      return make;
                      }//end method

                      //return model of car (model accessor)
                      public String getModel() {
                      return model;
                      }//end method

                      //return year of car (year accessor)
                      public int getYear() {
                      return year;
                      }//end method

                      //return price of car (price accessor)
                      public int getPrice() {
                      return price;
                      }//end method

                      //set new price of car (price mutator)
                      public void setPrice(int iPrice) {
                      price = iPrice;
                      }//end method

                      //print method
                      public void print() {
                      System.out.prin tln("Make = " + getMake() + "," + " Model = " + getModel() + "," + " Year = " + getYear() + "," + " Price = $" + getPrice());
                      }//end method
                      }

                      import java.util.*;
                      import java.net.*;
                      import java.lang.*;

                      public class CarDealerApp{

                      public static void main(String[] args) {

                      //declare array of Car objects. Array name is carShop
                      final int MAX_SIZE = 100;
                      Car carShop[] = new Car[MAX_SIZE];

                      Scanner stdin = new Scanner(System. in);

                      System.out.prin t("Please input the number of effective cars currently in the shop?: ");
                      int numberOfCars = stdin.nextInt() ;

                      for(int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
                      System.out.prin tln();
                      System.out.prin t("What is the make of your car? ");
                      String m = stdin.next();
                      System.out.prin t("What is the model of your car? ");
                      String d = stdin.next();
                      System.out.prin t("What is the Year of your car? ");
                      int y = stdin.nextInt() ;
                      System.out.prin t("What is the Price of your car? $");
                      int p = stdin.nextInt() ;
                      Car C = new Car(m, d, y, p);
                      carShop[i] = C;
                      }
                      System.out.prin tln();
                      System.out.prin tln(" The effective cars currently in the shop are: ");
                      System.out.prin tln();
                      for (int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
                      carShop[i].print();
                      }
                      System.out.prin tln("The Price of all effective cars currently in the shop is: ");
                      int prices[] = new int[numberOfCars];
                      int dataSum = 0;
                      for (int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
                      prices[i]= carShop[i].getPrice();
                      dataSum += prices[i];
                      }
                      System.out.prin tln(" Total Price = $" + dataSum);
                      System.out.prin tln(" Search the shop for car below a certain price. ");
                      System.out.prin tln();
                      System.out.prin t("Please enter the price to search for: $ ");
                      int searchPrice = stdin.nextInt() ;
                      System.out.prin tln("All cars in the shop with a price of: $" + searchPrice + " or lower are: ");
                      for (int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
                      if (carShop[i].getPrice() <= searchPrice) {
                      carShop[i].print();
                      }
                      }
                      System.out.prin tln(" Search the shop for car of a certain Make ");
                      System.out.prin tln(" and reduce their price by $1000. ");
                      System.out.prin tln();
                      System.out.prin t("Please enter the make to search for: ");
                      String searchMake = stdin.next();
                      System.out.prin tln();
                      for (int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
                      if (carShop[i].getMake() == searchMake) {
                      int iPrice = carShop[i].getPrice() - 1000;
                      carShop[i].setPrice(iPric e);
                      carShop[i].print();

                      }
                      }
                      }
                      }
                      When you are searching for a model is their a particular model you are looking for. If not then it all you have to do is minus 1000 for all models.
                      Search for just the model of the car then print that model with the price minus 1000.

                      nomad

                      Comment

                      • AZRebelCowgirl73
                        New Member
                        • Nov 2006
                        • 47

                        #12
                        Originally posted by nomad
                        When you are searching for a model is their a particular model you are looking for. If not then it all you have to do is minus 1000 for all models.
                        Search for just the model of the car then print that model with the price minus 1000.

                        nomad

                        No I actually have to ask the user to input the model to search for then reduce the price by $1000. The price reduction only appliees to the car make inputted by the user. Thanks!!!! ALI :P

                        Comment

                        Working...