MathStart.Java file already created this is Math.java FYI (IF statements)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xosunkist
    New Member
    • Mar 2007
    • 10

    #1

    MathStart.Java file already created this is Math.java FYI (IF statements)

    Here is the code I created below. There is a problem that it does not recognize the choice statements or public static. What did I do wrong so I can compile?


    import java.util.*;
    public class Math
    {
    double d1, d2, d3, total, d4, d5, sum;
    public static void output()
    {
    Scanner scannerObject = new Scanner(System. in);
    System.out.prin tln("Let's add three whole numbers");
    choice=scannerO bject.nextInt() ;
    if(choice==1)
    {
    Math.addThreeWh ole();
    }
    if (choice==2)
    {
    Math.addTwoDeci mals();
    }
    else if ((choice<1)||(c hoice>2))
    {
    System.out.prin t("You must enter either 1 or 2.\n");
    System.out.prin t("You will need to run the program again.\n\n");
    }
    }
    public static void addThreeWhole()
    {
    Scanner scannerObject = new Scanner(System. in);
    System.out.prin t("Enter whole number:");
    d1 = scannerObject.n extDouble();
    System.out.prin t("Enter whole number:");
    d2 = scannerObject.n extDouble();
    System.out.prin t("Enter whole number:");
    d3 = scannerObject.n extDouble();
    System.out.prin t("\n");
    total = d1+d2+d3;
    }
    public static void addTwoDecimals( )
    {
    Scanner scannerObject = new Scanner(System. in);
    System.out.prin t("Enter decimal point number:");
    d4 = scannerObject.n extDouble();
    System.out.prin t("Enter decimal point number:");
    d5 = scannerObject.n extDouble();
    sum = d4+d5;
    }
    }
  • xosunkist
    New Member
    • Mar 2007
    • 10

    #2
    MathStart.Java file already created this is Math.java FYI (IF statements)

    Here is the code I created below. There is a problem that it does not recognize the choice statements or public static. What did I do wrong so I can compile?


    import java.util.*;
    public class Math
    {
    double d1, d2, d3, total, d4, d5, sum;
    public static void output()
    {
    Scanner scannerObject = new Scanner(System. in);
    System.out.prin tln("Let's add three whole numbers");
    choice=scannerO bject.nextInt() ;
    if(choice==1)
    {
    Math.addThreeWh ole();
    }
    if (choice==2)
    {
    Math.addTwoDeci mals();
    }
    else if ((choice<1)||(c hoice>2))
    {
    System.out.prin t("You must enter either 1 or 2.\n");
    System.out.prin t("You will need to run the program again.\n\n");
    }
    }
    public static void addThreeWhole()
    {
    Scanner scannerObject = new Scanner(System. in);
    System.out.prin t("Enter whole number:");
    d1 = scannerObject.n extDouble();
    System.out.prin t("Enter whole number:");
    d2 = scannerObject.n extDouble();
    System.out.prin t("Enter whole number:");
    d3 = scannerObject.n extDouble();
    System.out.prin t("\n");
    total = d1+d2+d3;
    }
    public static void addTwoDecimals( )
    {
    Scanner scannerObject = new Scanner(System. in);
    System.out.prin t("Enter decimal point number:");
    d4 = scannerObject.n extDouble();
    System.out.prin t("Enter decimal point number:");
    d5 = scannerObject.n extDouble();
    sum = d4+d5;
    }
    }

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      where u define choice.....

      listen one more thing ur static method can access only non-static data members

      welcome ..... have a good day

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by xosunkist
        Here is the code I created below. There is a problem that it does not recognize the choice statements or public static. What did I do wrong so I can compile?


        import java.util.*;
        public class Math
        {
        double d1, d2, d3, total, d4, d5, sum;
        public static void output()
        {
        Scanner scannerObject = new Scanner(System. in);
        System.out.prin tln("Let's add three whole numbers");
        choice=scannerO bject.nextInt() ;
        if(choice==1)
        {
        Math.addThreeWh ole();
        }
        if (choice==2)
        {
        Math.addTwoDeci mals();
        }
        else if ((choice<1)||(c hoice>2))
        {
        System.out.prin t("You must enter either 1 or 2.\n");
        System.out.prin t("You will need to run the program again.\n\n");
        }
        }
        public static void addThreeWhole()
        {
        Scanner scannerObject = new Scanner(System. in);
        System.out.prin t("Enter whole number:");
        d1 = scannerObject.n extDouble();
        System.out.prin t("Enter whole number:");
        d2 = scannerObject.n extDouble();
        System.out.prin t("Enter whole number:");
        d3 = scannerObject.n extDouble();
        System.out.prin t("\n");
        total = d1+d2+d3;
        }
        public static void addTwoDecimals( )
        {
        Scanner scannerObject = new Scanner(System. in);
        System.out.prin t("Enter decimal point number:");
        d4 = scannerObject.n extDouble();
        System.out.prin t("Enter decimal point number:");
        d5 = scannerObject.n extDouble();
        sum = d4+d5;
        }
        }
        Please use code tags next time when posting code.
        Where did you tell the compiler that choice is an int?

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by xosunkist
          Here is the code I created below. There is a problem that it does not recognize the choice statements or public static. What did I do wrong so I can compile?


          import java.util.*;
          public class Math
          {
          double d1, d2, d3, total, d4, d5, sum;
          public static void output()
          {
          Scanner scannerObject = new Scanner(System. in);
          System.out.prin tln("Let's add three whole numbers");
          choice=scannerO bject.nextInt() ;
          if(choice==1)
          {
          Math.addThreeWh ole();
          }
          if (choice==2)
          {
          Math.addTwoDeci mals();
          }
          else if ((choice<1)||(c hoice>2))
          {
          System.out.prin t("You must enter either 1 or 2.\n");
          System.out.prin t("You will need to run the program again.\n\n");
          }
          }
          public static void addThreeWhole()
          {
          Scanner scannerObject = new Scanner(System. in);
          System.out.prin t("Enter whole number:");
          d1 = scannerObject.n extDouble();
          System.out.prin t("Enter whole number:");
          d2 = scannerObject.n extDouble();
          System.out.prin t("Enter whole number:");
          d3 = scannerObject.n extDouble();
          System.out.prin t("\n");
          total = d1+d2+d3;
          }
          public static void addTwoDecimals( )
          {
          Scanner scannerObject = new Scanner(System. in);
          System.out.prin t("Enter decimal point number:");
          d4 = scannerObject.n extDouble();
          System.out.prin t("Enter decimal point number:");
          d5 = scannerObject.n extDouble();
          sum = d4+d5;
          }
          }
          Keep one problem in one thread please.

          Comment

          • xosunkist
            New Member
            • Mar 2007
            • 10

            #6
            Originally posted by dmjpro
            where u define choice.....

            listen one more thing ur static method can access only non-static data members

            welcome ..... have a good day
            Code:
                import java.util.*;
            public class Math
            {
            
            			public static double addThreeWhole =0;
            
                        public static double addTwoDecimals =0;
            
            public static double choice =0;
            
            double d1, d2, d3, total;
            	public static void output()
            
            	{
            
            
            		Scanner scannerObject = new Scanner(System.in);
            		System.out.println("Let's add three whole numbers");
            		choice=scannerObject.nextDouble();
            		if(choice==1)
            		{
            			Math.addThreeWhole();
            		}
            		if (choice==2)
            		{
            			Math.addTwoDecimals();
            		}
            		else if ((choice<1)||(choice>2))
            		{
            			System.out.print("You must enter either 1 or 2.\n");
            			System.out.print("You will need to run the program again.\n\n");
            		}
            	}
            	public static void addThreeWhole()
            	{
            		Scanner scannerObject = new Scanner(System.in);
            		System.out.print("Enter whole number:");
            		d1 = scannerObject.nextDouble();
            		System.out.print("Enter whole number:");
            		d2 = scannerObject.nextDouble();
            		System.out.print("Enter whole number:");
            		d3 = scannerObject.nextDouble();
            		System.out.print("\n");
            		total = d1+d2+d3;
            	}
            	public static void addTwoDecimals()
            	{
            double d4, d5, sum;
            		Scanner scannerObject = new Scanner(System.in);
            		System.out.print("Enter decimal point number:");
            		d4 = scannerObject.nextDouble();
            		System.out.print("Enter decimal point number:");
            		d5 = scannerObject.nextDouble();
            		sum = d4+d5;
            	}
            }


            This is my new version. It is telling me now that non static variable cannot be referenced to a static context?? What variable is that? What do I need to change? I'm lost? I've updated this though from the Choice.

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by xosunkist
              Code:
                  import java.util.*;
              public class Math
              {
              
              			public static double addThreeWhole =0;
              
                          public static double addTwoDecimals =0;
              
              public static double choice =0;
              
              double d1, d2, d3, total;
              	public static void output()
              
              	{
              
              
              		Scanner scannerObject = new Scanner(System.in);
              		System.out.println("Let's add three whole numbers");
              		choice=scannerObject.nextDouble();
              		if(choice==1)
              		{
              			Math.addThreeWhole();
              		}
              		if (choice==2)
              		{
              			Math.addTwoDecimals();
              		}
              		else if ((choice<1)||(choice>2))
              		{
              			System.out.print("You must enter either 1 or 2.\n");
              			System.out.print("You will need to run the program again.\n\n");
              		}
              	}
              	public static void addThreeWhole()
              	{
              		Scanner scannerObject = new Scanner(System.in);
              		System.out.print("Enter whole number:");
              		d1 = scannerObject.nextDouble();
              		System.out.print("Enter whole number:");
              		d2 = scannerObject.nextDouble();
              		System.out.print("Enter whole number:");
              		d3 = scannerObject.nextDouble();
              		System.out.print("\n");
              		total = d1+d2+d3;
              	}
              	public static void addTwoDecimals()
              	{
              double d4, d5, sum;
              		Scanner scannerObject = new Scanner(System.in);
              		System.out.print("Enter decimal point number:");
              		d4 = scannerObject.nextDouble();
              		System.out.print("Enter decimal point number:");
              		d5 = scannerObject.nextDouble();
              		sum = d4+d5;
              	}
              }


              This is my new version. It is telling me now that non static variable cannot be referenced to a static context?? What variable is that? What do I need to change? I'm lost? I've updated this though from the Choice.
              1.)Avoid naming classes with names of classes in the java API (especially from the java.lang package)
              variables d1,d2, etc must be declared static as well if you want to access them in static methods.

              Comment

              • xosunkist
                New Member
                • Mar 2007
                • 10

                #8
                Thanks, I have it now.

                Comment

                Working...