Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 13th, 2011, 04:35 PM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy Lesson 5 exercises.

Maybe I'm missing something, but I'm not sure I'm following the steps in the Try It section.

Step 1 says to copy TestTax from Lesson4 and Tax from lesson 3. This means I'm using a TestTax class that tries to employ a 3 argument constructor to create a new Tax instance, while the Tax class has no declared constructor. Doing these copies causes Eclipse to give me a number of compile errors in TestTax.

Step 2 says to remove three lines, presumably from TestTax because Tax doesn't have any lines declaring any values, but the step doesn't specify.

Step 3 says to "add the following code fragment" but doesn't say in which class or where in those classes. I get the concept of an If statement (I've used other languages) but I'm not sure where to put this.

I haven't proceeded further because I'm very unconfident that I'm doing any of this right. Can I get some clarification?
Reply With Quote
  #2 (permalink)  
Old April 14th, 2011, 05:00 PM
Registered User
 
Join Date: Apr 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here is an examples of a source code.
I agree that this book is a bit too " concentrated " .But additional reading (see p.28 http://download.oracle.com/javase/tu...ava/index.html
)
will do the job ,at least for me .

public class TestTax {


public static void main(String[] args) {
if (args.length !=3){
System.out.println("Sample usage of the program: " +
" java TestTax 50000 NJ 2");
System.exit(0);
}

double grossIncome = Double.parseDouble(args[0]);
String state = args[1];
int dependents = Integer.parseInt(args[2]) ;






Tax t = new Tax();

double yourTax = t.calcTax(grossIncome, state, dependents); //calculating tax


System.out.println("Your tax is " + yourTax);



}

}

Last edited by sergeySE; April 14th, 2011 at 05:14 PM..
Reply With Quote
  #3 (permalink)  
Old April 14th, 2011, 05:07 PM
Registered User
 
Join Date: Apr 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The rest of the code .
And remember to use command line to enter arguments (see lesson 1 in the book ) :-)

public class Tax
{

double gi;
String st;
int dp;


public double calcTax(double a,String b,int c ) {

gi=a;
st=b;
dp=c;

double stateTax = 0;

if (gi < 30000) {

stateTax=gi*0.05;

}
else{

stateTax= gi*0.06;
}
return stateTax;

}

}
Reply With Quote
  #4 (permalink)  
Old April 20th, 2011, 10:01 AM
Registered User
 
Join Date: Apr 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default strange errors here

I put it right before the statement Tax t= new Tax
this seems to be ok - BUT! the line
String state = args[l];
produces an error and I don't have an idea why
EM 'cannot be resolved to a variable


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

if (args.length != 3) {
System.out.println("das war noch nichts");
System.exit(0);
}
double grossIncome = Double.parseDouble(args[0]);
String state = args[l];
int dependents = Integer.parseInt(args[2]);


Tax t = new Tax(grossIncome, state, dependents);

double yourTax = t.calcTax();
//calculating tax

double youreTax = t.calceTax();
//calculating tax

// Printing the result
System.out.println("Your tax is " +
yourTax + "$ = " + youreTax + "€") ;


Tax t2 = new Tax(65000, 4);

double hisTax = t2.calcTax();
double hiseTax = t2.calceTax();
// Printing the result
System.out.println("Your tax is " +
hisTax + "$ = " + hiseTax + "€") ;

}
}
Reply With Quote
  #5 (permalink)  
Old April 20th, 2011, 06:07 PM
Registered User
 
Join Date: Apr 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ruediger isn't it just an incorrect argument " String state = args[l]; " supposed to be a figure : "1"
Reply With Quote
  #6 (permalink)  
Old April 21st, 2011, 03:47 AM
Registered User
 
Join Date: Apr 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default That's right

That was right, -
but I copied these 3 lines from the (Online) book - and as this charakter is quite similar to a 1.... I didn't see it
Thank you
Reply With Quote
  #7 (permalink)  
Old March 20th, 2013, 10:41 AM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Compiles, but errors out when trying to run it

My code compiles when I enter javac TestTax.java on the command line,
but when I try to run it it gives me the following error:
//--------------------------------------------------------------------

C:\Users\uriel.gutierrez\workspace\Lesson5\src\les son5>java TestTax 100 AR 2
Exception in thread "main" java.lang.NoClassDefFoundError: TestTax (wrong name:
lesson5/TestTax)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknow n Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unkno wn Source)

