8 Replies - 294 Views - Last Post: 03 February 2013 - 11:25 AM Rate Topic: -----

#1 jcutch03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 31-January 13

My value always outputs as 0 because I had to initialize it

Posted 31 January 2013 - 08:51 PM

This is my first post on here. This is for my homework but I am not asking anyone to do it for me. My code does not have any compile errors at the moment. My problem is that when I run the program the roomTotal is always zero. I think it has something to do with the initialization. But, if I do not intialize it an error will occur. Does anyone have any suggestions on how I can get this to output the actual room total.

import javax.swing.JOptionPane;	//Needed for JOptionPane
import java.text.DecimalFormat;	//Needed for DecimalFormat Class

public class Cruise 
{

    public static void main(String[] args)
    {	
    	
    	//Input Variables
    	String customerNumber;	//To hold customers ID number
    	int roomType;		//To hold room Type
    	int roomPeople;		//To hold the number of people staying in the room
    	double roomCharge;	//To hold the room charges before possible discount
    	double roomDiscount;	//To hold excursion discount
    	int excursionNum;	//To hold the number of excursions that the  customer will go on
    	int excursionPeople;	//To hold the number of people that will be going on the excursions
    	double excursionCharge;	//To hold the excursion charges before possible discount
    	double excursionDiscount;	//To hold excursion discount
    	int internetUse;	//To hold if the user wants Internet use in their room
    	int internetDays;	//To hold the number of days that the user wants Internet in their room
    	int sodaCard;		//To hold whether the user wants a soda card
    	int sodaPeople;		//To hold the number of people that want soda cards in the customer's room
    	String input;		//To hold keyboard input
    	
    	//Ouput Variables
    	double excursionTotal;	//To hold the excursion total after possible discount
    	double internetTotal;	//To hold total Internet expense
    	double sodaTotal;		//To hold total soda card expense
    	double cruiseTotal;		//To hold the total expenses for the room
    	double roomTotal=0;		//To hold the total room expenses
    	//Get user customer number
    	customerNumber=JOptionPane.showInputDialog("What is your customerNumber?");
    
    	//Get customers room selection
    	input=JOptionPane.showInputDialog("What type of room would you like to book?\n" +
    									"Press 0 for a state room which costs $649.00 per person.\n" +
    									"Press 1 for a ocean view room which costs $749.00 per person.\n" +
    									"Press 2 for a room with a balacony which costs $1,069.00 per person\n" +
    									"Press 3 for a suite which costs $1499.00 per person");
    	roomType=Integer.parseInt(input);
    	//Get the number of people that will be staying in the room
    	input=JOptionPane.showInputDialog("How many people will be staying in your room?  There is a \n" +
    										"maximum of five people per room.\n" +
    										"Press 0 if there will be one person staying in your room.\n"+
    										"Press 1 if there will be two people staying in your room.\n"+
    										"Press 2 if there will be three people staying in your room.\n"+
    										"Press 3 if there will be four people staying in your room.\n"+
    										"Press 4 if there will be five people staying in your room.\n");
    	roomPeople=Integer.parseInt(input);
    	
		//Get the number of excursions the customer will be going on
    	input=JOptionPane.showInputDialog("The excursions that the cruise offers are:\n" +
    										"*Jet skiing\n*Snorkeling\n*Parasailing\n*Swimming with dolphins\n" +
    										"*Helmet\n All excursions have a flat rate of $180.00 per person.\n " +
    										"There is a limit of four excursions per room.  How many excursions will you be going on? ");
    	excursionNum=Integer.parseInt(input);
    	
    	//Get the number of people that will be attending the excursions
    	input=JOptionPane.showInputDialog("How many people will be going on the excursions with you?");
    	excursionPeople=Integer.parseInt(input);
    	
    	//See is the user would like to have Internet access on the cruise
    	input=JOptionPane.showInputDialog("Would you like to have Internet access?\n"+
    										"Press 0 for no and 1 for yes:");
    	internetUse=Integer.parseInt(input);
    	
    	//Get the number of days that the user would like to have Internet on the cruise
    	input=JOptionPane.showInputDialog("The Internet costs $15.00 per day.\n" +
    									"How many days would you like to have Internet access?");
    	internetDays=Integer.parseInt(input);
    	
    	//See if the user would like a soda card on the cruise
    	input=JOptionPane.showInputDialog("The cruise line offers a soda card for $25.00 per person\n"+
    										"The soda card allows the card holder to have unlimited soda\n"+
    										"for the entire week that you are on the cruse.\n"+
    										"Would you like to get a soda card?\n" +
    										"Enter 0  for no and 1 for yes:");
    	sodaCard=Integer.parseInt(input);
    	
    	//See how many people will be needing soda cards on the cruise
    	input=JOptionPane.showInputDialog("How many soda cards will you be needing for the people in your room?");
    	sodaPeople=Integer.parseInt(input);
    	
    	//Call methods to perform calculations
    	roomCharge=calculateRoomCharge(roomType,roomPeople);
    	excursionCharge=calculateExcursionCharge(excursionNum, excursionPeople);
    	internetTotal=calculateInternetTotal(internetUse, internetDays);
    	sodaTotal=calculateSodaTotal(sodaCard, sodaPeople);
    	
    	//Call method to diplay results
    	displayResults(customerNumber, roomTotal,excursionCharge, internetTotal, sodaTotal);   	
    	System.exit(0);
    }
     /** The calculateRoomCharge method will be used to calculate the total charge
     *for the room
     *@param roomType The type of room
     *@param roomPeople The number of people sharing the room
     *@return roomTotal The total cost of the room with a potential dicount
    */
    public static double calculateRoomCharge(int roomType, int roomPeople)
    {
    	double roomCharge;
    	double roomDiscount;
		double roomTotal;
		
    	if(roomType==0 && roomPeople==0)
    	{
    		roomCharge=649*1;
    	}
    	else if(roomType==0 && roomPeople==1)
    	{
    		roomCharge=649*2;
    	}
    	else if(roomType==0 && roomPeople==2)
    	{
    		roomCharge=649*3;
    	}
    	else if(roomType==0 && roomPeople==3)
    	{
    		roomCharge=649*4;
    	}
    	else if(roomType==0 && roomPeople==4)
    	{
    		roomCharge=649*5;
    	}
    	else if (roomType==1 && roomPeople==0)
    	{
    		roomCharge=749*1;
    	}
    	else if(roomType==1 && roomPeople==1)
    	{
    		roomCharge=749*2;
    	}
    	else if(roomType==1 && roomPeople==2)
    	{
    		roomCharge=749*3;
    	}
    	else if(roomType==1 && roomPeople==3)
    	{
    		roomCharge=749*4;
    	}
    	else if(roomType==1 && roomPeople==4)
    	{
    		roomCharge=749*5;
    	}
    	else if(roomType==2 && roomPeople==0)
    	{
    		roomCharge=1069*1;
    	}
    	else if(roomType==2 && roomPeople==1)
    	{
    		roomCharge=1069*2;
    	}
    	else if(roomType==2 && roomPeople==2)
    	{
    		roomCharge=1069*3;
    	}
    	else if(roomType==2 && roomPeople==3)
    	{
    		roomCharge=1069*4;
    	}
    	else if(roomType==2 && roomPeople==4)
    	{
    		roomCharge=1069*5;
    	}
    		else if(roomType==3 && roomPeople==0)
    	{
    		roomCharge=1499*1;
    	}
    	else if(roomType==3 && roomPeople==1)
    	{
    		roomCharge=1499*2;
    	}
    	else if(roomType==3 && roomPeople==2)
    	{
    		roomCharge=1499*3;
    	}
    	else if(roomType==3 && roomPeople==3)
    	{
    		roomCharge=1499*4;
    	}
    	else if(roomType==3 && roomPeople==4)
    	{
    		roomCharge=1499*5;
    	}
    	else
    	{
    		JOptionPane.showMessageDialog(null, "Invalid Data!");
    		roomCharge=7495;
    	}	
    	if(roomPeople==3)
    	{
    		roomDiscount= (roomCharge *.10);
    		roomTotal=roomCharge-roomDiscount;
    	}
    	else if(roomPeople>=4)
    	{
    		roomDiscount= (roomCharge *.15);
    		roomTotal=roomCharge-roomDiscount;
    	}
    	else
    	{
    		roomTotal=roomCharge;
    	}

    	return roomTotal;
    }
   	/** The calculateExcursionCharge method will be used to calculate the total charge
     *for the excursions
     *@param excursionNum Number of excursions the customer plans on going on
     *@return excursionCharge The cost of the excursion with a potential discount
    */
    public static double calculateExcursionCharge(int excursionNum, int excursionPeople)
    {
    	double excursionCharge;
    	double excursionDiscount;
    	double excursionTotal;
    
    	if(excursionNum==1)
    	{
    		excursionCharge=180*excursionPeople;
    	}
    	else if(excursionNum==2)
    	{
    		excursionCharge=360*excursionPeople;
    	}
    	else if(excursionNum==3)
    	{
    		excursionCharge=540*excursionPeople;
    	}
    	else if(excursionNum==4)
    	{
    		excursionCharge=720*excursionPeople;
    	}
    	else
    	{
    		JOptionPane.showMessageDialog(null, "Invalid Data!");
    		excursionCharge=720;
    	}
        	if(excursionNum==2)
    	{
    		excursionDiscount= (excursionCharge*.15);
    		excursionTotal=excursionCharge-excursionDiscount;
    	}
    	else if(excursionNum>=3)
    	{
    		excursionDiscount= (excursionCharge*.20);
    		excursionTotal= excursionCharge-excursionDiscount;
    	}
    	else
    	{
    		excursionTotal=excursionCharge;
    	}
    	return excursionTotal;
    }

