Results 1 to 4 of 4
Thread: class problem
- 11-23-2010, 02:05 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 24
- Rep Power
- 0
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
Application to use BatterJava 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); } }
Java Code:public class UsesBatter { public static void main(String[]args) { Batter batter1; batter1 = new Batter(); batter1.calcAvg(); batter1.display(); } }
-
JOptionPane must be imported,
If you're going to be using it without using its fully qualified name.Java Code:import javax.swing.JOptionPane;
- 11-23-2010, 03:39 AM #3
Member
- Join Date
- Oct 2010
- Posts
- 24
- Rep Power
- 0
omg, duh! thank you as always! anyhow, i'm wondering why my calcAvg method doesn't calc the avg?
application: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; } }
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(); } }
-
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
-
Problem to access data when a class calls another class
By ea09530 in forum New To JavaReplies: 0Last Post: 04-04-2010, 10:06 AM -
Need help with class problem!!
By ricky in forum New To JavaReplies: 5Last Post: 11-23-2009, 03:54 PM -
problem in accessing array values of one class in to jframe class
By cenafu in forum AWT / SwingReplies: 8Last Post: 03-21-2009, 09:34 AM -
class problem
By McChill in forum New To JavaReplies: 1Last Post: 02-24-2009, 03:26 AM -
problem calling function from class to class
By alin_ms in forum New To JavaReplies: 3Last Post: 12-19-2008, 07:35 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks