problem using the "extend"
My professor gave me a program using "extend" my first program getting the grade
of a student, works properly. But this one, When I compile it, there;'s no error but when i execute it, after the computation of gross my program will start all over again.. I mean, after the gross computation instead of the computation of the BONUS,DEDUCTIOn and total net pay will be the next. The program will start all over the Name, position, rate, hours then gross.. then name again, position, rate, hrs, gross.. What is the problem with my code.?
Here's my code
Code java:
import java.io.*;
public class rate
{
public static BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
public static int getRate() throws IOException
{
int r;
String name;
String pos;
System.out.print("Name: ");
name=a.readLine();
System.out.print("Position: ");
pos=a.readLine();
if (pos.charAt(0)=='m')
{
r=500;
}
else if (pos.charAt(0)=='s')
{
r=400;
}
else
{
r=300;
}
System.out.print("Rate: "+r);
return r;
}
}
----
import java.io.*;
public class hours extends rate
{
public static BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
public static int getHours() throws IOException
{
int noh;
System.out.print("Number of hours: ");
noh=Integer.parseInt(a.readLine());
return noh;
}
}
---
import java.io.*;
public class gross extends hours
{
public static BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
public static double getGross() throws IOException
{
double g;
int r,noh;
r=getRate();
noh=getHours();
g = r * noh;
System.out.print("Gross: "+g);
return g;
}
}
---
import java.io.*;
public class bonus extends gross
{
public static BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
public static double getBonus() throws IOException
{
double bon, g;
g=getGross();
if (g>=8000)
{
bon=1000;
}
else if (g>=5000)
{
bon=750;
}
else if(g>=3000)
{
bon = 500;
}
else
{
bon=0;
}
System.out.print("Bonus: "+bon);
bon=Double.parseDouble(a.readLine());
return bon;
}
}
---
import java.io.*;
public class tax extends bonus
{
public static BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
public static double getTax() throws IOException
{
double tax,g,t;
g=getGross();
if (g>=7000)
{
t=0.15;
}
else if (g>=4000)
{
t=0.10;
}
else if(g>=2000)
{
t = 0.05;
}
else
{
t=0;
}
System.out.print("DEDUCTION: ");
System.out.print("Tax: " +t);
t=Double.parseDouble(a.readLine());
return t;
}
}
---
import java.io.*;
public class sss extends tax
{
public static BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
public static double getSss() throws IOException
{
double s,g ;
g=getGross();
s = g *0.10;
System.out.print("SSS: "+s);
s=Double.parseDouble(a.readLine());
int med=100;
System.out.print("Medicare: "+med);
med=Integer.parseInt(a.readLine());
return s;
}
}
----
import java.io.*;
public class totaldec extends sss
{
public static BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
public static double getTotaldec() throws IOException
{
double tot,s,t;
int med=100;
s=getSss();
t=getTax();
tot = t+s+med;
System.out.print("Total Deduction: "+tot);
tot=Double.parseDouble(a.readLine());
return tot;
}
}
---
import java.io.*;
public class Net extends totaldec
{
public static BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
public static void main (String args[]) throws IOException
{
double g, bon,tot,net;
g=getGross();
bon=getBonus();
tot=getTotaldec();
net = getNet(g,bon,tot);
System.out.print("NET INCOME: " +net);
}
public static double getNet(double g,double bon,double tot) throws IOException
{
double np;
np = g+bon+tot;
return np;
}
}
---
that's it...
Kindly tell me what's the problem?
Re: problem using the "extend"
Quote:
Originally Posted by
Eildotcom
My professor gave me a program using "extend" my first program getting the grade
of a student, works properly. But this one, When I compile it, there;'s no error but when i execute it, after the computation of gross my program will start all over again.. I mean, after the gross computation instead of the computation of the BONUS,DEDUCTIOn and total net pay will be the next. The program will start all over the Name, position, rate, hours then gross.. then name again, position, rate, hrs, gross.. What is the problem with my code.?
Here's my code
Code java:
import java.io.*;
public class rate
{
public static BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
public static int getRate() throws IOException
{
int r;
String name;
String pos;
System.out.print("Name: ");
name=a.readLine();
System.out.print("Position: ");
pos=a.readLine();
if (pos.charAt(0)=='m')
{
r=500;
}
else if (pos.charAt(0)=='s')
{
r=400;
}
else
{
r=300;
}
System.out.print("Rate: "+r);
return r;
}
}
----
import java.io.*;
public class hours extends rate
{
public static BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
public static int getHours() throws IOException
{
int noh;
System.out.print("Number of hours: ");
noh=Integer.parseInt(a.readLine());
return noh;
}
}
---
import java.io.*;
public class gross extends hours
{
public static BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
public static double getGross() throws IOException
{
double g;
int r,noh;
r=getRate();
noh=getHours();
g = r * noh;
System.out.print("Gross: "+g);
return g;
}
}
---
import java.io.*;
public class bonus extends gross
{
public static BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
public static double getBonus() throws IOException
{
double bon, g;
g=getGross();
if (g>=8000)
{
bon=1000;
}
else if (g>=5000)
{
bon=750;
}
else if(g>=3000)
{
bon = 500;
}
else
{
bon=0;
}
System.out.print("Bonus: "+bon);
bon=Double.parseDouble(a.readLine());
return bon;
}
}
---
import java.io.*;
public class tax extends bonus
{
public static BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
public static double getTax() throws IOException
{
double tax,g,t;
g=getGross();
if (g>=7000)
{
t=0.15;
}
else if (g>=4000)
{
t=0.10;
}
else if(g>=2000)
{
t = 0.05;
}
else
{
t=0;
}
System.out.print("DEDUCTION: ");
System.out.print("Tax: " +t);
t=Double.parseDouble(a.readLine());
return t;
}
}
---
import java.io.*;
public class sss extends tax
{
public static BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
public static double getSss() throws IOException
{
double s,g ;
g=getGross();
s = g *0.10;
System.out.print("SSS: "+s);
s=Double.parseDouble(a.readLine());
int med=100;
System.out.print("Medicare: "+med);
med=Integer.parseInt(a.readLine());
return s;
}
}
----
import java.io.*;
public class totaldec extends sss
{
public static BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
public static double getTotaldec() throws IOException
{
double tot,s,t;
int med=100;
s=getSss();
t=getTax();
tot = t+s+med;
System.out.print("Total Deduction: "+tot);
tot=Double.parseDouble(a.readLine());
return tot;
}
}
---
import java.io.*;
public class Net extends totaldec
{
public static BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
public static void main (String args[]) throws IOException
{
double g, bon,tot,net;
g=getGross();
bon=getBonus();
tot=getTotaldec();
net = getNet(g,bon,tot);
System.out.print("NET INCOME: " +net);
}
public static double getNet(double g,double bon,double tot) throws IOException
{
double np;
np = g+bon+tot;
return np;
}
}
---
that's it...
Kindly tell me what's the problem?
Well, it may be confused about variable "a" since you have one both in the superclass and in the subclass, and it might not know which you're referring to, especially since both are public.
Re: problem using the "extend"
Your program calls getGross, then getBonus...in getBonus, getGross is called again.
As a side note I'm not sure I understand how this code demonstrates inheritance, see What Is Inheritance? (The Java™ Tutorials > Learning the Java Language > Object-Oriented Programming Concepts) for a better explanation