Wrox Programmer Forums
|
BOOK: Java Programming 24-Hour Trainer by Yakov Fain
This is the forum to discuss the Wrox book Java Programming 24-Hour Trainer by Yakov Fain; ISBN: 978-0-470-88964-0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Java Programming 24-Hour Trainer by Yakov Fain section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 4th, 2011, 11:16 PM
Registered User
 
Join Date: Sep 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Lesson 4

I am new to Java and bought Java programming 24 hr trainer to hopefully get a jump start on a class I am taking. But I am having problems with Lesson 4.
I have the program calculating tax, but I am struggling with converting the tax to euros. I am unsure as to how to add another static function. Can someone point me in the right direction?

This is my code:
Code:
class Tax {	
	 double grossIncome;     //class member variables
	 String state;
	 int dependents;
	 static int customerCounter;
	 static double euros;
	 
	 
	// First Constructor
	Tax (double gi, String st, int depen, double euros){
		this.grossIncome = gi;    //member variable initialization
		this.state = st;
		this.dependents=depen;
		customerCounter++;   // increment the counter by one
		
		//results
		System.out.println("Prepare the tax data for customer #" + customerCounter);
	}
	//Second Constructor
	Tax (double gi, int depen, double euros){
		this(gi, "NY", depen, euros);
	}
	public double calcTax(){
		return (grossIncome*0.033 - dependents*100);
		
	}
	public double convert(){
		return (calcTax()*1.25);
	}
}
Code:
class TestTax {
	 public static void main (String[] args){
		 double grossIncome;     //local variables
		 double euros;
		 String state;
		 int dependents;
		 
		 
		 grossIncome = 75000;
		 dependents = 2;
		 euros = 1.25;
		 state = "NJ";
		 
		 Tax   t = new Tax(grossIncome, state, dependents, euros);
		 
		 double yourTax = t.calcTax(); //calculating tax
		// double convTax = t.convert(); // calculating euros
		 
		 //print results
		 System.out.println("Your tax is $" + yourTax);
		 System.out.println("Your tax in euros is $" + yourTax);
		 
		 
	 }

}
Also, I can't seem to open the code that accompanies this text. I can download the files but they arror out when I try to open them in Eclipse. I don't know what I am doing wrong. Can you help with that also?

Thank you for any assistance.

MTheys
 
Old September 5th, 2011, 01:56 PM
Registered User
 
Join Date: Aug 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Add something like this to your first class (Tax) and call it using Tax.convertToEuros(...).

Code:
	 static double  convertToEuros(double dollars) {
		 double euros = dollars * 1.25;
		 return euros;
	 }
You should be able to find solutions at http://code.google.com/p/practicaljava/.
 
Old September 15th, 2011, 02:40 AM
Authorized User
 
Join Date: Sep 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Lesson 4

Mtheys

I've just started to learn how to program in Java, what have I done is assigned 1.25 to the variable convertToEuros as this is a static value.

Also to convert from dollars to euros you need to divide dollars by euros.

Below is my code I think I'm going in the right direction.

Code:
class Tax {
	double grossIncome;//class variables
	String state;
	int dependents;
	static int customerCounter;
	static double convertToEuros = 1.25;
	double dollars;
	//Constructor
	Tax (double gi, String st, int depen){
		grossIncome = gi;// member variable initialisation
		state = st;
		dependents = depen;
		customerCounter ++; //increment the counter by one
		System.out.println("Preparing tax data for customer #" + customerCounter);
	}
		public double calcTac(){
			return (grossIncome * 0.33 - dependents * 100);
		}
		public double convertTaxEuros(){
			dollars = grossIncome *0.33 - dependents * 100;
			
			return (dollars / convertToEuros);
		}
Code:
class TestTax {

	public static void main(String[] args) {
		double grossIncome; //local variables
		String state;
		int dependents;
		
		grossIncome = 50000;
		dependents = 2;
		state = "NJ";
		
		Tax t = new Tax(grossIncome, state, dependents);
		
		double yourTax = t.calcTac(); //calculating tax
		double yourTaxToEuro = t.convertTaxEuros();
		
		Tax t2 = new Tax(65000, "TX", 4);
		double hisTax = t2.calcTac();
		double hisTaxToEuro = t2.convertTaxEuros();
		
		//Print Result
		
		System.out.println("Your tax is $" + yourTax);
		System.out.println("Your tax in Euros is " + yourTaxToEuro);
		System.out.println("His tax is $" + hisTax);
		System.out.println("His tax in Euros is " + hisTaxToEuro);

	}

}
Hope this helps and if I'm not going in the right direction can someone please point me there.
 
Old September 15th, 2011, 05:02 AM
Authorized User
 
Join Date: Sep 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

wasn't happy with the output

Preparing tax for customer #1
Preparing tax for customer #2
Results....

So I formatted the testTax code below
Code:
class TestTax {
	public static void main(String[] args) {
		double grossIncome; //local variables
		String state;
		int dependents;
		
		grossIncome = 50000;
		dependents = 2;
		state = "NJ";
		
		Tax t = new Tax(grossIncome, state , dependents);
	
			double yourTax = t.calcTax();//calculating tax
			double yourTaxEuros = t.convertTaxEuros();
			
			
			
			//Printing Result
			System.out.println("Your tax is " + yourTax);
			System.out.println("Your tax in Euros " + yourTaxEuros);
		
		Tax t2 = new Tax(65000, "TX", 4);
			
			double hisTax = t2.calcTax();
			double hisTaxEuros = t2.convertTaxEuros();
			
			//Printing Result
			System.out.println("His tax is " + hisTax);
			System.out.println("His tax in Euros " + hisTaxEuros);
			
		Tax t3 = new Tax(79000, "FL", 6);
		
			double t3Tax = t3.calcTax();
			double t3TaxEuros = t3.convertTaxEuros();
			
			//Printing Result
			System.out.println("Customer #" + Tax.customerCounter + " tax is $" + t3Tax);
			System.out.println("Customer #" + Tax.customerCounter + " tax in Euros " + t3TaxEuros);
		
		}
the output now flows better and is easier to read.

Preparing the tax data for customer #1
Your tax is 16300.0
Your tax in Euros 13040.0
Preparing the tax data for customer #2
His tax is 21050.0
His tax in Euros 16840.0
Preparing the tax data for customer #3
Customer #3 tax is $25470.0
Customer #3 tax in Euros 20376.0





Similar Threads
Thread Thread Starter Forum Replies Last Post
Lesson 5 exercises. thelemur BOOK: Java Programming 24-Hour Trainer by Yakov Fain 9 March 29th, 2013 11:56 PM
Lesson 4 Try it Exercise caa5042 BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 13 February 2nd, 2013 03:32 PM
Lesson 8 sergeySE BOOK: Java Programming 24-Hour Trainer by Yakov Fain 1 June 29th, 2011 09:49 AM
Lesson 6 Try It zavodney BOOK: Stephens' Visual Basic Programming 24-Hour Trainer 2 June 6th, 2011 10:07 PM
Lesson 3 question soneal777 BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 1 August 30th, 2010 06:17 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.