Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 hours to days and hours
Index -> Programming, Java -> Java Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
cool dude




PostPosted: Fri May 12, 2006 7:53 pm   Post subject: hours to days and hours

i am learning java so i'm making these little programs to get myself better at java. the reason i'm posting these so called programs is because i like criticism because i learn from it and can become a better programmer. i will start making a game and something bigger when i get more educated in java and when i have more time i.e. summer. for now here is my program. you enter #of hours and it will output #of days and hours.

code:

import java.io.*;

public class Time {
    public static void main(String[] args){
        String shours;
        int ihours;
        int hoursleft;
        int days;
       
        try{
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Enter the number of hours");
            shours = in.readLine();
            ihours = Integer.parseInt(shours);
            days = ihours/24    ;
            hoursleft = ihours%24;
            System.out.println(ihours + " hours = " + days + " days " + "and " + hoursleft + " hours");
        }catch(IOException io){
            System.out.println("can't read input");
        }catch(NumberFormatException nfe){
            System.out.println("can't convert");
        }
    }
}
Sponsor
Sponsor
Sponsor
sponsor
Vertico




PostPosted: Fri Jun 02, 2006 2:24 pm   Post subject: (No subject)

Claping Good luck with that. I personaly found Java interesting and fun to learn/use.
HellblazerX




PostPosted: Fri Jun 02, 2006 4:58 pm   Post subject: (No subject)

Not bad, but you could do without some of those variables, seeing as you only call them up once. Like shour. Rather than storing the input into shour, and then converting the integer with that, you could just convert the input into integer form. Same thing with your hoursleft and days. You could just perform the functions inside the output parameter. So it would look something like this:
code:
ihours = Integer.parseInt (in.readLine ());
System.out.println (ihours + " hours = " + (ihours / 24) + " days " + "and " + (ihours % 24) + " hours");

It just seems inefficient to have variables that only store values that will be used once. And good luck with your game.
wtd




PostPosted: Fri Jun 02, 2006 5:03 pm   Post subject: (No subject)

HellblazerX wrote:
Not bad, but you could do without some of those variables, seeing as you only call them up once. Like shour.


Or if nothing else, you could limit the scope of that variable.

I fear Turing genuinely cripples the mind with regards to scoping.
HellblazerX




PostPosted: Fri Jun 02, 2006 5:07 pm   Post subject: (No subject)

wtd wrote:
Or if nothing else, you could limit the scope of that variable.

I don't mean to go off topic, but what's scoping of a variable mean?
wtd




PostPosted: Fri Jun 02, 2006 5:35 pm   Post subject: (No subject)

HellblazerX wrote:
wtd wrote:
Or if nothing else, you could limit the scope of that variable.

I don't mean to go off topic, but what's scoping of a variable mean?


It is one of the most important fundamental concepts with which to become familiar.

In cool dude's program, the variable "shours" is declared in the method's scope. It is now available to the entire method. The thing is... it doesn't have to be. It's only ever used in the "try" block. Therefore it can be scoped locally within that block. It will not be accessible outside that block if this is done.
Display posts from previous:   
   Index -> Programming, Java -> Java Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: