• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Very simple executable class

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everybody, new here
I have started my college experience before some months and finally got into my first Java class.
Well my teacher gave me an exercise for homework and i run into a main difficulty(Dont judge me hard i am new to this )

Difficulty:
1)I wrote that in jdeveloper but when i build and tried to run it, it did not let me input anything. (it is only a single class, all my previous homework excercises where at least 2 classes (1 executable & one non-executable) maybe i did something wrong)

Also i am open to any suggestions on how to make it look better (you know impress my teacher ) [ also i am not good with maths maybe my parameters are bad ]


Here are the instructions my teacher gave me :
Write an executable class to implement a very simple game, as follows:

player 1 is asked to specify a number from 0-100

player 2 is asked to guess the number

if the guess does not diverge from the number in question for more than 3 points, the program should display the message "Pretty good"
if the guess diverges for more than 3 points but no more than 10, the message should be "You can do it"
if the divergence is more than 10 points, the message should be "Never mind"
Make sure that you check both players' input for data type consistency and range validity.

And here is what i have done so far :
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Angelos Naoumis wrote:Very simple executable class

The way you wrote it, it is not simple. Welcome to the ranch

1. You don't need 2 scanner objects. You could change to:

2. Never ever omit curly braces (at least for now), even tho there are only 1 line of code within block. Your indented code should be looking similar to this:

Angelos Naoumis wrote:when i build and tried to run it, it did not let me input anything

For me it worked. What makes you think it is not letting you input anything?
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's probably not related to your issue, but I don't think it's a good idea to create multiple scanners that read from System.in.

How do you run your application? What happens when you run it?
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Since Scanners are not thread‑safe, there is the theoretical possibility that you could read the numbers in reverse order, but I think the chances of anything actually going wrong are very slim. You should not create two Scanners pointing to System.in because you only need one which you can reuse. And you always close a Scanner pointing to anything else but not if it points to System.in.
Why have you got everything in the main method?
 
Angelos Naoumis
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:It's probably not related to your issue, but I don't think it's a good idea to create multiple scanners that read from System.in.

How do you run your application? What happens when you run it?


Seems like i had to check some properties in jdeveloper .

Liutauras Vilda wrote:

Angelos Naoumis wrote:Very simple executable class

The way you wrote it, it is not simple. Welcome to the ranch

1. You don't need 2 scanner objects. You could change to:

2. Never ever omit curly braces (at least for now), even tho there are only 1 line of code within block. Your indented code should be looking similar to this:

Angelos Naoumis wrote:when i build and tried to run it, it did not let me input anything

For me it worked. What makes you think it is not letting you input anything?


Thanks for the information already applied them and cleary it looks better

Campbell Ritchie wrote:Welcome to the Ranch

Since Scanners are not thread‑safe, there is the theoretical possibility that you could read the numbers in reverse order, but I think the chances of anything actually going wrong are very slim. You should not create two Scanners pointing to System.in because you only need one which you can reuse. And you always close a Scanner pointing to anything else but not if it points to System.in.
Why have you got everything in the main method?


Is there any other way? I mean i would like to know how to save time and ram (this task is simple but a bigger one can profit from it)
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Angelos Naoumis wrote:. . . Is there any other way? I mean i would like to know how to save time and ram (this task is simple but a bigger one can profit from it)

Forget about saving time or RAM. You use as much time as you need. And memory is now cheap; you can buy more memory than you will need for less than we would spend going out to celebrate somebody's birthday.
Yes, you should create an object which embodies the requirements of your app.Now you can put the logic required in the start() method.

Please don't quote the whole of every preceding post.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out Main is a Pain for the correct way to use the main method.
 
Angelos Naoumis
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I find it intresting , but i have not been introduced yet to non-public methods, final , instance method or run/register methods that i read in the article so i am not certain about the differences so i used what i though is okay (i will definitely make a reference into my class about this so i can get more information).
We have worked with constructor only for while in class (also setters and getters) but we did not continue further for more information.
Whats the point of an empty constructor?
Can i call multiple methods from a launcher?
ex (assume that CashRegister , CashRegister1 and CashRegister2 are different classes (without containing main methods)

Also do i have to rename run method in each class in order to load properly? run/run1/run2?
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's expand the code given by Campbell:

Constructors are used to create objects, so if you don't need any parameters to create the object, you use an empty constructor. There is an empty constructor in every class by default.

 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should always write a constructor for every class, irrespective of the existence of the default constructor. The default might not be quite what you want, and you can always give the constructor private access so you put that main method in the GameStarter class and it cannot be used from anywhere else.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:Never ever omit curly braces (at least for now), even tho there are only 1 line of code within block.


What I think you meant to write was:

Never ever omit curly braces.

;-)
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Angelos Naoumis wrote:. . . Whats the point of an empty constructor?

You can use it when there is no information required from outside in order to create an instance of the class.

Can i call multiple methods from a launcher? . . .

Yes, but it is better to do that from a start, run, or similar method than from the main method

Also do i have to rename run method in each class in order to load properly? run/run1/run2

No. As long as they are in different classes, the JVM will find the correct method. But don't mark those methods static.Your example will not compile because you have declared the same variable thrice.But the names of the classes are very confusing.
 
straws are for suckers. tiny ads are for attractive people.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic