473,499 Members | 1,618 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem adding a object to an array

47 New Member
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.println("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.java
* 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.println();
System.out.println(" CMIS 141 Project 3");
System.out.println(" File: Car.java");
System.out.println(" Created by: Annie (ALI) Ideus-Balboa");
System.out.println(" Due: June 3rd, 2007");
System.out.println();

System.out.print("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.println();
System.out.println("Your Car (Car c3):");
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();
System.out.print("What is the Year of your car? ");
int y = stdin.nextInt();
System.out.print("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.println(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
May 30 '07 #1
11 2433
nomad
664 Recognized Expert Contributor
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.println("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.java
* 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.println();
System.out.println(" CMIS 141 Project 3");
System.out.println(" File: Car.java");
System.out.println(" Created by: Annie (ALI) Ideus-Balboa");
System.out.println(" Due: June 3rd, 2007");
System.out.println();

System.out.print("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.println();
System.out.println("Your Car (Car c3):");
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();
System.out.print("What is the Year of your car? ");
int y = stdin.nextInt();
System.out.print("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.println(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
Expand|Select|Wrap|Line Numbers
  1.  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.
Expand|Select|Wrap|Line Numbers
  1.  
  2. System.out.print("What is the make of your car? ");    
  3.             String m = stdin.nextLine();
  4.             System.out.print("What is the model of your car? ");
  5.             String d = stdin.nextLine();
  6.  
Delete the Line, should look like this:
Expand|Select|Wrap|Line Numbers
  1.  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
May 30 '07 #2
blazedaces
284 Contributor
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.println("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.java
* 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.println();
System.out.println(" CMIS 141 Project 3");
System.out.println(" File: Car.java");
System.out.println(" Created by: Annie (ALI) Ideus-Balboa");
System.out.println(" Due: June 3rd, 2007");
System.out.println();

System.out.print("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.println();
System.out.println("Your Car (Car c3):");
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();
System.out.print("What is the Year of your car? ");
int y = stdin.nextInt();
System.out.print("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.println(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@locationInMemory, 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.println(carShop[i]);"

with

"carShop[i].print();"


Hope that solved your problem,
-blazed
May 30 '07 #3
shanakard
13 New Member
Expand|Select|Wrap|Line Numbers
  1. 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(Object 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.
May 30 '07 #4
AZRebelCowgirl73
47 New Member
Ok here is what I have So far:

//this is the car.java file that must be used when running the CarDealerApp.java
//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.println("Make = " + getMake() + "," + " Model = " + getModel() + "," + " Year = " + getYear() + "," + " Price = $" + getPrice());
}//end method
}

// This is the CarDealerApp.java 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.println();
System.out.print("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.println();
System.out.print("What is the make of your car? ");
String m = stdin.next();
System.out.print("What is the model of your car? ");
String d = stdin.next();
System.out.print("What is the Year of your car? ");
int y = stdin.nextInt();
System.out.print("What is the Price of your car? $");
int p = stdin.nextInt();
Car C = new Car(m, d, y, p);
carShop[i] = C;
System.out.println();
}
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.java file.

Can anyone help me with what I may be missing! Thanks in advance ALI :P
May 30 '07 #5
nomad
664 Recognized Expert Contributor
Ok here is what I have So far:

//this is the car.java file that must be used when running the CarDealerApp.java
//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.println("Make = " + getMake() + "," + " Model = " + getModel() + "," + " Year = " + getYear() + "," + " Price = $" + getPrice());
}//end method
}

// This is the CarDealerApp.java 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.println();
System.out.print("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.println();
System.out.print("What is the make of your car? ");
String m = stdin.next();
System.out.print("What is the model of your car? ");
String d = stdin.next();
System.out.print("What is the Year of your car? ");
int y = stdin.nextInt();
System.out.print("What is the Price of your car? $");
int p = stdin.nextInt();
Car C = new Car(m, d, y, p);
carShop = C;
System.out.println();
}
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.java 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

Expand|Select|Wrap|Line Numbers
  1.  carShop = C; 
  2. System.out.println();
  3. for (int i= 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
  4. carShop.print();
  5. }
  6.  




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



nomad
May 30 '07 #6
AZRebelCowgirl73
47 New Member
[/i]

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

Expand|Select|Wrap|Line Numbers
  1.  carShop = C; 
  2. System.out.println();
  3. for (int i= 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
  4. carShop.print();
  5. }
  6.  




add this part
carShop[i] = C;
System.out.println();
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
May 30 '07 #7
shanakard
13 New Member
Expand|Select|Wrap|Line Numbers
  1. public class Car{
  2. public static String make;
  3. public static String model;
  4. public static int year;
  5. public static int price;
  6. //Car(); constructor with parameters
  7. public Car(String m, String d, int y, int p) {
  8. 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;

Expand|Select|Wrap|Line Numbers
  1. public class Test{
  2.    private static int count;
  3.  
  4.    public Test(){
  5.        count+=1;
  6.    }
  7.  
  8.    public void finalize(){
  9.        count--;
  10.    }
  11.  
  12.    public static int getCount(){
  13.         return count;
  14.    }
  15.  
  16.    public static void main(String[] args){
  17.        Test a,b,c;
  18.  
  19.        System.out.println("Count before any object is created is "+Test.getCount());     //don't need a instance of the class to access static variables
  20.        a=new Test();
  21.  
  22.        System.out.println("Count after 'a=new Test()' is "+Test.getCount());
  23.        b= new Test();
  24.        c=new Test();
  25.  
  26.        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
  27.        //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'
  28.  
  29.        a=null;
  30.        System.gc();  //invokes the garbage collector
  31.  
  32.        System.out.println("Count after object pointed to by 'a' is destroied is "+Test.getCount());
  33.    }
  34.  
  35. }
  36.  
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 ;-)
May 31 '07 #8
AZRebelCowgirl73
47 New Member
Expand|Select|Wrap|Line Numbers
  1. public class Car{
  2. public static String make;
  3. public static String model;
  4. public static int year;
  5. public static int price;
  6. //Car(); constructor with parameters
  7. public Car(String m, String d, int y, int p) {
  8. 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
Jun 1 '07 #9
AZRebelCowgirl73
47 New Member
Here is my problem:

I have two java files:

One named Car.java and the other named CarDealerApp.java: 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.java 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.println("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.print("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.println();
System.out.print("What is the make of your car? ");
String m = stdin.next();
System.out.print("What is the model of your car? ");
String d = stdin.next();
System.out.print("What is the Year of your car? ");
int y = stdin.nextInt();
System.out.print("What is the Price of your car? $");
int p = stdin.nextInt();
Car C = new Car(m, d, y, p);
carShop[i] = C;
}
System.out.println();
System.out.println(" The effective cars currently in the shop are: ");
System.out.println();
for (int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
carShop[i].print();
}
System.out.println("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.println(" Total Price = $" + dataSum);
System.out.println(" Search the shop for car below a certain price. ");
System.out.println();
System.out.print("Please enter the price to search for: $ ");
int searchPrice = stdin.nextInt();
System.out.println("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.println(" Search the shop for car of a certain Make ");
System.out.println(" and reduce their price by $1000. ");
System.out.println();
System.out.print("Please enter the make to search for: ");
String searchMake = stdin.next();
System.out.println();
for (int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
if (carShop[i].getMake() == searchMake) {
int iPrice = carShop[i].getPrice() - 1000;
carShop[i].setPrice(iPrice);
carShop[i].print();

}
}
}
}
Jun 1 '07 #10
nomad
664 Recognized Expert Contributor
Here is my problem:

I have two java files:

One named Car.java and the other named CarDealerApp.java: 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.java 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.println("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.print("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.println();
System.out.print("What is the make of your car? ");
String m = stdin.next();
System.out.print("What is the model of your car? ");
String d = stdin.next();
System.out.print("What is the Year of your car? ");
int y = stdin.nextInt();
System.out.print("What is the Price of your car? $");
int p = stdin.nextInt();
Car C = new Car(m, d, y, p);
carShop[i] = C;
}
System.out.println();
System.out.println(" The effective cars currently in the shop are: ");
System.out.println();
for (int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
carShop[i].print();
}
System.out.println("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.println(" Total Price = $" + dataSum);
System.out.println(" Search the shop for car below a certain price. ");
System.out.println();
System.out.print("Please enter the price to search for: $ ");
int searchPrice = stdin.nextInt();
System.out.println("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.println(" Search the shop for car of a certain Make ");
System.out.println(" and reduce their price by $1000. ");
System.out.println();
System.out.print("Please enter the make to search for: ");
String searchMake = stdin.next();
System.out.println();
for (int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
if (carShop[i].getMake() == searchMake) {
int iPrice = carShop[i].getPrice() - 1000;
carShop[i].setPrice(iPrice);
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
Jun 1 '07 #11
AZRebelCowgirl73
47 New Member
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
Jun 1 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

6
3887
by: Naran Hirani | last post by:
Hi Guys and Gals, Please can u help me with this little problem. I am collecting a set of values from a web form and putting them into a JS array object. I now want to check/ensure that...
4
4791
by: Derek | last post by:
Hi, I've built a rather large CGI that dumps a lot of data and a fairly complex javascript app out to the client's browser. Granted this may be poor style according to someone web design...
4
1441
by: TheBurgerMan | last post by:
Hi all. I have an array of objects (Let's call it Object A) and each of these objects has an array of other objects (Let's call these Object B) in it. I can successfully repeat through Object A's...
1
3875
by: tangus via DotNetMonster.com | last post by:
Hello all, I'm really struggling with getting some Active Directory code to work in ASP.NET. Can you please provide assistance? I am executing the following code: Dim enTry As DirectoryEntry =...
0
3172
by: maitrepoy | last post by:
I have to create a small addin which works on Powerpoint, Word, Outlook, and Excel on Office 2000, XP, and 2003. This addin consists in adding 2 new Buttons in the "File" Menu of office. This is...
2
4426
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
7
1749
by: Trickynick1001 | last post by:
Hi, a newbie here. I don't have a real firm grasp on the idea of Javascript, as I'm used to programming in Qbasic and C. I'm not used to OOP. Anyway, I really don't have any idea what the...
1
2041
by: DJG79 | last post by:
Hi all, I am using an open source menu that i found and it works great, except for one thing that when the web page is not scrolled to the very top the drop down links will not stay visible. Has...
2
3131
by: sorobor | last post by:
dear sir .. i am using cakephp freamwork ..By the way i m begener in php and javascript .. My probs r bellow I made a javascript calender ..there is a close button ..when i press close button...
5
1480
by: Ranmini | last post by:
Dear prob solver, i have a problem with array lists. i am using array lists for adding objects. i just want to know, when you are adding an object to an arraylist whats the value u pass to the...
0
7009
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7178
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7390
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4602
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
302
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.