Results 1 to 7 of 7
- 10-11-2010, 10:21 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 9
- Rep Power
- 0
Keyboard Input In A Seperate Class?
Just started learning Java a week or so ago. I have made a little Java program that calculates the amount of reviews, and the average review rating that something has. For instance, on the app store, you would enter in 1 star, 2 star, 3 star, 4 star, and 5 star reviews and the program would calculate the average review score. Anyway, maybe it would just be easier to show you. What I want to do is have the keyboard input section in a different class. But when I try to do this, my program just returns a 0 for value. I know it has something to do with the KeyboardInput variables not retaining their data on the cross class traveling. However, I have no idea what to do about that. Thanks in advance!
ReviewHandler class:
Java Code:public class ReviewHandler extends KeyboardInput { public void reviews() { KeyboardInput keyboard = new KeyboardInput(); keyboard.fiveStar(); int starOne = firstStar * 1; int starTwo = secondStar * 2; int starThree = thirdStar * 3; int starFour = fourthStar * 4; int starFive = fifthStar * 5; int reviewCount = firstStar + secondStar + thirdStar + fourthStar + fifthStar; double reviewStarCount = starOne + starTwo + starThree + starFour + starFive; double reviewAverage = reviewStarCount / reviewCount; reviewAverage = reviewAverage * 100; reviewAverage = Math.round(reviewAverage); reviewAverage = reviewAverage / 100; System.out.println("Total reviews: " + reviewCount); System.out.println("Average review score: " + reviewAverage); } }
Java Code:import java.util.Scanner; public class KeyboardInput { int firstStar, secondStar, thirdStar, fourthStar, fifthStar; public void fiveStar() { Scanner starScanner = new Scanner(System.in); System.out.println("Number of 1 star reviews: "); firstStar = starScanner.nextInt(); System.out.println("Number of 2 star reviews: "); secondStar = starScanner.nextInt(); System.out.println("Number of 3 star reviews: "); thirdStar = starScanner.nextInt(); System.out.println("Number of 4 star reviews: "); fourthStar = starScanner.nextInt(); System.out.println("Number of 5 star reviews: "); fifthStar = starScanner.nextInt(); } }
Java Code:public class ReviewLauncher { public static void main (String[] args) { ReviewHandler start = new ReviewHandler(); start.reviews(); } }
Last edited by FatalSylence; 10-11-2010 at 11:14 PM. Reason: Updates
-
A few suggestions:
1) keyboardInput should not extend reviewHandler since it doesn't satisfy the "is-a" rule. At it's core it isn't a reviewHandler.
2) Give keyboardInput class fields with public getter methods. That way objects that use keyboardInput objects can extract the information that it collects.
3) Please, please, please learn Java coding conventions and give your code proper identifiers and formatting. As written your code is very hard for others (namely us and your instructors) to read. Class names should be capitalized and proper indentation should be used.
4) If your code generates any errors, please post the error messages themselves and indicate which line(s) are causing the errors.
Luck!
- 10-11-2010, 11:04 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 9
- Rep Power
- 0
Thank you for your prompt reply.
I have edited my original post with fixed grammar, punctuation, and updated code. Please take a look if you have time.
In response to your reply:
1. I changed it to ReviewHandler extends KeyboardInput. Otherwise it didn't seem they were seeing each others variables.
2. How would I go about doing that, exactly? I am very, very new to Java.
3. I worked on my sloppy code a little bit in my original example. Hopefully it is a little easier to read now. =)
4. My code runs perfectly, but when I run the program, enter in the 1, 2, 3, 4, and 5 star reviews, it returns a value of zero. I am guessing it's something with the public getter methods that you mentioned. The variable has no way to keep it's value in it's cross class travel. I have no idea how to implement public getter methods though. Would you be willing to help me on that?
Thank you very much!
- 10-11-2010, 11:29 PM #4
Member
- Join Date
- May 2010
- Posts
- 27
- Rep Power
- 0
Instead of keyboard.fivestart(), just make it fivestar(). Java thinks it's already a function because you've extended the class.
reviewAverage / 100;
Also, be CAREFUL with integer division. I've had tons of problems. I always convert to float, then divide, then convert back to integer.
- 10-12-2010, 12:06 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
The "extends" keyword and subclassing is not about making variables or methods defined in one class visible in another. If that is your only reason for using "extends" then don't do that: instead find some other way of making things accessible (ask if need be).
(With only a week or so's Java under your belt it's a fair bet that subclassing is not necessary or correct.)
I note that your ReviewHandler HAS-A KeyboardInput. So possibly where you have things like
Java Code:int starOne = firstStar * 1;
you really mean
Java Code:int starOne = keyboard.firstStar * 1;
"HAS-A" - the idea of one type having a member variable of another type, like ReviewHandler and KeyboardInput - is a much more appropriate idiom at this stage.
-
- 10-12-2010, 05:29 AM #7
Member
- Join Date
- Oct 2010
- Posts
- 9
- Rep Power
- 0
This is exactly what the problem was. I replaced firstStar with keyboard.firstStar and it worked. Thank you so much!
While I am here, I'd like to ask if you guys see ways my code could improve. I am so new at this and I know from experience that I should try my hardest to do things right the first time. What are some ways I could improve it? Data encapsulation? If so, how would I go about doing that?
Thank you all for your help! It is much appreciated!
Similar Threads
-
How to use another image using a keyboard input
By Rekuta in forum New To JavaReplies: 0Last Post: 05-13-2010, 05:00 PM -
Manipulating components of a seperate GUI Class
By ribbs2521 in forum New To JavaReplies: 1Last Post: 10-20-2009, 10:53 PM -
get keyboard input while running in the background?
By gen1mx6 in forum Advanced JavaReplies: 16Last Post: 07-16-2009, 03:51 PM -
Basic keyboard input and edit
By BHCluster in forum New To JavaReplies: 18Last Post: 08-08-2008, 11:52 PM -
Polled keyboard input through swing
By Prometheus in forum Advanced JavaReplies: 2Last Post: 02-04-2008, 04:05 PM
Bookmarks