C:\Users\uriel.gutierrez\workspace\Lesson5\src\les son5>
//------------------------------------------------------------------------------------

package lesson5;



public class TestTax {
public static void main(String[] args) {
if (args.length !=3){
System.out.println("Sample usage of the program: " + " java TestTax 5000 NJ 2");
System.exit(0);
}

double grossIncome = Double.parseDouble(args[0]);
String state = args[1];
int dependents = Integer.parseInt(args[2]);
class Tax {
double gi;
String st;
int depen;


public double calcTax(double gi, String st, int depen){
this.gi = gi;
this.st = st;
this.depen = depen;

double stateTax=0;
if (gi < 30000){
stateTax=gi*0.05;
}
else {
stateTax= gi*0.07;
}
return stateTax;
}


}

Tax t = new Tax();


double yourTax = t.calcTax(grossIncome, state, dependents);

System.out.println("Your tax is " + yourTax);

}

}


Any suggestions?
Reply With Quote
  #8 (permalink)  
Old March 20th, 2013, 12:07 PM
Authorized User
Points: 30, Level: 1
Points: 30, Level: 1 Points: 30, Level: 1 Points: 30, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2011
Location: uk
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default RE: Compiles, but errors out when trying to run it

Alonsodev

From the command line navigate to your eclipse workspace I'm running on XP and my location is C:\Documents and Settings\user\workspace\lesson5\bin

from here run java TestTax 50000 NJ 2

the 50000 NJ 2 are the arguments that are pass to the Tax class and returns the calculated tax

below is my class TestTax and class Tax Code

Code:
class TestTax {
	public static void main(String[] args) {
		
		if (args.length != 3){
			System.out.println("Sample usage of the program: java TestTax 50000 NJ 2");
			System.exit(0);
		}
		
		double grossIncome = Double.parseDouble(args[0]); //local variables
		String state = args[1];
		int dependents = Integer.parseInt(args[2]);
		
		
		Tax t = new Tax(grossIncome, state, dependents);
	
			double yourTax = t.calcTax();//calculating tax

			
			
			
			//Printing Result
			System.out.println("Your tax is $" + yourTax);
			
	}
}
Code:
class Tax {
	double grossIncome;//class member variables
	String state;
	int dependents;
	static int customerCounter;
	static double convertToEuros = 1.25;
	static 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 the tax data for customer #" + customerCounter);
	}

	public double calcTax() {
		
		return (grossIncome * 0.33 - dependents * 100);
	}
	public double convertTaxEuros(){
		dollars = grossIncome *0.33 - dependents * 100;
		
		return (dollars / convertToEuros);
	}
}
Reply With Quote
  #9 (permalink)  
Old March 29th, 2013, 11:38 PM
Registered User
Points: 6, Level: 1
Points: 6, Level: 1 Points: 6, Level: 1 Points: 6, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Alonsodev,

I am getting the exact same error. I have read and tried exactly what Wolverine says to do and it does not work for me. The program works just fine in Eclispe, but I cannot get the command line to work.

If you find anything out, I would appreciate it. I will do the same.
Reply With Quote
  #10 (permalink)  
Old March 29th, 2013, 11:56 PM
Registered User
Points: 6, Level: 1
Points: 6, Level: 1 Points: 6, Level: 1 Points: 6, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Alonsodev,

I got mine to work by removing the package line. I expanded the Chapter 5 program to do a similar program, but I have a java file called Circuit.java and then TestLesson5.java. After I removed the package line from each file, I then did the following:

javac Circuit.java TestLesson5.java

That created a Circuit.class file and a TestLesson5.class file.

I then entered java TestLesson5

The program then execute as it did in Eclipse.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lesson 19B-Displaying Data in MVC Cybermaniac BOOK: ASP.NET 4 24-Hour Trainer 3 January 31st, 2012 08:16 AM
Lesson 3 question soneal777 BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 1 August 30th, 2010 06:17 PM
Exercises kulfi BOOK: Beginning Microsoft Visual Basic 2010 0 May 11th, 2010 06:35 AM
Exercises Joanne Bell BOOK: Beginning ASP.NET 1.0 1 August 2nd, 2006 09:15 AM
Lesson on Event Calendar - Chapter 14 Mike Smith BOOK: Professional C#, 2nd and 3rd Editions 6 April 6th, 2004 03:03 PM



All times are GMT -4. The time now is 11:53 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.