Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 13, 2007, 04:46 PM   #1
sixstorm
macrumors regular
 
Join Date: Jan 2006
Location: Nashville, TN
Send a message via AIM to sixstorm
Some Java Advice . . .

Hello fellow programmers. I just wrote this super simple program and for some reason, JGrasp is having a hard time with my discount variables, saying "they may not have been initialized". Check the code out for yourself. I'm sure I'm just missing something but . . .

Also, the letters in bold are the letters that JGrasp is pointing out.

Code:
/* Jonathan Andrew Scott
	2/13/07
	jscottpass4.java
	This program is for a catering service in order to figure up the totals upon which the user
	will input.  See comment below for more details.
*/

import javax.swing.JOptionPane;

public class jscottpass4
{
	// Constants
	public static final double BPRICE = 5.50;
	public static final double	LPRICE = 9.50;
	public static final double	DPRICE = 16.50;
	public static final double	BDISCOUNT = .10;
	public static final double	LDISCOUNT = .15;
	public static final double DDISCOUNT = .12;

	public static void main(String[] Args)
	{
		// Variables
		double bfastIn, lunchIn, dinnerIn, totalBf, totalL, totalD, beforeTax, totalTax, afterTax;
		double bDiscount, lDiscount, dDiscount;
		String bfastStr;
		String lunchStr;
		String dinnerStr;
		String totalOutputStr;
		
		bfastStr = JOptionPane.showInputDialog("Enter Number of Breakfast Ordered: ");
		bfastIn = Double.parseDouble(bfastStr);
		
		lunchStr = JOptionPane.showInputDialog("Enter Number of Lunches Ordered: ");
		lunchIn = Double.parseDouble(lunchStr);
		
		dinnerStr = JOptionPane.showInputDialog("Enter Number of Dinners Ordered: ");
		dinnerIn = Double.parseDouble(dinnerStr);
		
		// If statement to check if there is a discount or not
		// Breakfast Discount Check
		if (bfastIn >= 10)
			{
				bDiscount = (BPRICE * bfastIn) * BDISCOUNT;
				totalBf = (BPRICE * bfastIn) + bDiscount;
			}
		else
			{
				totalBf = BPRICE * bfastIn;
			}
			
		// Lunch Discount Check	
		if (lunchIn >= 15)
			{
				lDiscount = (LPRICE * lunchIn) * LDISCOUNT;
				totalL = (LPRICE * lunchIn) + lDiscount;
			}
		else
			{
				totalL = LPRICE * lunchIn;
			}
			
		// Dinner Discount Check
		if (dinnerIn >= 8)
			{
				dDiscount = (DPRICE * dinnerIn) * DDISCOUNT;
				totalD = (DPRICE * dinnerIn) + dDiscount;
			}
		else
			{
				totalD = DPRICE * dinnerIn;
			}
			
		// Calculations for totals
			
		beforeTax = totalBf + totalL + totalD;
		totalTax = beforeTax * .10;
		afterTax = beforeTax + totalTax;
		
		// Output Message for All Information
		
		totalOutputStr = "Meal   " + "Quantity   " + "Cost  " + "Discount  " + "Cost After Discount\n" +
							  "Breakfast" + bfastIn + " " + BPRICE + " " + bDiscount + " " + totalBf + "\n" +
							  "Lunch" + lunchIn + " " + LPRICE + " " + lDiscount + " " + totalL + "\n" +
							  "Breakfast" + dinnerIn + " " + DPRICE + " " + dDiscount + " " + totalD + "\n";
		
		// Display the dialog for final information.
					  
		JOptionPane.showMessageDialog(null, totalOutputStr, "Totaled Information", JOptionPane.INFORMATION_MESSAGE);
		
		System.exit(0);
	}
}
Thanks in advance!
sixstorm is offline   0 Reply With Quote
Old Feb 13, 2007, 05:18 PM   #2
jeremy.king
macrumors 603
 
jeremy.king's Avatar
 
Join Date: Jul 2002
Location: Fox Lake, IL
change
Code:
double bDiscount, lDiscount, dDiscount;
to
Code:
double bDiscount=0.0;
double lDiscount=0.0;
double dDiscount=0.0;
The moral of the story is these variables are only set in the true part of your if block, and are referenced outside of your if conditions in totalOutputStr. You could also assign a value to these variables in the else block.

PS - It is convention to capitialize (w/mixed case) your class names. jscottpass4 should become JScottPass4.
jeremy.king is offline   0 Reply With Quote
Old Feb 13, 2007, 05:52 PM   #3
sixstorm
Thread Starter
macrumors regular
 
Join Date: Jan 2006
Location: Nashville, TN
Send a message via AIM to sixstorm
Thanks for the reply. It's a little weird that you have to set those to a default 0.0 value, but I guess my C++ and Java are running together. Thanks again!
sixstorm is offline   0 Reply With Quote
Old Feb 14, 2007, 10:29 AM   #4
jeremy.king
macrumors 603
 
jeremy.king's Avatar
 
Join Date: Jul 2002
Location: Fox Lake, IL
Quote:
Originally Posted by sixstorm View Post
Thanks for the reply. It's a little weird that you have to set those to a default 0.0 value, but I guess my C++ and Java are running together. Thanks again!
You always have to initialize variables within a method.

Local variables do not get default values, but class instance and static variables will get a default value assigned.
jeremy.king is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Some apps prompt to install Java 6 even when Java 7 is installed MacMan988 OS X Mavericks (10.9) 1 Apr 18, 2014 07:56 AM
Java 6 or Java 7 for OSX Mavericks for your Macbook Pro? Starfyre MacBook Pro 3 Dec 22, 2013 03:49 PM
Java 6.x security risk (?) vs Java 7.x and broken apps w/ v7.x installs? mgiamo Mac Basics and Help 1 Dec 15, 2013 03:48 PM
Chrome on OS X -- Java plugin / Java 7 vs Java 6 Issue booyahbooyah Mac Applications and Mac App Store 4 May 31, 2013 09:13 AM
Installed Java 7.07 but doesn't show up in Java preferences? -mattias- OS X 10.8 Mountain Lion 21 Aug 31, 2012 04:49 PM

Forum Jump

All times are GMT -5. The time now is 09:15 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC