Results 1 to 5 of 5
  1. #1
    ea09530 is offline Member
    Join Date
    Apr 2010
    Posts
    5
    Rep Power
    0

    Default Beginner in Java - need some help

    :confused:hello I have prepare to codes:
    1) a timer that returns every sec the current date
    2) A simple program that returns some infos
    What I want to do (and I cant) is:
    every sec that the date is returned from prog 1, to return also the infos of prog 2

    Prog 1
    ----------------------------
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.Timer;

    public class TimerTest
    {
    public static void main(String[] args)
    {
    ActionListener listener = new TimePrinter();
    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(now);
    Toolkit.getDefaultToolkit().beep();

    }
    }

    ______________________________
    ______________________________


    prog 2
    --------------------------------------------
    import static java.lang.Math.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;

    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, 6, 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
    {
    public void actionPerformed(ActionEvent event)
    {
    Date now = new Date();
    System.out.println("At the tone, the time is " + now);
    Toolkit.getDefaultToolkit().beep();
    }

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

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default

    Nothing is returned: both your actionPerformed( ... ) method as well as your main( ... ) method in your EmployeeProject3 class are void. Why not simpy add the following to your actionPerformed( ... ) method?

    Java Code:
    EmployeeProject3.main(null);
    that way not just the current time is printed with a beep but the other class is run as well.

    kind regards,

    Jos

  3. #3
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default

    hi,
    your 1st code is corrected in one postion:
    JOptionPane.showMessageDialog(null, "Quit program?");
    changed into :
    JOptionPane.showMessageDialog(this, "Quit program?");

  4. #4
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default

    Sorry,I misunderstood you.
    The following is my reply after understanding your meaning?
    I hope it can help you.
    package GUI;
    import static java.lang.Math.PI;
    import static java.lang.Math.pow;

    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Date;

    import javax.swing.JOptionPane;
    import javax.swing.Timer;

    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, 6, 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);
    Timer timer = new Timer(1000,new Employee(staff));
    timer.start();
    JOptionPane.showMessageDialog(null, "确认退出吗?");
    System.exit(0);
    }
    }

    class Employee implements ActionListener{
    Employee [] staff ;
    int cx = 0;
    public Employee(Employee[] _staff){
    this.staff = _staff;
    }
    public void actionPerformed(ActionEvent event) {
    if (cx==staff.length-1) return ;
    Date now = new Date();
    System.out.println("At the tone, the time is " + now);
    Toolkit.getDefaultToolkit().beep();
    // print out information about all Employee objects

    Employee e = staff[cx++];
    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());
    }

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

  5. #5
    ea09530 is offline Member
    Join Date
    Apr 2010
    Posts
    5
    Rep Power
    0

    Default

    Dear friend - Much Obliged for your help !! I really appreciated it !
    It really worked ! - Then, I continued with rest tasks and now I have just finisehd.
    Now my stress has been kicked away.
    Best regards from Athens/Greece - Michael

Similar Threads

  1. java beginner
    By devstarter in forum New To Java
    Replies: 4
    Last Post: 03-03-2010, 08:39 AM
  2. Beginner Java Tutorial
    By Goddard in forum New To Java
    Replies: 2
    Last Post: 11-29-2009, 09:59 PM
  3. Beginner Java question
    By DanK in forum New To Java
    Replies: 3
    Last Post: 04-27-2009, 04:29 AM
  4. Java Beginner needs help!!
    By Polyy in forum New To Java
    Replies: 4
    Last Post: 11-23-2008, 02:11 AM
  5. beginner to Java
    By notwist in forum New To Java
    Replies: 15
    Last Post: 04-18-2008, 09:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •