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 March 5th, 2012, 04:25 AM
Registered User
 
Join Date: Mar 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Lesson3

In lesson 3:

public class NjTax extends Tax {
public double calcTax() {
double stateTax=0;
if (grossIncome < 30000) {
stateTax= grossIncome*0.05;
}
else{
stateTax= grossIncome*0.06;
}

return stateTax;
}

}

I don't understand why is there a zero in line 3 "double stateTax=0;"
I removed zero and it works just fine
 
Old March 5th, 2012, 04:58 AM
Registered User
 
Join Date: Feb 2012
Posts: 5
Thanks: 0
Thanked 1 Time in 1 Post
Default Reply

Code:
public class NjTax extends Tax {
public double calcTax() {
double stateTax=0;
if (grossIncome < 30000) {
stateTax= grossIncome*0.05;
}
else{
stateTax= grossIncome*0.06;
}

return stateTax;
}
}
in above code the line
Code:
double stateTax=0;
is inside the calcTax() method, so stateTax is a local variable and local variables must initialize before its use.
In above code stateTax is actual using in line
Code:
return stateTax;
and it is already initalized inside the if and else block;
If you use the following code
Code:
public class NjTax extends Tax {
public double calcTax() {
double stateTax;
if (grossIncome < 30000) {
stateTax= grossIncome*0.05;
}
else{

//stateTax= grossIncome*0.06;  

}

return stateTax;
}
}
here i removed 0 from double stateTax;
and commented line
stateTax=grossIncome*0.06;
so it'll generate an error message
because here stateTax is only initialize inside if block. But if condition becomes false(i.e. value of grossIncome is greater then 30000) than it'll not initialize because I commened that line and we cannot use any local variable without initialization.
 
Old March 13th, 2012, 01:30 PM
Registered User
 
Join Date: Mar 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am currently doing Lesson 3. I have done up to step 5, so have done both the Tax and TestTax classes. When I run the TestTax as a java application, I get an error that says "Selection does not contain a main type". As you can see from the code below, it does. Am I missing something??

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

NJTax t = new NJTax(); // creating an instance

t.grossIncome= 50000; // assigning the values
t.dependents= 2;
t.state= "NJ";

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

// Printing the result
System.out.println("Your tax is " + yourTax);
}
}
 
Old March 14th, 2012, 01:02 AM
Registered User
 
Join Date: Feb 2012
Posts: 5
Thanks: 0
Thanked 1 Time in 1 Post
Default main() error

This type of error generates only when there is no main method in our application.
Just check your code and checkout the spelling of main()
It must look like
Code:
public static void main(String[] arg)
Or it may be possible that you haven't imported it correctly
the following steps are to import project in eclipse

click
File->Import
select General Option from tree list
click existing projects into workspace->next
select
"select root directory" radio button and click "Browse"
now select the folder where your project is exist
suppose I have copied that lesson3 folder inside f:\p2ptest folder
then I have selected it from f:\p2ptest\Lesson3 and then click "ok"
after that click "Finish"
Now you can run your project
 
Old March 14th, 2012, 01:22 AM
Authorized User
 
Join Date: Feb 2012
Posts: 30
Thanks: 5
Thanked 4 Times in 4 Posts
Default

It may be that the project that you have selected in the Package Explorer window is not the one that contains your TestTax class. Please note that, if you have multiple projects open, what you see in the editor window may be different from your currently selected project.









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