Results 1 to 4 of 4

Thread: class problem

  1. #1
    smray7 is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default class problem

    I don't know what's wrong!
    1 error found:
    File: E:\week 10\Batter.java [line: 22]
    Error: JOptionPane cannot be resolved

    Java Code:
    public class Batter
    {
      private int playerNumber;
      private int numAtBats;
      private int numHits;
      private double battingAverage;
      
      public Batter()
      {
        playerNumber = 0;
        numAtBats = 1;
        numHits = 0;
        battingAverage = 0;
      }
    
    public void calcAvg()
    {
      battingAverage = numHits/numAtBats;
    }
    public void display()
    {
     JOptionPane.showMessageDialog(null, "Stats for better" + playerNumber
                                     + "\n At Bats: " + numAtBats
                                     +"\tHits: " + numHits
                                     +"\tBatting Average: " + battingAverage);
    }
    }
    Application to use Batter

    Java Code:
    public class UsesBatter
    {
      public static void main(String[]args)
      {
        Batter batter1;
        batter1 = new Batter();
        batter1.calcAvg();
        batter1.display();
      }
      
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    JOptionPane must be imported,
    Java Code:
    import javax.swing.JOptionPane;
    If you're going to be using it without using its fully qualified name.

  3. #3
    smray7 is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default

    omg, duh! thank you as always! anyhow, i'm wondering why my calcAvg method doesn't calc the avg?

    Java Code:
    import javax.swing.JOptionPane;
    public class Batter // This is a class
    {
      private int playerNumber;    //delcaration of data
      private int numAtBats;
      private int numHits;
      private double battingAverage;
      
      public Batter()    //his is a constructor
      {
        playerNumber = 0;
        numAtBats = 1;
        numHits = 0;
        battingAverage = 0;
      }
    
    public void calcAvg()  //accesor method
    {
      battingAverage = numHits/numAtBats;
    }
    public void display()  //accessor method 
    {
     JOptionPane.showMessageDialog(null, "Stats for batter " + playerNumber
                                     + "\n At Bats: " + numAtBats
                                     +"\t Hits: " + numHits
                                     +"\t Batting Average: " + battingAverage);
    }
    public void setPlayerNumber(int pnum) // mutator method
    {
      playerNumber = pnum;
    }
    public void setNumAtBats(int numBats)  //mutator method
    {
      numAtBats = numBats;
    }
    public void setNumHits(int hits)  //mutator method
    {
      numHits = hits;
    }
    }
    application:

    Java Code:
    public class UsesBatter // This is an application that uses the class Batter
    {
      public static void main(String[]args)
      {
        Batter batter1;          //object of batter class
        batter1 = new Batter();  // calling constructor
        batter1.calcAvg();       //calling method using object name
        batter1.display();
        
        Batter batter2;         // 2nd object of batter class
        batter2 = new Batter();
        batter2.setPlayerNumber(22);
        batter2.setNumAtBats(12);
        batter2.setNumHits(10);
        batter2.calcAvg();
        batter2.display();
      }
      
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    It does calculate average, but does so with int division, which means if you divide an int by an int, you'll get the integer result from the division and the remainder gets tossed out. In other words:

    4/5 = 0
    6/5 = 1
    12/5 = 2

    To do double division, cast either the numerator or the denominator to double:

    Java Code:
    battingAverage = (double) numHits/numAtBats;

Similar Threads

  1. Replies: 0
    Last Post: 04-04-2010, 10:06 AM
  2. Need help with class problem!!
    By ricky in forum New To Java
    Replies: 5
    Last Post: 11-23-2009, 03:54 PM
  3. Replies: 8
    Last Post: 03-21-2009, 09:34 AM
  4. class problem
    By McChill in forum New To Java
    Replies: 1
    Last Post: 02-24-2009, 03:26 AM
  5. problem calling function from class to class
    By alin_ms in forum New To Java
    Replies: 3
    Last Post: 12-19-2008, 07:35 PM

Posting Permissions

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