First, simple, two-classed tax program
Hi all,
I'm entering into a programming course next September and am trying to get the essentials of Java before hand. I attempted to create my first program but have learned that it's much easier when you can speak to someone who knows what they're doing. Anyway, here is my code:
Code Java:
public class Taxone {
//naming variables
double income;
String state;
int dependants;
}
Code Java:
public class Taxtwo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO creating instance of a variable
Taxone t = Taxone;
t.income = 1000000;
t.state = NY;
t.dependants = 4;
{
public double t.calctax();
stateTax=0;
if (income > 999999){
stateTax=income*0.75;
{
else stateTax=income*0.0001;
}
}
return stateTax;
double yourTax = t.calctax();
System.out.println("Your tax is" yourTax);
My Error
I get an error that says I cannot invoke primitive type double on the t.calctax method in the last few lines of class 2, and consequently the yourTax variable is invalid on the last line.
Help mucho appreciado.
Re: First, simple, two-classed tax program
What are you trying to accomplish? And please use code tags
Re: First, simple, two-classed tax program
Quote:
Originally Posted by
vanDarg
What are you trying to accomplish? And please use code tags
What I thought I would accomplish was to create a few variables in the first class, and then define them in the second class and create a simple method in the second class to give me a final value.
Class One
Code Java:
public class Taxone {
//naming variables
double income;
String state;
int dependants;
}
Class Two
Code Java:
public class Taxtwo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO creating instance of a variable
Taxone t = Taxone;
t.income = 1000000;
t.state = NY;
t.dependants = 4;
{
public double t.calctax();
stateTax=0;
if (income > 999999){
stateTax=income*0.75;
{
else stateTax=income*0.0001;
}
}
return stateTax;
[COLOR="red"]double yourTax = t.calctax();[/COLOR]
System.out.println("Your tax is" [COLOR="red"]yourTax[/COLOR]);
The errors are where I attempted to make the code red, the first says I can't use the double primitive there, the second says the yourTax variable is invalid.
Re: First, simple, two-classed tax program
First, if you are trying to manipulate variables of the first class with methods, you should define those methods within the first class:
Code JAVA:
public class Taxone {
//naming variables
double income;
String state;
int dependants;
// Define methdos here
}
Second, I'm not sure where your stateTax variable is coming from. I can't find where it is declared. Please look at my signature and click the link for the java tutorials. I would go through them all, but pay special attention to primitive data types as well as defining classes.
Re: First, simple, two-classed tax program
Quote:
Originally Posted by
vanDarg
First, if you are trying to manipulate variables of the first class with methods, you should define those methods within the first class:
Code JAVA:
public class Taxone {
//naming variables
double income;
String state;
int dependants;
// Define methdos here
}
Second, I'm not sure where your stateTax variable is coming from. I can't find where it is declared. Please look at my signature and click the link for the java tutorials. I would go through them all, but pay special attention to primitive data types as well as defining classes.
Thanks very much, will check your tips out a bit later today.
Re: First, simple, two-classed tax program
If you get errors, please post the full error message text and stack trace (if present). Java errors usually tell you exactly what is wrong and where.
To create a new instance of Taxone, you must use the 'new' keyword:
Quote:
Taxone t = new Taxone();
I can see you appear to be trying to declare a public double in the middle of your 'main' method, but not only can you not do that (only member variables can have access specifiers like 'public', not local variables) it doesn't make sense, because you haven't named it:
Quote:
...
public double ?? t.calctax(); // ?? where's the variable name?
...
You're also using local variables 'stateTax' & 'income' that you haven't declared, and calling a method 'calctax()' that doesn't exist, you're trying to return a value from 'main', which is a void method, plus you have a curly brace { that has no corresponding closing curly brace }, state value or variable NY is undefined (it could be "NY"), etc...