Follow us on Twitter!
Become the change you seek in the world. - Gandhi
Thursday, April 24, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 28
Guests Online: 27
Members Online: 1

Registered Members: 82904
Newest Member: jamessmith123
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

first project in java

buddywithgol
Member



Posts: 84
Location: behind tor
Joined: 08.01.12
Rank:
Newbie
Warn Level: 30
Posted on 03-03-12 14:17
check out this simple code
http://www.hellbo. . .2038_.html









Fixed spelling in title, korg.


all good thing must come to an end, aka your computer.


www.imagenesanimadas.net/Informatica/Virus/virus10.gif

Edited by korg on 03-03-12 17:57
www.h4xer.com
Author

RE: first project in java

Arabian
Banned



Posts: 332
Location: inside you.
Joined: 22.09.10
Rank:
Apprentice
Posted on 03-03-12 22:06
buddywithgol wrote:
check out this simple code
http://www.hellbo. . .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:

Code
import 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 (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.

Code
public class Project { /* make it public. It's good practice and you'll learn why later. */
       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:


Code
import 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;
      }
   }

}






G'bye y'all! I was an asshole, So korg banned me.

Edited by Arabian on 04-03-12 01:13
Author

RE: first project in java

buddywithgol
Member



Posts: 84
Location: behind tor
Joined: 08.01.12
Rank:
Newbie
Warn Level: 30
Posted on 03-03-12 22:19
thx yes, i am a beginner, i can see how your version of the code is better.Grin


all good thing must come to an end, aka your computer.


www.imagenesanimadas.net/Informatica/Virus/virus10.gif
www.h4xer.com
Author

RE: first project in java

Arabian
Banned



Posts: 332
Location: inside you.
Joined: 22.09.10
Rank:
Apprentice
Posted on 03-03-12 22:23
buddywithgol wrote:
thx yes, i am a beginner, i can see how your version of the code is better.Grin


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.


G'bye y'all! I was an asshole, So korg banned me.
Author

RE: first project in java

dopeboimag1k
Member

Your avatar

Posts: 30
Location: the Internet
Joined: 01.05.11
Rank:
Moderate
Posted on 04-03-12 03:10
Arabian wrote:
buddywithgol wrote:
thx yes, i am a beginner, i can see how your version of the code is better.Grin


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.


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.


thirdgen.net16.net/images/signature.jpg

"Everybody wants greatness but nobody wants to work for it."

"If it was easy, everybody would do it."
Author

RE: first project in java

Arabian
Banned



Posts: 332
Location: inside you.
Joined: 22.09.10
Rank:
Apprentice
Posted on 04-03-12 03:26
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.


G'bye y'all! I was an asshole, So korg banned me.
Author

RE: first project in java

newbee
Member



Posts: 127
Location: India
Joined: 27.12.11
Rank:
Active User
Warn Level: 20
Posted on 04-03-12 06:44
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 .


www.hellboundhackers.org/sig/r/64440.png
i1078.photobucket.com/albums/w488/ads99nrg/signature.png

Author

RE: first project in java

tuere816
Member

Your avatar

Posts: 41
Location: In your closet, in your head
Joined: 27.11.11
Rank:
Apprentice
Posted on 04-03-12 16:08
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?


www.hellboundhackers.org/sig/hbh2.png

THE SWEETEST KID ALIVE ..... in Virtual world
img255.imageshack.us/img255/5292/2566.png
kulkarniabhishek94@yahoo.co.in
Author

RE: first project in java

tuere816
Member

Your avatar

Posts: 41
Location: In your closet, in your head
Joined: 27.11.11
Rank:
Apprentice
Posted on 04-03-12 16:08
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?


www.hellboundhackers.org/sig/hbh2.png

THE SWEETEST KID ALIVE ..... in Virtual world
img255.imageshack.us/img255/5292/2566.png
kulkarniabhishek94@yahoo.co.in
Author

RE: first project in java

newbee
Member



Posts: 127
Location: India
Joined: 27.12.11
Rank:
Active User
Warn Level: 20
Posted on 04-03-12 17:10
(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 ?


www.hellboundhackers.org/sig/r/64440.png
i1078.photobucket.com/albums/w488/ads99nrg/signature.png

Author

RE: first project in java

Arabian
Banned



Posts: 332
Location: inside you.
Joined: 22.09.10
Rank:
Apprentice
Posted on 05-03-12 19:34
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:

Code
private static void compute(int l, int r) {
      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:

Code
compute(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.


G'bye y'all! I was an asshole, So korg banned me.

Edited by Arabian on 06-03-12 10:55
Author

RE: first project in java

newbee
Member



Posts: 127
Location: India
Joined: 27.12.11
Rank:
Active User
Warn Level: 20
Posted on 06-03-12 09:34
thanks for the info and the flames ... it feels good to have been flamed after a long time Smile


www.hellboundhackers.org/sig/r/64440.png
i1078.photobucket.com/albums/w488/ads99nrg/signature.png

Author

RE: first project in java

newbee
Member



Posts: 127
Location: India
Joined: 27.12.11
Rank:
Active User
Warn Level: 20
Posted on 06-03-12 11:17
i'm not a wellspring of knowledge like you , arabian , but this is how i did the program a couple years back :-
Code
import java.io.* ;
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 ?


www.hellboundhackers.org/sig/r/64440.png
i1078.photobucket.com/albums/w488/ads99nrg/signature.png

Author

RE: first project in java

Arabian
Banned



Posts: 332
Location: inside you.
Joined: 22.09.10
Rank:
Apprentice
Posted on 06-03-12 19:26
newbee wrote:
i'm not a wellspring of knowledge like you , arabian , but this is how i did the program a couple years back :-
Code
import java.io.* ;
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.


G'bye y'all! I was an asshole, So korg banned me.

Edited by Arabian on 06-03-12 21:03
Author

RE: first project in java

newbee
Member



Posts: 127
Location: India
Joined: 27.12.11
Rank:
Active User
Warn Level: 20
Posted on 07-03-12 07:29
thanks for pointing out my errors . i will be more careful next time . oh and thanks for the compliment .


www.hellboundhackers.org/sig/r/64440.png
i1078.photobucket.com/albums/w488/ads99nrg/signature.png

Author

RE: 555-555-0199@example.com


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 13-03-12 08:18
%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