     /** The calculateInternetTotal method will be used to calculate the total charge
     *for the Internet
     *@param internetUse How many days the Internet will be needed
     *@return internetTotal The total Internet charge, which is the internetDays multiplied by the cost of the Internet
    */
    public static double calculateInternetTotal(int internetUse, int internetDays)
    {
    double internetTotal;
    	if(internetUse==1)
 	{
 		internetTotal=internetDays*15;
 	}
 	else if(internetUse==0)
 	{
 		internetTotal=internetDays*0;
 	}
 	else
 	{
 		JOptionPane.showMessageDialog(null, "Invalid Data.");
 		internetTotal=0;
 	}
 	return internetTotal;
    }
 	 /** The calculatesodaTotal method will be used to calculate the total charge
     *for the soda card
     *@param sodaCard If customer wants to purchase soda cards
     *@return sodaTotal The total cost of the soda cards, which is the number of people who want one multiplied by 25
    */
    public static double calculateSodaTotal(int sodaCard, int sodaPeople)
    {
    double sodaTotal;
    
 	if(sodaCard==1)
 	{
 		sodaTotal=sodaPeople * 25.00;
 	}
 	else if(sodaCard==0)
 	{
 		sodaTotal= sodaPeople * 0;
 	}
 	else
 	{
 		JOptionPane.showMessageDialog(null, "Invalid Data.");
 		sodaTotal=0;
 	}
 	return sodaTotal;
    }
    /**The displayResults method outputs the results
     *@param customerID The customer ID number
 	 *@param roomTotal The total charge for the room
 	 *@param excursionTotal The total charge for the excursions
 	 *@param internetTotal The total charge for Internet use
 	 *@param sodaTotal
 	 *@param cruiseTotal The total charge for the cruise which if the roomTotal added to the excursion total added to the Internet
 	 *total added to the soda total
 	*/ 
 	public static void displayResults(String customerNumber, double roomTotal, double excursionTotal, double internetTotal, double sodaTotal)
 	{
 	double cruiseTotal;
 	
 	DecimalFormat formatter=new DecimalFormat("#0.00");
 	
 	//Calculate the total charge
 	cruiseTotal=roomTotal+excursionTotal+internetTotal+sodaTotal;
 	
 	//Output the customers expenses for the cruise
 	JOptionPane.showMessageDialog(null, "The cruise expenses for customer number " + customerNumber +
 									" for McCutcheon cruise line is as follows: ");
 	JOptionPane.showMessageDialog(null, "The total charge for the room is $" + formatter.format(roomTotal));
   	JOptionPane.showMessageDialog(null, "The total charge for the excusrions is $" + formatter.format(excursionTotal));
   	JOptionPane.showMessageDialog(null, "The total charge for the Internet is $" + formatter.format(internetTotal));
   	JOptionPane.showMessageDialog(null, "The total charge for the sodaCard is $" + formatter.format(sodaTotal));
 	JOptionPane.showMessageDialog(null, "The total charge for your cruise expenses is $" + formatter.format(cruiseTotal));
 	
    	
    	
    	}
    		
    		
    	
    }
    
 



