Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: problem using the "extend"

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile 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
    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?
    Last edited by copeg; October 20th, 2010 at 08:51 AM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: problem using the "extend"

    Quote Originally Posted by Eildotcom View Post
    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
    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.

  3. #3
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,103
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default 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

Similar Threads

  1. Java says:"Hello World". I say:"It works!"
    By Davidovic in forum Member Introductions
    Replies: 4
    Last Post: June 29th, 2010, 07:13 AM
  2. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 07:35 AM
  3. "While" Problem
    By Killasnipe004 in forum Loops & Control Statements
    Replies: 9
    Last Post: August 13th, 2009, 09:01 AM
  4. [SOLVED] Is "Public void closeFile()" a problem in the program for AS-Level computing project
    By muffin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 5th, 2009, 09:12 PM
  5. [SOLVED] "GridLayout" problem in Java program
    By antitru5t in forum AWT / Java Swing
    Replies: 3
    Last Post: April 16th, 2009, 10:26 AM

Tags for this Thread