Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Hi, your may be seeing alot of me soon ;)

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Location
    France
    Posts
    2
    My Mood
    Cheerful
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Talking Hi, your may be seeing alot of me soon ;)

    Hi guys,

    I'm new at Java, but I'm new and learning fast. I brought a book of Amazon called 'Java for Dummies' by Barry Burd, I recommend this book, it is easy to read and well explained. I am willing to learn alot about Java, hoping to be able to orientate myself to Android application development. I speak english and french fluently, though I don't think french will help alot here
    I have written very little thing so far in Java, including a simple calculator (no gui) and a couple of other various thing. I'm new to all this, but I'm motivated.
    The first thing I'm gonna ask is:
    -Is this good for my first code ever in Java?
    import static java.lang.System.in;
    import static java.lang.System.out;
    import java.util.Scanner;
     
    public class calculatorCore {
     
    	public static void main(String[] args) {
    		Scanner input = new Scanner(in);
    		boolean realAnswerMenu = false;
     
    		out.println("------ extremeCalc by " + info.creatorName + "------");
    		out.println();
    		out.println("Menu-------------------------------");
    		out.println("-- 1 - Calculate now! -------------");
    		out.println("-- 2 - Info -----------------------");
    		out.println("-----------------------------------");
    		out.println();
    		do {
    			out.print("MENU > ");
    			String menu = input.next();
    			try {
    				int menuInt = Integer.parseInt(menu);
    				out.println();
    				switch (menuInt) {
    					case 1:
    						realAnswerMenu = true;
    						break;
    					case 2:
    						out.println("Simple, just type what your asked, and press [ENTER], if you want to stop, when they ask you a sign type stop and fill the input spaces with random ints until it stops, uppercase or lowercase does not matter.");
    						out.println("Made by " + info.creatorName);
    						out.println("Build " + info.build + " "+ info.versionName);
    						out.println();
    						break;
    					default:
    						out.println("Sorry, dunno that option, enter a number depending what you want and press [ENTER]");
    						out.println();
    				}
    			} catch (NumberFormatException e) {
    				out.println();
    				out.println("Sorry, dunno that option, enter a number depending what you want and press [ENTER]");
    				out.println();
    			}
    		} while (realAnswerMenu == false);
    		out.println();
    		String sign;
    		double num;
    		out.print("NUM  > ");
    		double tot = input.nextDouble();
    		boolean stop = false;
     
    		do {
    			do {
    				out.print("SIGN > ");
    				sign = input.next();
    				if (!sign.equalsIgnoreCase("stop") && !sign.equals("-") && !sign.equals("+") && !sign.equals("*") && !sign.equals("/")) {
    					out.println("ERROR> Sign not recognised");
    				}
    				if (sign.equalsIgnoreCase("stop")) {
    					stop = true;
    				}
    			} while (!sign.equalsIgnoreCase("stop") && !sign.equals("-") && !sign.equals("+") && !sign.equals("*") && !sign.equals("/"));
    			out.print("NUM  > ");
     
    			num = input.nextDouble();
    			if (sign.equals("+")) {
    				tot = calculations.addition(tot , num);
    			}
    			if (sign.equals("-")) {
    				tot = calculations.subtraction(tot , num);
    			}
    			if (sign.equals("*")) {
    				tot = calculations.multiplication(tot , num);
    			}
    			if (sign.equals("/")) {
    				tot = calculations.division(tot , num);
    			}
    			out.println("TOTAL> " + tot);
    		} while (stop != true);
    		out.print("Thanks HF for using my calculator :D, " + info.creatorName);
    	}
     
    }
    public class calculations {
     
    	public static double addition(double tot , double num) {
    		return tot += num;
    	}
    	public static double subtraction(double tot , double num) {
    		return tot -= num;
    	}
    	public static double multiplication(double tot , double num) {
    		return tot *= num;
    	}
    	public static double division(double tot , double num) {
    		return tot /= num;
    	}
     
    }
    public class info {
     
    	static int build = 1;
    	static String versionName = "0.1 ALPHA";
    	static String creatorName = "zeokila";
     
    }
    Please give me tips to make it better.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Hi, your may be seeing alot of me soon ;)

    Please read the following: http://www.javaprogrammingforums.com...e-posting.html
    I have moved your post to a more appropriate category.
    Some tips:
    a) I recommend you read Code Conventions for the Java Programming Language
    b) add some comments to your code (preferable in javadoc format)
    c) What happens when a user enters a non-expected value - you should validate user input (or catch the exceptions thrown by parse*)

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Location
    France
    Posts
    2
    My Mood
    Cheerful
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hi, your may be seeing alot of me soon ;)

    OK, thanks, and sorry for posting in the wrong section :S

  4. #4
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: Hi, your may be seeing alot of me soon ;)

    the question about the structure is about OO. Anyway, the code looks great for a first timer, try to start organizing what you want done in groups.
    I think your calc class should probably just be a method. and your info class could also be a method (perhaps one that printed out the version info).

    methods DO STUFF to things
    classes MAKE THINGS

  5. #5
    Member clydefrog's Avatar
    Join Date
    Feb 2012
    Posts
    67
    Thanks
    15
    Thanked 2 Times in 2 Posts

    Default Re: Hi, your may be seeing alot of me soon ;)

    I agree with what JonLane said, you can just make your calculations class into methods. I dont think there is any use in having it be a class; you can probably make it a void method too.

    like for example:

    public static void  add(double tot, double dum)
    {
         tot = tot + num;
         System.out.print("TOTAL: " + tot);
    }

Similar Threads

  1. Help with looping etc THANKS ALOT IN ADVANCE
    By moesom in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 15th, 2011, 10:43 AM