Results 1 to 7 of 7
  1. #1
    FatalSylence is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default 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);
    
    }
    }
    KeyboardInput class:

    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();
    
    }
    }
    ReviewLauncher class:

    Java Code:
    public class ReviewLauncher {
    
    public static void main (String[] args) {
    
    ReviewHandler start = new ReviewHandler();
    start.reviews();
    
    }
    }
    Last edited by FatalSylence; 10-12-2010 at 12:14 AM. Reason: Updates

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

    Default

    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!

  3. #3
    FatalSylence is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    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!
    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!

  4. #4
    somanayr is offline Member
    Join Date
    May 2010
    Posts
    27
    Rep Power
    0

    Default

    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.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by pbrockway2 View Post
    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.)
    Amen. I second this heartily and this the point I was trying to make (poorly in my first post): This is not a place where inheritance will help you but rather will harm you.

  7. #7
    FatalSylence is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    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.
    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

  1. How to use another image using a keyboard input
    By Rekuta in forum New To Java
    Replies: 0
    Last Post: 05-13-2010, 06:00 PM
  2. Manipulating components of a seperate GUI Class
    By ribbs2521 in forum New To Java
    Replies: 1
    Last Post: 10-20-2009, 11:53 PM
  3. get keyboard input while running in the background?
    By gen1mx6 in forum Advanced Java
    Replies: 16
    Last Post: 07-16-2009, 04:51 PM
  4. Basic keyboard input and edit
    By BHCluster in forum New To Java
    Replies: 18
    Last Post: 08-09-2008, 12:52 AM
  5. Polled keyboard input through swing
    By Prometheus in forum Advanced Java
    Replies: 2
    Last Post: 02-04-2008, 05:05 PM

Tags for this Thread

Posting Permissions

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