Results 1 to 18 of 18
  1. #1
    tzzt123 is offline Member
    Join Date
    May 2013
    Posts
    23
    Rep Power
    0

    Default New to Java. Hello! and a question.

    Hello

    I'm making my first Java steps, at least as a programmers. I am currently trying to build a rock scissors game. Not sure I am doing it right, I decided to try building 2 classes: the first one would take input from players: 1 for rock, 2 for paper,etc) and another to compare and find out who wins.

    A: do you think it is the right way or at least a simple way?
    b: how do i access integers or strings from a different class?

    any help would be appreciated

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

    Default Re: New to Java. Hello! and a question.

    A: separating the game logic from the actual input is a nice way to do it;
    B: the class that 'owns' the information should allow you to get the information; the de-facto way of doing this is through getters() (and maybe setters()) methods.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    tzzt123 is offline Member
    Join Date
    May 2013
    Posts
    23
    Rep Power
    0

    Default Re: New to Java. Hello! and a question.

    Hi JoshA.

    Thanks for replying.

    I think I am confused with definitions: Classes are infarct complete differences files within a project. Right? So what are public and private ? modifiers ?

  4. #4
    tzzt123 is offline Member
    Join Date
    May 2013
    Posts
    23
    Rep Power
    0

    Default Re: New to Java. Hello! and a question.

    I meant function. Accessing int from a different function

  5. #5
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: New to Java. Hello! and a question.

    you have have links between Classes:

    Java Code:
    public class Eg {
    
    private int number;
    
    public Eg(int x) {
    
    number = x;
    }
    
    public int getNumber() {
    
    return number;
    
    }
    
    }
    And to access these from another Class you need:

    Java Code:
    public class Test {
    
    private Eg example;
    
    public Test(){
    
    example = new Eg(5);
    }
    
    public void accessDifferentClassVariable(){
    
    System.out.print("Number from Eg Class is :"+example.getNumber());
    
    }
    
    }
    This will print out:Number from Eg Class is : 5

  6. #6
    tzzt123 is offline Member
    Join Date
    May 2013
    Posts
    23
    Rep Power
    0

    Default Re: New to Java. Hello! and a question.

    I found a way to share them between functions. Simply declare them in the public class.

    However, now I have a function that is supposed to an integer from the user and return it back so I can use it in another function, but it always returns 0.

    private int TakeInput() {
    int p1 = readInt("Player1:);
    int p2 = readInt("Player2:");

    Whats's missing ?

  7. #7
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: New to Java. Hello! and a question.

    but the is a huge difference between :

    Declaring a variable in a public Class as a public variable

    Declaring a variable in a public Class as a private variable.


    int p1 = readInt("Player1:) makes no sense,

    i dont know what your readInt method is, but if you are asking for user input you need a Scanner object,

    the ("Player1:) makes no sense, you are saying its a String using " ", and the colon is not needed.

    int p1 = readInt(Player1); is the correct way of saying it, but i cant say this is correct for your program as 1, i cant see all your code, 2 i cant see the readInt method, 3, i doubt readInt takes a Player Object as parameters


    look up Scanners and loops, this is how i would get the user input.

    I would also encapsulate my data

  8. #8
    tzzt123 is offline Member
    Join Date
    May 2013
    Posts
    23
    Rep Power
    0

    Default Re: New to Java. Hello! and a question.

    would readLine not be more simple than scanner ?

  9. #9
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: New to Java. Hello! and a question.

    just saying personally id use a Scanner as it has a lot more functionality but its your preference

  10. #10
    tzzt123 is offline Member
    Join Date
    May 2013
    Posts
    23
    Rep Power
    0

    Default Re: New to Java. Hello! and a question.

    not sure how to use it yet so i wanted to keep it simple for now........... but now there is obviously another problem.

  11. #11
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: New to Java. Hello! and a question.

    the other problem is?

  12. #12
    tzzt123 is offline Member
    Join Date
    May 2013
    Posts
    23
    Rep Power
    0

    Default Re: New to Java. Hello! and a question.

    I need to be able to test my code with Junit. Followed tutorials but a little confused. I worked out the Paper Rock Scissors on my own (not sure if it is very very clean but I did what I could for now) and not I need to build a small test case for it.

  13. #13
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: New to Java. Hello! and a question.

    well you need to import the JUnit libary.

    Have you looked at the API for JUnit tests (Assert), they can help you more than anything or anyone! :D

  14. #14
    tzzt123 is offline Member
    Join Date
    May 2013
    Posts
    23
    Rep Power
    0

    Default Re: New to Java. Hello! and a question.

    I think I need to clean this code first. Dont I ?

    Would you like me to paste the whole thing so you could give me advice on how to clean it? The program actually works now. It is just messy. A little.

  15. #15
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: New to Java. Hello! and a question.

    If it works then its not hugely important to clean your code its only a simple program so having it slightly messy isnt a major issue, you just have to be aware of correct syntax and encapsulation for future projects.

  16. #16
    tzzt123 is offline Member
    Join Date
    May 2013
    Posts
    23
    Rep Power
    0

    Default Re: New to Java. Hello! and a question.

    yeah but if it is not clean I guess it would be hard to think about a way to test it..........

  17. #17
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: New to Java. Hello! and a question.

    not really, look at the Junit API, you will find every possible way to test your code in there.

  18. #18
    tzzt123 is offline Member
    Join Date
    May 2013
    Posts
    23
    Rep Power
    0

    Default Re: New to Java. Hello! and a question.

    Just did it.

    I wanted to see if someone would review it. Both.

Similar Threads

  1. java question
    By jm47 in forum New To Java
    Replies: 2
    Last Post: 04-12-2012, 04:23 AM
  2. Java Question
    By Etimer in forum New To Java
    Replies: 7
    Last Post: 02-04-2012, 07:09 AM
  3. Java Question [Beginner Question]
    By joker760 in forum New To Java
    Replies: 3
    Last Post: 12-13-2011, 04:01 PM
  4. Java question
    By TGH in forum New To Java
    Replies: 12
    Last Post: 11-27-2009, 02:05 PM
  5. Java Question
    By Jay-1.1 in forum New To Java
    Replies: 11
    Last Post: 05-01-2008, 04:04 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
  •