First Java program made purely by myself.

Discussion in 'Programming General' started by slashshot007, Sep 7, 2007.

First Java program made purely by myself.
  1. Unread #1 - Sep 7, 2007 at 9:22 PM
  2. slashshot007
    Joined:
    May 6, 2006
    Posts:
    164
    Referrals:
    0
    Sythe Gold:
    0

    slashshot007 Active Member

    First Java program made purely by myself.

    this is the first program i made by myself. i'm quite proud of it. constructive criticism is appreciated.

    Code:
    import java.util.Scanner;
    
    public class tempurature {
        public static void main( String [] args ){
        	short loop = 2;      
        	while (loop == 2 ){
            	System.out.print( "If you would like to convert from Fahrenheit to Celsius, press 1 or,\n if you would like to convert Celsius to Fahrenheit, press 2: " );
            	Scanner input = new Scanner(System.in);  	
            	short stupid = input.nextShort();
            	if (stupid == 1){
             	   	System.out.print( "Enter Degrees Fahrenheit: " );
             	   	int f = input.nextInt();
        	       	double c = (5.0/9.0)*(f-32);
        	       	System.out.println( f + " Degrees Fahrenheit is equal to " + c + " Degrees Celsius.");
        	       	loop = 1;
        	    } else if (stupid == 2){
        	       	System.out.print( "Enter Degrees Celsius: ");
        	       	int c = input.nextInt();
        	       	double f = (9.0/5.0)*c+32;
       	         	System.out.print( c + " Degrees Celsius is equal to " + f + " Degrees Fahrenheit." );
       	         	loop = 1;
      	      	} else{
      	          	System.out.println( "Please Try Again. \n") ;
      	          	loop = 2;
      	    	}
    		} 
    	}
    }
     
  3. Unread #2 - Sep 7, 2007 at 10:50 PM
  4. Olan14
    Joined:
    Jan 26, 2007
    Posts:
    581
    Referrals:
    0
    Sythe Gold:
    0

    Olan14 Forum Addict

    First Java program made purely by myself.

    Uhmm that equation for conversion we learned in 6th grade lolll. But nice for a beginner.
     
  5. Unread #3 - Sep 9, 2007 at 9:30 PM
  6. slashshot007
    Joined:
    May 6, 2006
    Posts:
    164
    Referrals:
    0
    Sythe Gold:
    0

    slashshot007 Active Member

    First Java program made purely by myself.

    updated it so if you don't enter 1 or two it loops the program.
     
  7. Unread #4 - Sep 9, 2007 at 11:19 PM
  8. Fagex_SantaClaus
    Referrals:
    0

    Fagex_SantaClaus Guest

    First Java program made purely by myself.

    Wait, your first program converts F to C. LELZ.
    Umm, i still haven't memorized that equation <_<
     
  9. Unread #5 - Sep 14, 2007 at 6:21 PM
  10. x&#8313;
    Referrals:
    1

    x&#8313; Guest

    First Java program made purely by myself.

    Nice work. I suggest you improve on naming your variables a little bit. Variable names should tell anyone that reads the code exactly what a variable is used for. c and f are alright names, but in bigger programs, single character variable names are discouraged unless used inside for loops, etc.
     
  11. Unread #6 - Sep 16, 2007 at 6:59 AM
  12. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    First Java program made purely by myself.

    I tidied it up for you:

    Code:
    import java.util.Scanner;
    
    public class fcConverter {
    	static double ConvertC(int Celcius) {
    		return (double)(9.0/5.0)*Celcius+32;
    	}
    	
    	static double ConvertF(int Fahrenheit) {
    		return (double)(5.0/9.0)*(Fahrenheit-32);
    	}
        
        public static void main(String[] args) {
        	boolean loop = true;
    			
    		System.out.println("Welcome to my Temperature Converter!\n");
    		
    		do {
    			try {
    				System.out.println("Press:\n\t'1' to convert Fahrenheit to Celcius\n\t'2' to convert Celcius to Fahrenheit\n\t'0' to quit");
    				System.out.print("Enter your choice: ");
            		Scanner input = new Scanner(System.in);
            		short choice = input.nextShort();
            		switch(choice) {
            			case 0:
    	        			System.out.println("\n\nThankyou for using my temperature converter!");
            				loop = false;
            				break;
            			case 1:
    	        			System.out.print("Input Fahrenheit: ");
            				int ftemp = input.nextInt();
            				System.out.println(ftemp + " degrees Fahrenheit is " + ConvertF(ftemp) + " degrees Celcius.\n");
            				break;
            			case 2:
    	        			System.out.print("Input Celcius: ");
            				int ctemp = input.nextInt();
            				System.out.println(ctemp + " degrees Celcius is  " + ConvertC(ctemp) + " degrees Fahrenheit.\n");
            				break;
            			default:
    						System.out.println("Please enter either 1 or 2.\n");
            		}       			
            	} catch(java.util.InputMismatchException exc) {
    				System.out.println("Error! Wrong input!\n");
    			}
    		} while(loop==true);
        }
    }
    output:
    Remember: there are such primitive types as booleans, and when checking a true or false statement, its best to use the do-while to make it neater.

    Tip: If you're learning how to manage input and output, a core thing to learn is exception handling. You see the 'try' and 'catch' statement in my code? Thats what generates the text "Error! Wrong input!" rather than crashing the program. Error handling is an art in itself, but its a core skill if you wish to program in Java.

    Good for a first app. My first app was "hello world" from my book xD
     
< [Help Request]Transpiring to VB.NET | C# more simular to java than C++ >

Users viewing this thread
1 guest


 
 
Adblock breaks this site