9 Replies - 829 Views - Last Post: 02 November 2009 - 09:32 PM Rate Topic: -----

#1 yolifeisGOOD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 02-November 09

how do i get a return value

Posted 02 November 2009 - 08:28 PM

Write a class called Warehouse which holds radios, televisions, and computers. Assume that the following unit prices are given:

Radio: $49.00
Television: $375.00
Computer: $699.00

Note that these will be constants. Further, they will be available to ALL instances of the warehouse class. In addition, the class should have the following:
a. Instance variables: qtyRadio, qtyTelevision, and qtyComputer to keep track of the quantities of radios, televisions, and computers, respectively.
b. Two constructors: a default constructor starts the warehouse with no items (quantity will be 0 for all). The second constructor will allow one to create a warehouse object with specified initial quantities of radios, televisions, and computers.
c. Methods to increase the quantities of each of the items in the warehouse. These are:
addRadio(int x) adds x quantities to qtyRadio
addTV(int x) adds x quantities to qtyTelevision
addComputer(int x) adds x to qtyComputer
d. A toString() method that displays a warehouse object in the following format:
Number of radios: [display quantities of radios]
Number of televisions: [display quantities of televisions]
Number of computers: [display quantities of computers]
Total value of items: [ display ( qtyRadio * 49.00) + ( qtyTelevision * 375.00) + (qtyComputer * 699.00) ]


public class Warehouse {
	//these are instance variables
	public final double RADIO=49;
	public final double TELEVISION=375;
	public final double COMPUTER=699;
	
	public int qtyRadio = 0;
	public int qtyTelevision = 0;
	public int qtyComputer = 0;
	public double totalValue = 1;


	public int QWarehouse(){
		return qtyRadio;

//default constructor
	} 

	public int QWarehouse(int xRadio, int xTelevision, int xComputer){
		this.qtyRadio = xRadio;
		this.qtyTelevision = xTelevision;
		this.qtyComputer = xComputer;
		return qtyRadio;
	} 
		
	public int getRadio(){
		return qtyRadio;
	}
	
	public void addRadio(int xRadio){
		qtyRadio += xRadio;
			
	}   

	public int getTelevision(){
		return qtyTelevision;
	}
	
	public void addTelevision(int xTelevision){
		qtyTelevision += xTelevision;
	}   
   
   public int getComputer(){
		return qtyComputer;
	}
	
	public void addComputer(int xComputer){
		qtyComputer += xComputer;

	}   
	
	public double addtotalValue(double totalValue){
		totalValue = (qtyRadio * RADIO) + (qtyTelevision * TELEVISION) + (qtyComputer * COMPUTER);
		return totalValue;
		
	}
		
	public String toString(){
		String st;
		//integer type use %d, string is %s
		st=String.format("Number of radios: %d\nNumber of televisions: %d\nNumber of computers: %d\nTotal value of items: %s\n",
						qtyRadio,qtyTelevision,qtyComputer,totalValue);
		return st;
	} 
} 



and to test the warehouse class

public class WarehouseTest {
 
	public static void main(String[] args) {

	Warehouse test= new Warehouse();
		test.addRadio(2);
		test.addTelevision(3);
		test.addComputer(4);
		
		System.out.println(test);

	}
}




what did i do wrong? im missing the return value. it wont do the calculation

Is This A Good Question/Topic? 0
  • +

Replies To: how do i get a return value

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: how do i get a return value

Posted 02 November 2009 - 08:34 PM

Constructor names must match the class name and cannot return values. So this code:
	public int QWarehouse(){
		return qtyRadio;

//default constructor
	}

	public int QWarehouse(int xRadio, int xTelevision, int xComputer){
		 ...
	} 



needs to be changed to this:
	public Warehouse(){
		// Does nothing
	}

	public Warehouse(int xRadio, int xTelevision, int xComputer){
		this.qtyRadio = xRadio;
		this.qtyTelevision = xTelevision;
		this.qtyComputer = xComputer;
	} 



Then use and accessor method to get the current value of qtyRadio
public int getQtyRadio() {
	return qtyRadio;
}




Also, to be more object oriented change these vaiable to private and add acessor methods like the one I just showed you
	private int qtyRadio = 0;
	private int qtyTelevision = 0;
	private int qtyComputer = 0;
	private double totalValue = 1;




-- Cheers!

This post has been edited by Dogstopper: 02 November 2009 - 08:37 PM

Was This Post Helpful? 0
  • +
  • -

#3 yolifeisGOOD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 02-November 09

Re: how do i get a return value

Posted 02 November 2009 - 08:40 PM

ok i've fix that problem you just stated but i still need to find a way to return the TOTAL VALUE, i know it has to do something with this code here


 public double addtotalValue(double totalValue){
		totalValue = (qtyRadio * RADIO) + (qtyTelevision * TELEVISION) + (qtyComputer * COMPUTER);
		return totalValue;
		
	}



this is what i get when i run the program

Number of radios: 2
Number of televisions: 3
Number of computers: 4
Total value of items: 1.0


Process completed.

