CrazyEngineers
  • Output of this byte code is not working properly

    Rits rishi

    Member

    Updated: Oct 26, 2024
    Views: 1.2K
    import java.util.*;
     
    public class Code3 {
     
    /**
    * @ param args the command line arguments
    */
    public static void main(String[] args) {
    Scanner s1=new Scanner(System.in);
    System.out.println("enter your %age");
    float a=s1.nextFloat();
    System.out.println("enter your location");
    String b=s1.next();
    if(a>90 && b=="chandigarh"){
    System.out.println("selected");
    }
    else if(a>95&&b!="chandigarh"){
    System.out.println("selected");
    }
    else if(a>=80&&a<90&&b=="chandigarh"){
    System.out.println("waiting");
    }
    else if(a>90&&a<95&&b!="chandigarh"){
    System.out.println("waiting");
    }
    else if(a<80&&b=="chandigarh"){
    System.out.println("rejected");
    }
    else if(a<90&&b!="chandigarh"){
    System.out.println("rejected");
    }
    }
    }
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Manish Goyal

    MemberJul 11, 2013

    Basic approach for debugging any program is to analyse what will the input and what will be the output, after this comes the logic.

    You should better print value of a and b and check if it is taking correct input
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberJul 11, 2013

    1. what is not working?? You didn't provided input and output and what are your expected output.

    2. Comparing string like
     b=="chandigarh"
    is wrong way compare strings, If you want to compare strings you should always use
    string1.equals(string2)
    .


    run the following code you will understand.

    String a = new String("ABC");
            String b = "ABC";
            String c = "ABC";
            if (a==b){
                System.out.println(true+"a==b");
            }
         
            if (b==c){
                System.out.println(true+"b==c");
            }
    
    Are you sure? This action cannot be undone.
    Cancel
  • Rits rishi

    MemberJul 15, 2013

    okay! i got it! finally..😀 my program is running... now i am actually having confusion at function calling..
    my new program:-
    import java.util.*;
    public class pro {
      int a,b;
    static Scanner s1=new Scanner(System.in);
    public static void main(String args[]){
    pro p1=new pro();
     
    int o=p1.menu();
    System.out.println("o="+o);
    p1.input();
    p1.choice(o);
     
    }
    public int menu() //first function
    { int num;
    System.out.println("welcome to my calculator");
    System.out.println("choose your operation: \n enter 1 for addition \n enter 2 for subtraction \n enter 3 for multiplication \n enter 4 for division \n enter 5 for modulus");
    System.out.println("enter your choice number:");
    int c=s1.nextInt();
    num=s1.nextInt();
    return num;
    }
    public void input() //2nd function
    {
    System.out.println("enter first number:");
    a=s1.nextInt();
    System.out.println("enter second number:");
    b=s1.nextInt();
     
    }
    public int add(int x,int y) //3rd function
    {
    int z=x+y;
    return z;
    }
    public int sub(int x,int y) //4th function
    {
    int z=x-y;
    return z;
    }
    public int mul(int x,int y) //5th function
    {
    int z=x*y;
    return z;
    }
    public int divide(int x,int y) //6th function
    {
    int z=x/y;
    return z;
    }
    public int mod(int x,int y) //7th function
    {
    int z=x%y;
    return z;
    }
    public int choice(int c) //8th function
    {
      int f=0;
    if(c==1)
    f=add(a,b);
    else if (c==2)
        f=sub(a,b);
    else if (c==3)
        f=mul(a,b);
    else if (c==4)
        f=divide(a,b);
    else if (c==5)
        f=mod(a,b);
     
    return f;
    }
     
    }
    //this calculator is not returning values..KINDLY help me in configuring my mistakes.. i want to be "code confident!". 😐
    Are you sure? This action cannot be undone.
    Cancel
  • simplycoder

    MemberJul 15, 2013

    Rits rishi
    okay! i got it! finally..😀 my program is running... now i am actually having confusion at function calling..
    my new program:-
    import java.util.*;
    public class pro {
      int a,b;
    static Scanner s1=new Scanner(System.in);
    public static void main(String args[]){
    pro p1=new pro();
     
    int o=p1.menu();
    System.out.println("o="+o);
    p1.input();
    p1.choice(o);
     
    }
    public int menu() //first function
    { int num;
    System.out.println("welcome to my calculator");
    System.out.println("choose your operation: \n enter 1 for addition \n enter 2 for subtraction \n enter 3 for multiplication \n enter 4 for division \n enter 5 for modulus");
    System.out.println("enter your choice number:");
    int c=s1.nextInt();
    num=s1.nextInt();
    return num;
    }
    public void input() //2nd function
    {
    System.out.println("enter first number:");
    a=s1.nextInt();
    System.out.println("enter second number:");
    b=s1.nextInt();
     
    }
    public int add(int x,int y) //3rd function
    {
    int z=x+y;
    return z;
    }
    public int sub(int x,int y) //4th function
    {
    int z=x-y;
    return z;
    }
    public int mul(int x,int y) //5th function
    {
    int z=x*y;
    return z;
    }
    public int divide(int x,int y) //6th function
    {
    int z=x/y;
    return z;
    }
    public int mod(int x,int y) //7th function
    {
    int z=x%y;
    return z;
    }
    public int choice(int c) //8th function
    {
      int f=0;
    if(c==1)
    f=add(a,b);
    else if (c==2)
        f=sub(a,b);
    else if (c==3)
        f=mul(a,b);
    else if (c==4)
        f=divide(a,b);
    else if (c==5)
        f=mod(a,b);
     
    return f;
    }
     
    }
    //this calculator is not returning values..KINDLY help me in configuring my mistakes.. i want to be "code confident!". 😐
    First of all this code lacks documentation.
    You should document each and every logic that is used describing the logic on higher level of abstraction.

    When debugging, try to use break-points, watch variables, stack-trace, it will give you immense knowledge about your own program.

    If you are using normal notepad and command prompt, try debugging by printing the values.

    something like this: int c=s1.nextInt();
    System.out.println("c = "+c);

    I notice that you are not using c anywhere.
    int c=s1.nextInt();
    num=s1.nextInt();

    do you think this might be a problem.
    Try inputing same value twice.
    Are you sure? This action cannot be undone.
    Cancel
  • rahul69

    MemberJul 15, 2013

    #-Link-Snipped-# just comment the following line and see if it works:
    int c=s1.nextInt();
    Are you sure? This action cannot be undone.
    Cancel
  • nareshkumar6539

    MemberJul 15, 2013

    Rits rishi
    okay! i got it! finally..😀 my program is running... now i am actually having confusion at function calling..
    my new program:-
    import java.util.*;
    public class pro {
      int a,b;
    static Scanner s1=new Scanner(System.in);
    public static void main(String args[]){
    pro p1=new pro();
     
    int o=p1.menu();
    System.out.println("o="+o);
    p1.input();
    p1.choice(o);
     
    }
    public int menu() //first function
    { int num;
    System.out.println("welcome to my calculator");
    System.out.println("choose your operation: \n enter 1 for addition \n enter 2 for subtraction \n enter 3 for multiplication \n enter 4 for division \n enter 5 for modulus");
    System.out.println("enter your choice number:");
    int c=s1.nextInt();
    num=s1.nextInt();
    return num;
    }
    public void input() //2nd function
    {
    System.out.println("enter first number:");
    a=s1.nextInt();
    System.out.println("enter second number:");
    b=s1.nextInt();
     
    }
    public int add(int x,int y) //3rd function
    {
    int z=x+y;
    return z;
    }
    public int sub(int x,int y) //4th function
    {
    int z=x-y;
    return z;
    }
    public int mul(int x,int y) //5th function
    {
    int z=x*y;
    return z;
    }
    public int divide(int x,int y) //6th function
    {
    int z=x/y;
    return z;
    }
    public int mod(int x,int y) //7th function
    {
    int z=x%y;
    return z;
    }
    public int choice(int c) //8th function
    {
      int f=0;
    if(c==1)
    f=add(a,b);
    else if (c==2)
        f=sub(a,b);
    else if (c==3)
        f=mul(a,b);
    else if (c==4)
        f=divide(a,b);
    else if (c==5)
        f=mod(a,b);
     
    return f;
    }
     
    }
    //this calculator is not returning values..KINDLY help me in configuring my mistakes.. i want to be "code confident!". 😐
    In main method you are just calling the choice() method you are not storing the return value from choice()
    p1.choice(o); change the statement to below
    System.out.println("result is :"+p1.choice(o));( OR )
    int result=p1.choice(o);
    Systme.out.println("result is: "+result);
    Are you sure? This action cannot be undone.
    Cancel
  • Rits rishi

    MemberJul 17, 2013

    nareshkumar6539
    In main method you are just calling the choice() method you are not storing the return value from choice()
    p1.choice(o); change the statement to below
    System.out.println("result is :"+p1.choice(o));( OR )
    int result=p1.choice(o);
    Systme.out.println("result is: "+result);
    thankyou so much!!!!! it is returning values now.. 😀 but i think i am still confused in function calling with arguements..... i don't actually realize when and where to pass arguements and when do we have to store a function into some variable?? can you please tell me any link to a website that clears my confusion or any tutorial that could help me??
    Are you sure? This action cannot be undone.
    Cancel
  • Rits rishi

    MemberJul 17, 2013

    rahul69
    #-Link-Snipped-# just comment the following line and see if it works:
    int c=s1.nextInt();
    thankx for the help.. but even after comment it was not returning the values.
    Are you sure? This action cannot be undone.
    Cancel
  • Rits rishi

    MemberJul 17, 2013

    simplycoder
    First of all this code lacks documentation.
    You should document each and every logic that is used describing the logic on higher level of abstraction.

    When debugging, try to use break-points, watch variables, stack-trace, it will give you immense knowledge about your own program.

    If you are using normal notepad and command prompt, try debugging by printing the values.

    something like this: int c=s1.nextInt();
    System.out.println("c = "+c);

    I notice that you are not using c anywhere.
    int c=s1.nextInt();
    num=s1.nextInt();

    do you think this might be a problem.
    Try inputing same value twice.
    thankyou.. but i think i am still confused in function calling with arguements..... i don't actually realize when and where to pass arguements and when do we have to store a function into some variable?? can you please tell me any link to a website that clears my confusion or any tutorial that could help me??
    Are you sure? This action cannot be undone.
    Cancel
  • simplycoder

    MemberJul 17, 2013

    Rits rishi
    thankyou.. but i think i am still confused in function calling with arguements..... i don't actually realize when and where to pass arguements and when do we have to store a function into some variable?? can you please tell me any link to a website that clears my confusion or any tutorial that could help me??
    Before writing the function, think whether you would like to use the result some where. If yes, then return the variable and store the returned value from the function and use it wherever you like.
    If not, then don't.

    Example,
    1.)Write a program (WAP) with functions, to check if a number is prime. Print the answer from a method dedicated only to display the result, all function calling has to be done from main.

    2.) WAP with functions a program to check if a number is an Armstrong number, Print the answer from a method dedicated only to display the result, all function calling has to be done from main.

    3.)WAP with function to display the following in these scenarios
    If user enters
    1, then print "Hello Java."
    2, then print "Hello C"
    3, then print "Hello C++"
    4, then print "CE is awesome!"

    Before writing the program, think if you need to store the result of the function some where and code accordingly.
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register