Is This A Good Question/Topic? 0
  • +

Replies To: My value always outputs as 0 because I had to initialize it

#2 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2089
  • View blog
  • Posts: 3,181
  • Joined: 21-June 11

Re: My value always outputs as 0 because I had to initialize it

Posted 31 January 2013 - 09:30 PM

What else would roomTotal be? You set it to 0 and then you never change its value.

Note that if Java did not force you to initialize local variables, roomTotal's value would either be 0 or a random value depending on how Java decided to handle uninitialized local variables. Either way it would not have the value you want it to have, because you never set it to that value.

This post has been edited by sepp2k: 31 January 2013 - 09:30 PM

Was This Post Helpful? 0
  • +
  • -

#3 jcutch03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 31-January 13

Re: My value always outputs as 0 because I had to initialize it

Posted 31 January 2013 - 09:49 PM

View Postsepp2k, on 31 January 2013 - 09:30 PM, said:

What else would roomTotal be? You set it to 0 and then you never change its value.

Note that if Java did not force you to initialize local variables, roomTotal's value would either be 0 or a random value depending on how Java decided to handle uninitialized local variables. Either way it would not have the value you want it to have, because you never set it to that value.

I thought I was reassigning a new number to the variable in the calculateRoomCharge method...How do you suggest I go about fixing this. I am trying to make the roomTotal the roomCharge-the potential discount .
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2089
  • View blog
  • Posts: 3,181
  • Joined: 21-June 11