i want the total value of items to be (qtyRadio * RADIO) + (qtyTelevision * TELEVISION) + (qtyComputer * COMPUTER);

not 1.0
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: how do i get a return value

Posted 02 November 2009 - 08:49 PM

Ok I see.. . In your toString method, you are printing out total value before you actually get it. here would be your toString method correct:

public String toString(){
		String st;

		//GET TOTAL NUMBER OF UNITS
		totalValue = calculateTotalValue();

		//integer type use %d, string is %s
		st=String.format("Number of radios: %d\nNumber of televisions: %d\nNumber of computers: %d\nTotal value of items: %s\n",
						qtyRadio,qtyTelevision,qtyComputer,totalValue);
		return st;
	} 




Now, in THIS method:
		 public double addtotalValue(double totalValue){
		totalValue = (qtyRadio * RADIO) + (qtyTelevision * TELEVISION) + (qtyComputer * COMPUTER);
		return totalValue;
	   
	}



You need to rename it to fit the rest of your code, change it to this:
	public double calculateTotalValue () { 
		totalValue = (qtyRadio * RADIO) + (qtyTelevision * TELEVISION) + (qtyComputer * COMPUTER);
		return totalValue;
	   
	}




EDIT: Sorry, got the code backwards!

-- Cheers!

This post has been edited by Dogstopper: 02 November 2009 - 08:55 PM

Was This Post Helpful? 0
  • +
  • -

#5 yolifeisGOOD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 02-November 09

Re: how do i get a return value

Posted 02 November 2009 - 08:57 PM

yay CHEERS!!!

hey but one quick question

Warehouse test1 = new Warehouse();
		System.out.println("How many radio do you want to add to the warehouse?");
		int qtyRadio = input.nextInt();
		System.out.println("How many television do you want to add to the warehouse?");
		int qtyTelevision = input.nextInt();
		System.out.println("How many computer do you want to add to the warehouse?");
		int qtyComputer = input.nextInt();
		
		System.out.println(test1);



how do i take those input value and pass it to the warehouse class

i know int qtyRadio = input.nextInt(); is wrong but i don't know what the correct code is
Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: how do i get a return value

Posted 02 November 2009 - 09:07 PM

You would use the java.util.Scanner class

Scanner sc = new Scanner(System.in);
int i = sc.nextInt();



That would start a new Scanner object and then would read an integer from the command line!
Was This Post Helpful? 0
  • +
  • -

#7 yolifeisGOOD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 02-November 09

Re: how do i get a return value

Posted 02 November 2009 - 09:13 PM

sorry maybe im not stating my question clearly, i meant like how can i pass the input value to the Warehouse class,this is my code but when i run it would display 0 for all output

import java.util.*;
public class WarehouseTest {
 
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);

Warehouse test1 = new Warehouse();
		test1.getQtyRadio();
		System.out.println("How many radio do you want to add to the warehouse?");
		int qtyRadio = input.nextInt();
		
		test1.getQtyTelevision();
		System.out.println("How many television do you want to add to the warehouse?");
		int qtyTelevision = input.nextInt();
		
		test1.getQtyComputer();
		System.out.println("How many computer do you want to add to the warehouse?");
		int qtyComputer = input.nextInt();
		
		System.out.println(test1);


Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: how do i get a return value

Posted 02 November 2009 - 09:25 PM

Two ways to this at this point:
Way 1: get all the input and construct the object
import java.util.*;
public class WarehouseTest {
 
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);

		// Get all the user input
		System.out.println("How many radio do you want to add to the warehouse?");
		int qtyRadio = input.nextInt();
	   
		System.out.println("How many television do you want to add to the warehouse?");
		int qtyTelevision = input.nextInt();
	   
		System.out.println("How many computer do you want to add to the warehouse?");
		int qtyComputer = input.nextInt();

		// Now send that information via a constrctor
		Warehouse house = new Warehouse(qtyRadio, qtyTelevision, qtyComputer);  
	   
		System.out.println(house);
}



Way # 2: add them as you input
import java.util.*;
public class WarehouseTest {
 
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);

		Warehouse house = new Warehouse();  

		// Get the user input
		System.out.println("How many radio do you want to add to the warehouse?");
		int qtyRadio = input.nextInt();

		// add it
		house.addRadio(qtyRadio);
	   // And so on....
	   
		System.out.println("How many television do you want to add to the warehouse?");
		int qtyTelevision = input.nextInt();

		house.addTelevision(qtyTelevision);
	   
		System.out.println("How many computer do you want to add to the warehouse?");
		int qtyComputer = input.nextInt();

		house.addComputer(qtyComputer);
	   
		System.out.println(house);
}



There you go, hope you understand now!
Was This Post Helpful? 0
  • +
  • -

#9 yolifeisGOOD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 02-November 09

Re: how do i get a return value

Posted 02 November 2009 - 09:29 PM

thank you very much!!!
Was This Post Helpful? 0
  • +
  • -

#10 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: how do i get a return value

Posted 02 November 2009 - 09:32 PM

View PostyolifeisGOOD, on 2 Nov, 2009 - 10:29 PM, said:

thank you very much!!!


You are very Welcome.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1