1.11M Members

cannot find symbol on my assignment

 
0
 

create a console application that compute the area of triangle, and ask the user if he want to repeat the program.
but i haved an 8 error saying cannot find symbol in line 19,23,26,27.

import java.util.Scanner;
public class Area3

{
    public static void main(String args[]) 
    {
        Scanner input = new Scanner(System.in);

        Area3 tri = new Area3();
        float base, height, area;
        String yes,no;
        tri.base();
        tri.height();
        tri.area();}


        public void base(){
        System.out.print("enter base of triangle");
        base = input.nextFloat();}

        public void height(){
        System.out.print("enter height of triangle");
        height = input.nextFloat();}

        public void area(){
        area = (base * height)/2;
        System.out.print("the area of triangle is:"+ area);

        System.out.println("Do you want to repeat the program? "Yes" or "No" ");
        if(yes)


    }
}
 
0
 

You are wise to try to break up your application into smaller methods, but you need to remember that you cannot use something from one method in another method. The variables input, base, height, and area are all owned by the main method and you cannot use them in any other method.

If you want to use something in a method you either need to create it in that method, make it part of the object (which is tri in this case), or else make it a parameter to the method.

 
0
 

as bguild said,

move line 10 out side of the main() method. if do that then those are available to all the methods by changing variable usage like as follows

1.instead of using 'base' variable in the base() method

base = input.nextFloat();

replace as follows

this.base = input.nextFloat();  // this line says current class instance method using current class instace variable

like this you do for the remaining 2 variables

then check it once remaining is as you wish

let me know the status

 
0
 

my apologies for the above post i couldnt read the code properly that was my mistake

please go thru this

as bguild said,

move line 10 out side of the main() method. if do that then those are available to all the methods by changing variable usage like as follows

1.instead of using 'base' variable in the base() method

base = input.nextFloat();

replace as follows

this.base = input.nextFloat();  // this line says current class instance method using current class instace variable(if you are doing the assignments inside methods)

otherwise if you want to assign values to those varibles using class object reference do as folloes

class_ref_name.base = input.nextFloat(); // like as for other variables 

like this you do for the remaining variable

i think the following code would be better one (after modifying your class)

 class Area3
    {
      public Scanner input =null;
      public float base, height, area;
    public static void main(String args[]){

    // your remaining code
      input = new Scanner(System.in);
      Area3 tri = new Area3();
      // call those methods        
      // your remaining code 

    }// end of main

    public void base(){
            System.out.println("enter value for base");
           this.base = input.nextFloat(); 
    }

    public void height(){
           System.out.println("enter value for height");
           this.height = input.nextFloat(); 
    }

    public void area(){
          this.area=this.height*this.base/2;
        // your code 
    }
    }// end of class

then check it once remaining is as you wish

let me know the status

 
0
 

it compiled with no error, it runs but nothing happened. no message from system.out.print

 
0
 

heres the code for the computation, i only need is to insert this program in a function and use the System.out.println("do you want to repeat the program? Yes or NO"); and then it will loop from the begining if the users select yes, and if no the program will end. help me that function method is dificult compared in other that i tried. Y_Y.

import java.util.Scanner;
public class Area2
{


    public static void main(String args[]) {

        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter width of Triangle ");
        float width = scanner.nextFloat();

        System.out.println("Enter height of Triangle ");
        float height = scanner.nextFloat();

        //calculating area of Triangle in Java
        float area = area(width, height);

        System.out.println("Area of Triangle is : " + area);

    System.out.println("Do you want to repeat the program again?");

    }   


    public static float area(float width, float height){
        return (width * height)/2;
    }
}
 
1
 

help you with what function? the iteration?

while ( true ){
    // your code
    String readAgain = scanner.next();
    if ( readAgain.equals("No"))
        break; // exit loop
}
 
0
 

Why do people prefer while over do-while so much, even when it's a perfect example ?

do {
   // your code
   String readAgain = scanner.next();
} while (readAgain.equalsIgnoreCase("yes"));
 
0
 

in my experience, there has to be the option that the statement is never true, so in that case the while is better :)

 
0
 

Sorry, I'm having a "senior moment"! Can you explain that again please?

 
0
 

the do while => minimum one time execution, with the test to repeat after the first iteration.
old habit of mine to give the option to end the application before the first iteration.

 
-1
 
Do this:

import java.util.Scanner;
public class Area3
{
float base, height, area; 

this declares global variables that can be reference from other methods. What you are doing in your example is trying to reference a "local" variable.
 
0
 

what i mean is the function - is a subroutine that contains one or more statements w/c perform specific tasks. i need to use class object to hold the computation but theres error like illegal start of expression.

i need to use:

Area3 tri = new Area3();
tri.base();
tri.height();
tri.area

and in the "do you want to repeat the program?" if yes if will loop back in tri.base

 
0
 

i made it with the function but why the area is always = 0.0 ?
can u help me add the System.out.println("do you want to repeat the program?")if yes it will loop again from the start. pls

import java.util.Scanner;
public class Area1
{

    float base, height, area;
    Scanner input = new Scanner(System.in);
    public static void main(String args[])
    {

        Area1 qwe = new Area1();
            qwe.base();
            qwe.height();
            qwe.area();}

        public void base(){
        System.out.println("Enter base of Triangle ");
        float base = input.nextFloat();}

        public void height(){
        System.out.println("Enter height of Triangle ");
        float height = input.nextFloat();}

        public void area(){
        float area = (base * height)/2;
        System.out.println("the Area of triangle is:" + area);

    }
}
 
0
 

The local variable named base is not the same as the instance field named base. You can set them to different values. The field will start as zero and if you don't give it a new value it will always be zero. When you do something like float base that is declaring a new thing called base, and please notice that you do that twice, so in total you have three things called base: a base field created on line 5, a base method created on line 15, and a base local variable created on line 17.

You give the base local variable the value from input.nextFloat(). You never give the base field a value, so it stays zero. And your height local field is also zero, so on line 24, base * height is always zero.

 
0
 

tnx sir i made it correctly.
last one problem the looping.

 
0
 

i almost finish it but theres 1 problem when i select the choice ==2 it calls again the height(); instead of ending the program?

import java.util.Scanner;
public class Area1
{

    float base, height, area;
    int choice;
    Scanner input = new Scanner(System.in);
    public static void main(String args[])
    {

        Area1 qwe = new Area1();
            qwe.base();
            qwe.height();
            qwe.area();
            qwe.choice();}

        public void base(){
        System.out.println("Enter base of Triangle ");
        base = input.nextFloat();}

        public void height(){
        System.out.println("Enter height of Triangle ");
        height = input.nextFloat();}

        public void area(){
        area = (base * height)/2;
        System.out.println("the Area of triangle is:\n" + area);}

        public void choice(){
        System.out.println("do you want to repeat the program?");
        System.out.println("\t\t[1]YES ");
        System.out.println("\t\t[2]NO ");
        choice = input.nextInt();

        if(choice == 1)
            base();
            height();
            area();
            choice();

        if(choice == 2)
            {System.exit(1);}
    }
}
 
1
 

The condition of an if statement only decides whether the next statement will be performed, not any statements after that. So on line 35 you have an if(choice == 1) which decides whether base() will happen, but the lines after that will always happen: height(); area(); choice(); etc.

If you want the if statement to control more than one statement, you need to group them together using {}, like if(a) {b(); c();}.

Question Answered as of 1 Year Ago by stultuske, bguild, JamesCherrill and 2 others
 
0
 

even though the question is answered already - do want to add this reply to ret801's post:
there is no such thing as "global variables" in Java. if you had read the previous posts, you might 've noticed this has been said already.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article