This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Two classes

 
Stephanie Dears
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This i my second Java class, and I must be missing some fundamental knowledge.

I have two classes. The main contains:


public class CarPartTest {

public static void main (String [] args) {

System.out.println("TESTING CarPart CLASS");

// instantiates two CarPart objects by calling parameter constructor
CarPart battery = new CarPart(3176, "battery", 15, 45.25);
CarPart ws = new CarPart(2745, "wind screen", 4, 175.60);

// 1. Testing object construction and print
System.out.println("\n1. Testing object construction and print ...");
System.out.print("Part data: ");
battery.print();

System.out.print("Part data: ");
ws.print();

My job is to write "a method print that writes out to the screen all car part's specific data" in another class. So far I had:

import java.io.*;
import java.awt.*;

public class CarPart {

int numItems;
int partID ;
String partName ;
int partStock ;
double partPrice ;

public CarPart() {


partID = 0;
String partName = "";
int partStock = 0;
double partPrice = 0;

} // end method CarPart

public String print(){
return partID;
return partName;
return partStock;
return partPrice;

} // end method print

For the print method is started out with:

import java.io.*;
import java.awt.*;

public class CarPart {

int numItems;
int partID ;
String partName ;
int partStock ;
double partPrice ;

public CarPart() {


partID = 0;
String partName = "";
int partStock = 0;
double partPrice = 0;

} // end method CarPart

public String print(){
return print;

} // end method print

which compiled fine but the instructor said was wrong.

I tried several other things, but nothing is working. Can anyone help?
 
Henry Wong
author
Marshal
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
which compiled fine but the instructor said was wrong.


You must not be showing us all of the code, because I don't see how some of methods could compile fine.

Henry
 
Stephanie Dears
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the entire code for the main method is:

public class CarPartTest {

public static void main (String [] args) {

System.out.println("TESTING CarPart CLASS");

// instantiates two CarPart objects by calling parameter constructor
CarPart battery = new CarPart(3176, "battery", 15, 45.25);
CarPart ws = new CarPart(2745, "wind screen", 4, 175.60);

// 1. Testing object construction and print
System.out.println("\n1. Testing object construction and print ...");
System.out.print("Part data: ");
battery.print();

System.out.print("Part data: ");
ws.print();

// 2. Testing class getters using the 'battery' object
System.out.println("\n2. Testing class getters using the 'battery' object ...");
int batID = battery.getPartID();
String batName = battery.getPartName();
int batStock = battery.getStock();
double batPrice = battery.getPartPrice();

System.out.print("Battery ID: ");
System.out.println(batID);

System.out.print("Battery name: ");
System.out.println(batName);

System.out.print("Battery stock: ");
System.out.println(batStock);

System.out.print("Battery price: ");
System.out.println(batPrice);

// 3. Testing addToStock
System.out.println("\n3. Testing addToStock...");
ws.addToStock(20);
System.out.print("Part data: ");
ws.print();

// 4. Testing remove (sell) 6 batteries (remove operation will be executed)
System.out.println("\n4. Testing remove (sell) 6 batteries - remove operation will be executed ...");
if(battery.removeFromStock(6)) {
System.out.print("The operation executed OK. Part data: ");
battery.print();
} else {
System.out.print("The operation could not be executed. Part data: ");
battery.print();
}

// 5. Testing remove (sell) 100 wind screens (the operation will not be executed)
System.out.println("\n5. Testing remove (sell) 100 wind screens - remove operation will not be executed ...");
if(ws.removeFromStock(100)) {
System.out.print("The operation executed OK. Part data: ");
ws.print();
} else {
System.out.print("The operation could not be executed. Part data: ");
ws.print();
}
}
}
 
Henry Wong
author
Marshal
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was referring to your CarPart class.

Henry
 
Stephanie Dears
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, sorry, this is the entire code:

import java.io.*;
import java.awt.*;

public class CarPart {

int numItems;
int partID ;
String partName ;
int partStock ;
double partPrice ;

public CarPart() {


partID = 0;
String partName = "";
int partStock = 0;
double partPrice = 0;

} // end method CarPart

public String print(){
return CarPart();

} // end method print

public int getPartID() {
return partID ;
} // end method getPartID

public String getPartName(){
return partName;
} // end method getPartName

public int getStock() {
return partStock;
} // end method getStock

public double getPartPrice() {
return partPrice ;
} // end method getPartPrice


public void addToStock (int item) {
numItems = item ;

} // end method addToStock

public boolean removeFromStock (int item) {
boolean moreToSearch;
int location = 0;
boolean found = false;

moreToSearch = (location < numItems);


location++;

item = location - 1;
numItems--;

location++;

moreToSearch = (location < numItems);

return found;
} // end method removeFromStock

} //end class
 
Henry Wong
author
Marshal
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the main code, it looks like your car part print() method is supposed to print something. However, all it does it just return a String. Is it supposed to print the String instead?

Other than that, could you elaborate the problem that you are having?

Henry
 
Stephanie Dears
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the CarPartTest is run, this is supposed to be the answer:

TESTING CarPart CLASS
1. Testing object construction and print ...
Part data: 3176, battery, 15, 45.25
Part data: 2745, wind screen, 4, 175.6
2. Testing class getters using the 'battery' object ...
Battery ID: 3176
Battery name: battery
Battery stock: 15
Battery price: 45.25
3. Testing addToStock...
Part data: 2745, wind screen, 24, 175.6
4. Testing remove (sell) 6 batteries � remove operation will be executed ...
The operation executed OK. Part data: 3176, battery, 9, 45.25
5. Testing remove (sell) 100 wind screens � remove operation will not be executed ...
The operation could not be executed. Part data: 2745, wind screen, 24, 175.6
 
Henry Wong
author
Marshal
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest that you modify the print() to actually print the values instead of just returning itself.

Henry
 
Stephanie Dears
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And how might I do that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic