Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

first project in java


Arabian's Avatar
Member
0 0

buddywithgol wrote: check out this simple code http://www.hellboundhackers.org/code/a-calc-2038_.html

Fixed spelling in title, korg.

This is certainly a first project. You're calling objects for no reason, you're doing things as complicated as possible, and you clearly don't have very good control of the language.

This did the trick in 1/4 of the lines. My code:



public class lol {

	public static void main(String[] args) {
		System.out.println("Enter *, /, +, -, or %");
		
		Scanner in = new Scanner (System.in);
		switch(in.nextLine()) {
			case "*": 
				System.out.println("Please enter in your numbers (with space): ");
				String[] nums0 = in.nextLine().split(" ");
				System.out.println(Integer.parseInt(nums0[0]) * Integer.parseInt(nums0[1]));
				break;
				
			case "/": 
				System.out.println("Please enter in your numbers (with space): ");
				String[] nums1 = in.nextLine().split(" ");
				System.out.println(Integer.parseInt(nums1[0]) / Integer.parseInt(nums1[1]));
				break;
				
			case "+": 
				System.out.println("Please enter in your numbers (with space): ");
				String[] nums2 = in.nextLine().split(" ");
				System.out.println(Integer.parseInt(nums2[0]) / Integer.parseInt(nums2[1]));
				break;
				
			case "-": 
				System.out.println("Please enter in your numbers (with space): ");
				String[] nums3 = in.nextLine().split(" ");
				System.out.println(Integer.parseInt(nums3[0]) / Integer.parseInt(nums3[1]));
				break;
				
			case "%": 
				System.out.println("Please enter in your numbers (with space): ");
				String[] nums4 = in.nextLine().split(" ");
				System.out.println(Integer.parseInt(nums4[0]) / Integer.parseInt(nums4[1]));
				break;
		}
	}

}

Added a few comments and formatted your code a bit to make it more readable and to start you off on the right path.

       public static void main(String[] args){ //String [] is convention.
 
 
    	   System.out.println("Press 1 to add\n" + "Press 4 to Multiply\n" + "Press 3 to Divide\n" + "Press 2 to subtract\n");  
    	   // learn your newline chars and formatting techniques so you don't have 5 print statements.
    	   
    	   Scanner  add = new Scanner(System.in);
    	   String add1 = add.nextLine(); //unnecessary char statement. if i put in "(" it still won't move to the next set of filters because it's not the proper char.
    	 
    	   /**
    	    * Use switch for chars and strings
    	    * it's just faster and better practice. Also
    	    * remember to add spaces for readability.
    	    */
    	   if(add2 == '1'){
    		   System.out.println("Enter your numbers");
    		   Project1 proob = new Project1();
    		   proob.addnum(5,6);
 
    	   }
           else if(add2 == '2'){
    		   System.out.println("Enter your numbers");
    		   Subtract sub = new Subtract();
    		   sub.subnum(0,0);
    	   }
           else if(add2 == '3'){
    		   System.out.println("Enter your numbers");
    		   Divide div = new Divide();
    		   div.divnum(0,0);
    	   }
           else if(add2 == '4'){
    		   System.out.println("Enter your numbers");
    		   Multiply mult = new Multiply();
    		   mult.multnum(0,0);
    	   }
 
 
       }
}```

Protip: Never do more than you have to. No one (including professors) are impressed by unnecessarily complicated and verbose code. The point of programming is to do what you need to do with elegance and concision. What is usually the best solution, is always the fastest, smallest, and usually best looking functional code.


My code optimized:


```markupimport java.util.Scanner;

public class lol {
	public static void main(String[]  args) {
	
		System.out.println("Enter *, /, +, -, or %");
		Scanner in = new Scanner (System.in);
		
		switch(in.nextLine()) {
			case "*": 
				System.out.println("Please enter in your numbers: ");
				System.out.println(in.nextInt() * in.nextInt());
				break;
				
			case "/": 
				System.out.println("Please enter in your numbers: ");
				System.out.println(in.nextInt() / in.nextInt());
				break;
				
			case "+": 
				System.out.println("Please enter in your numbers: ");
				System.out.println(in.nextInt() + in.nextInt());
				break;
				
			case "-": 
				System.out.println("Please enter in your numbers: ");
				System.out.println(in.nextInt() - in.nextInt());
				break;
				
			case "%": 
				System.out.println("Please enter in your numbers: ");
				System.out.println(in.nextInt() % in.nextInt());
				break;
		}
	}

}```

buddywithgol's Avatar
Member
0 0

thx yes, i am a beginner, i can see how your version of the code is better.:D


Arabian's Avatar
Member
0 0

buddywithgol wrote: thx yes, i am a beginner, i can see how your version of the code is better.:D

You'll get there. If you ever find yourself thinking "There's got to be an easier way of doing things (like parsing string numbers to integer)" - someone has probably already done something about it. Look up things you're not sure about in the Java API, and don't hesitate to ask us.


dopeboimag1k's Avatar
Member
0 0

Arabian wrote: [quote]buddywithgol wrote: thx yes, i am a beginner, i can see how your version of the code is better.:D

You'll get there. If you ever find yourself thinking "There's got to be an easier way of doing things (like parsing string numbers to integer)" - someone has probably already done something about it. Look up things you're not sure about in the Java API, and don't hesitate to ask us.[/quote]

Another thing that I have found in my years of programming is that if you are trying to do something and have no clue where to start, then odds are someone else has been where you are and someone has asked a similar question on a site such as stack overflow. Unless you are doing it for learning purposes, there is no need to reinvent the wheel. With Google and the Java API, you should be able to figure out anything you are trying to do as a beginner using Java.


Arabian's Avatar
Member
0 0

buddywithgol wrote: Another thing that I have found in my years of programming is that if you are trying to do something and have no clue where to start, then odds are someone else has been where you are and someone has asked a similar question on a site such as stack overflow. Unless you are doing it for learning purposes, there is no need to reinvent the wheel. With Google and the Java API, you should be able to figure out anything you are trying to do as a beginner using Java.

Are you trying to mock me, or are you just not reading the thread? I can never tell.


newbee's Avatar
Member
0 0

arabian , that's what dopeboimag1k wrote .

and as for the code , why don't you take the nos. as input in the beginning itself (i.e. outside the switch-case), and then take the user's choice ? that'd make the program much shorter .


tuere816's Avatar
Member
0 0

newbee wrote: and as for the code , why don't you take the nos. as input in the beginning itself (i.e. outside the switch-case), and then take the user's choice ? that'd make the program much shorter .

But , then would not there be a small limitation in user friendliness of the code..?

//possible division by zero?


tuere816's Avatar
Member
0 0

newbee wrote: and as for the code , why don't you take the nos. as input in the beginning itself (i.e. outside the switch-case), and then take the user's choice ? that'd make the program much shorter .

But , then would not there be a small limitation in user friendliness of the code..?

//possible division by zero?


newbee's Avatar
Member
0 0

(as for the division by 0) as this is his first java project , this much is acceptable

(as for the user friendliness) which is better , a little bit of user friendliness , or a shorter program ?


Arabian's Avatar
Member
0 0

UI and speed are not contrasting dichotomous values. It IS possible to have a friendly, fast and cost effective program, given you have the skill. Only idiots or noobs think like you do, newbee.

Did it in 5 lines:

		if (l == 0 || r == 0) {
			System.out.println("Please input valid numbers. Cannot divide by 0. \n");
			main(null);
		}
		else {
			System.out.println(l / r);
		}
	}```

And changing 1 in the division case:

```markupcompute(in.nextInt(), in.nextInt());```

Good code can be made extensible with minimal effort. Any condition you need may be met by simply adding onto good core structure.

newbee's Avatar
Member
0 0

thanks for the info and the flames … it feels good to have been flamed after a long time :)


newbee's Avatar
Member
0 0

i'm not a wellspring of knowledge like you , arabian , but this is how i did the program a couple years back :-

import java.util.Scanner;
public class calculator
{
    public static void main(String args[])throws IOException
    {
        Scanner sc = new Scanner(System.in);
        while(true)
        {
            System.out.println("Enter first no.");
            int a = sc.nextInt();
            System.out.println("Enter second no.");
            int b = sc.nextInt();
            System.out.println("Enter operator (+,-,* or /) ");
            char ch = (char)System.in.read();
            System.out.println("Answer :-");
            switch (ch)
            {
                case '+' :
                    System.out.println(a+b);
                    break;
                case '-' :
                    System.out.println(a-b);
                    break;
                case '*' :
                    System.out.println(a*b);
                    break;
                case '/' :
                {
                    if (b==0)
                        System.out.println("division by zero not possible");
                    else
                        System.out.println(a/b);
                    break;
                }
                default :
                    System.out.println("Wrong choice");
            }
            System.out.println("do again ? (enter n to terminate , anything else to continue");
            String again = sc.next();
            if (again.equalsIgnoreCase("n"))
                break ;   
        }
    }
}

        ```

what do you say ? 

Arabian's Avatar
Member
0 0

newbee wrote: i'm not a wellspring of knowledge like you , arabian , but this is how i did the program a couple years back :-

import java.util.Scanner;
public class calculator
{
    public static void main(String args[])throws IOException
    {
        Scanner sc = new Scanner(System.in);
        while(true)
        {
            System.out.println("Enter first no.");
            int a = sc.nextInt();
            System.out.println("Enter second no.");
            int b = sc.nextInt();
            System.out.println("Enter operator (+,-,* or /) ");
            char ch = (char)System.in.read();
            System.out.println("Answer :-");
            switch (ch)
            {
                case '+' :
                    System.out.println(a+b);
                    break;
                case '-' :
                    System.out.println(a-b);
                    break;
                case '*' :
                    System.out.println(a*b);
                    break;
                case '/' :
                {
                    if (b==0)
                        System.out.println("division by zero not possible");
                    else
                        System.out.println(a/b);
                    break;
                }
                default :
                    System.out.println("Wrong choice");
            }
            System.out.println("do again ? (enter n to terminate , anything else to continue");
            String again = sc.next();
            if (again.equalsIgnoreCase("n"))
                break ;   
        }
    }
}

        ```

what do you say ? 

I say you're throwing useless exceptions cuz you're not doing any IO, you've wrapped everything in a while(true) loop which is retarded, when you could simply call main() again, rather than trapping yourself in this costly loop, your filtering statements are incorrect, as I can use any decimal number to throw an error, and you have 4 print statements in the beginning of the method, which could be easily concat'd to one. All fun things to consider.


But better. Much better. And if your point was to show me that I could've added my filter in the case, you're absolutely right. My b.

newbee's Avatar
Member
0 0

thanks for pointing out my errors . i will be more careful next time . oh and thanks for the compliment .


ghost's Avatar
0 0

%5bquote%5d%5bb%5dnewbee+wrote%3a%5b%2fb%5d%0aarabian+%2c+that%26%2339%3bs+what+dopeboimag1k+wrote+.%0d%0a%0d%0aand+as+for+the+code+%2c+why+don%26%2339%3bt+you+take+the+nos.+as+input+in+the+beginning+itself+%28i.e.+outside+the+switch-case%29%2c+and+then+take+the+user%26%2339%3bs+choice+%3f+that%26%2339%3bd+make+the+program+much+shorter+.%5b%2fquote%5d