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 2 of 2

Thread: problem with data access when a class call another class

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

    Unhappy problem with data access when a class call another class

    Hello everybody,
    I am new to java and I can not finish this exercise.
    I have a timer (Prog1) that calls an Employee Report (Prog2).
    For every tick of the timer (Prog1), how can I double the value of the field basic_salary (Prog2)????
    Any help/advice/hints to this would be valuable to me becuase I dont know how to fight it.
    Thanks. Michael



    PROGRAM1
    ----------------------------------------------------------------------------------------
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.Timer; 
    // to resolve conflict with java.util.Timer
     
     
    public class TimerTest
    { 
    public static void main(String[] args)
    { 
    ActionListener listener = new TimePrinter();
     
    // construct a timer that calls the listener
    // once every 10 seconds
    Timer t = new Timer(1000, listener);
    t.start();
    JOptionPane.showMessageDialog(null, "Quit program?");
    System.exit(0);
    }
    }
     
    class TimePrinter implements ActionListener
    { 
    public void actionPerformed(ActionEvent event)
    { 
    Date now = new Date();
    System.out.println("Title current: " + now);
    Toolkit.getDefaultToolkit().beep();
    EmployeeProject3.main(null);
    }
    }
    -----------------------------------------------------------------------------------------


    PROGRAM2
    ----------------------------------------------------------------------------------------
    import static java.lang.Math.*;
    public class EmployeeProject3
    {
    public static void main(String[] args)
    { 
    // construct a Manager object
    Executive A = new Executive("Executive", 300, 7, 3);
    Manager B = new Manager("Manager", 400, 7, 0);
    Employee[] staff = new Employee[7];
     
    // fill the staff array with Manager and Employee objects
    staff[0] = A;
    staff[1] = B;
    staff[2] = new Employee("Employee", 500, 7, 0);
    staff[3] = new Employee("Employee", 500, 7, 0);
    staff[4] = new Employee("Employee", 500, 7, 0);
    staff[5] = new Employee("Employee", 500, 7, 0);
    staff[6] = new Employee("Employee", 500, 7, 0); 
     
    // print out information about all Employee objects 
     
    for (Employee e : staff)
    { 
    System.out.println("Type: " + e.getName() + "\t Basic Salary|" + e.getBasic_Salary()
    + "\t Height|" + e.getHeight() + "\t Radius|" + e.getRadius()
    + "\t Bonus|" + e.getBonus()+"\t Total|" + e.getTotal()); 
    } 
    }
    }
     
    class Employee
    { 
    private String name;
    private double basic_salary, total, radius, height;
     
    public Employee(String n, double s, double r, double h)
    {
    name = n;
    basic_salary= s;
    radius = r;
    height = h;
    }
     
    public String getName()
    {
    return name;
    }
     
    public double getBasic_Salary()
    {
    return basic_salary;
    }
     
    public double getRadius()
    {
    return radius;
    }
     
    public double getHeight()
    {
    return height;
    }
     
    public double getBonus()
    {
    double bonus=2*PI*radius;
    return Math.floor(bonus);
    }
     
    public double getTotal()
    {
    total=basic_salary+this.getBonus();
    return total;
    }
    }
     
    class Manager extends Employee
    { 
    public Manager(String n, double s, double r, double h)
    {
    super(n, s, r, h);
    } 
     
    public double getBonus()
    {
    double bonus=PI*pow (getRadius(),2);
    return Math.floor(bonus);
    } 
    }
     
    class Executive extends Manager
    { 
    public Executive (String n, double s, double r, double h)
    { 
    super(n, s, r, h);
    } 
     
    public double getBonus()
    {
    double bonus=PI*pow (getRadius(),2)*getHeight();
    return Math.floor(bonus);
    } 
    }
    -----------------------------------------------------------------------------------------
    Last edited by helloworld922; April 4th, 2010 at 10:32 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: problem with data access when a class call another class

    basic_salary is an instance variable, and therefor you need an instance to double it. If you are looking to double all the instances of Employee in EmployeeProject3, then you need a reference to that for you actionPerformed to access it. You can do so by creating the instance as part of you TimerTest class or TimerPrinter class, and access it from there.

Similar Threads

  1. Accessing a method of one class in another class
    By Sai in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 23rd, 2010, 04:06 PM
  2. Pulling in data from access
    By tdc5013 in forum JDBC & Databases
    Replies: 2
    Last Post: March 15th, 2010, 04:40 PM
  3. how to load a class and type cast to that class?
    By chinni in forum Object Oriented Programming
    Replies: 2
    Last Post: November 9th, 2009, 10:18 AM
  4. Main Class Not Found Problem
    By shadow in forum Java Theory & Questions
    Replies: 3
    Last Post: September 29th, 2009, 09:42 AM
  5. final class, final <variable> or <data member>
    By chronoz13 in forum Object Oriented Programming
    Replies: 9
    Last Post: September 20th, 2009, 08:19 AM