aspose file tools*
The moose likes Beginning Java and the fly likes Two classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Two classes" Watch "Two classes" New topic
Author

Two classes

Stephanie Dears
Ranch Hand

Joined: May 26, 2005
Posts: 43
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
Sheriff

Joined: Sep 28, 2004
Posts: 18842
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Stephanie Dears
Ranch Hand

Joined: May 26, 2005
Posts: 43
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
Sheriff

Joined: Sep 28, 2004
Posts: 18842
    
  40

I was referring to your CarPart class.

Henry
Stephanie Dears
Ranch Hand

Joined: May 26, 2005
Posts: 43
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
Sheriff

Joined: Sep 28, 2004
Posts: 18842
    
  40

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

Joined: May 26, 2005
Posts: 43
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
Sheriff

Joined: Sep 28, 2004
Posts: 18842
    
  40

I would suggest that you modify the print() to actually print the values instead of just returning itself.

Henry
Stephanie Dears
Ranch Hand

Joined: May 26, 2005
Posts: 43
And how might I do that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Two classes