Re: My value always outputs as 0 because I had to initialize it

Posted 31 January 2013 - 09:56 PM

In the calculateRoomCharge method you're assigning to the variable you've defined on line 105, which is local to that method. That's a completely different variable than the one defined on line 31, which is local to main.

Since you're returning roomTotal from the calculateRoomCharge method, you should simply make use of that return value.
Was This Post Helpful? 0
  • +
  • -

#5 jcutch03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 31-January 13

Re: My value always outputs as 0 because I had to initialize it

Posted 03 February 2013 - 10:40 AM

If I delete the roomTotal variable that is the local variable to the calculate roomChargeMethod I get an error saying that the roomTotal variable has not been declared.
Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2089
  • View blog
  • Posts: 3,181
  • Joined: 21-June 11

Re: My value always outputs as 0 because I had to initialize it

Posted 03 February 2013 - 10:54 AM

Which is true. The roomTotal variable that you define in main is local to main. You can't assign to it from other methods.

It appears like you don't understand local variables very well. Basically any reassignment you perform on local variables inside a method will not be visible from within any other method. So if you have two local variables with the same name in different methods, changing one will not affect the other because they're independent variables. And if you don't have a local variable with a given name in a method, then you can't access that name at all¹ - no matter whether another method has a local variable with a name like that or not.

As I said earlier, you're returning the value of the variable from the method, so you should use that return value.

¹ Assuming there are no static or, if the method is an instance method, instance variables with that name, which in this case, there aren't.

This post has been edited by sepp2k: 03 February 2013 - 11:13 AM

Was This Post Helpful? 0
  • +
  • -

#7 jcutch03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 31-January 13

Re: My value always outputs as 0 because I had to initialize it

Posted 03 February 2013 - 11:06 AM

Okay, first I want to apologize for being a pain. I just really am not understanding how I got the excursionTotal to output correctly, yet I cant get the roomTotal to. I have gone over this code a million times and I just dont get what I need to change to make this work. I understand that the variable defined in a method is local to the method and is not visible outside the method. I just do not understand what I need to change to make this work. Thank you so much for your time and help so far, I really do appreciate it. How do I use the return roomTotal variable I already have to output this correctly.

Firstly, I want to apologize for being such a pain. I feel like the answer is so simple and I am just not getting it. I also want to thank you for your patience and time so far, it is greatly appreciated. I understand that a variable declared in a method is only visible to that method. I guess I just do not know how to translate this into my code. How do I use the roomTotal value that I am returning to make the output correct.
Was This Post Helpful? 0
  • +
  • -

#8 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: My value always outputs as 0 because I had to initialize it

Posted 03 February 2013 - 11:21 AM

From a quick look, it seems the total charges are being calculated and stored in the roomCharge variable at this line (line 86):

roomCharge=calculateRoomCharge(roomType,roomPeople);

The results are then shown calling the method displayResults() at line 92. Try passing the variable roomCharge to displayResults() instead of roomTotal.

Oh, and if you want the total to indeed be stored in the variable roomTotal, then change line 86 to:

roomTotal=calculateRoomCharge(roomType,roomPeople);

and leave line 92 alone.

This post has been edited by GregBrannon: 03 February 2013 - 11:23 AM

Was This Post Helpful? 2
  • +
  • -

#9 jcutch03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 31-January 13

Re: My value always outputs as 0 because I had to initialize it

Posted 03 February 2013 - 11:25 AM

View PostGregBrannon, on 03 February 2013 - 11:21 AM, said:

From a quick look, it seems the total charges are being calculated and stored in the roomCharge variable at this line (line 86):

roomCharge=calculateRoomCharge(roomType,roomPeople);

The results are then shown calling the method displayResults() at line 92. Try passing the variable roomCharge to displayResults() instead of roomTotal.

Oh, and if you want the total to indeed be stored in the variable roomTotal, then change line 86 to:

roomTotal=calculateRoomCharge(roomType,roomPeople);

and leave line 92 alone.

OMG! THANK YOU SO MUCH! Everything is working perfectly! Your a life savor!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1