p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Java Programming 24-Hour Trainer by Yakov Fain (http://p2p.wrox.com/forumdisplay.php?f=624)
-   -   Lesson 5 exercises. (http://p2p.wrox.com/showthread.php?t=83297)

thelemur April 13th, 2011 04:35 PM

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?

sergeySE April 14th, 2011 05:00 PM

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);



}

}

sergeySE April 14th, 2011 05:07 PM

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;

}

}

Ruediger April 20th, 2011 10:01 AM

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 + "€") ;

}
}

sergeySE April 20th, 2011 06:07 PM

Ruediger isn't it just an incorrect argument " String state = args[l]; " supposed to be a figure : "1"

Ruediger April 21st, 2011 03:47 AM

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

Alonsodev March 20th, 2013 10:41 AM

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?

Wolverine March 20th, 2013 12:07 PM

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);
        }
}


oma_roland March 29th, 2013 11:38 PM

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.

oma_roland March 29th, 2013 11:56 PM

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.


All times are GMT -4. The time now is 04:28 AM.

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