How to make a program calculate the area of several objects separately using the values given by the user (scanner)

Asked

Viewed 1,971 times

0

I am new here and new in programming, I believe that the best way to establish the concepts and learn is in practice.

So I tried to do some basic programming on Eclipse (also use Intellij) and at first it worked, I wanted to use the Scanner of java I couldn’t get it right and I’m in it on time...

I believe it’s quite simple, the program consists of calculate to object area (triangle and trapezoid I inserted, just to test) with the value given by the user for example:

The formula of the area of a triangle is A = (b*h)/2 being b = base and h = altura.

The values of b and h who will give is the user in the console and so the program will calculate and return the value of the area (A) of settlement with the chosen geometric object.

But the problem is not in the resolution of the account, this part is all right.

The problem is time to make the user choose which geometric object (triangle or trapezoid) he wants to know the area for the program to calculate and execute only the block of code of that object, ignoring the other since it was not chosen.

My program asks for the value of both and regardless of what I do it does not execute only one of them, can be several mistakes I made and as a layman in this subject I ask for help to solve, either by a totally new code or reusing mine.

Follow the code below:

package Course;

import java.util.Scanner;

public class Utilizandoscanner1 {

public static void main(String[] args) {
    /*Descubra a área do que o usuário passar
     * Trapézio: A = ((B + b)/2) * h
     * B = Base maior, b = base menor, h = altura
     * 
     * Triângulo: A = (B*h)/2
     * B = Base, h = altura
     */


    System.out.println("Bem-vindo ao calculador de área!");
    System.out.println("Informe qual objeto quer calcular: ");

    Scanner input = new Scanner(System.in);

    boolean trapézio = true, triângulo = true;

    if(trapézio){
        Double B,b,h,A;
        //coletando os valores do usuário
        System.out.println("Informe o valor de B = base maior: ");
        B = input.nextDouble();
        System.out.println("Informe o valor de b = base menor: ");
        b = input.nextDouble();
        System.out.println("Informe o valor de h = altura");
        h = input.nextDouble();
        //calculando
        A = ((B + b)/2)*h;
        System.out.println("O valor dado é B: "+B+" b: "+b+" h: "+h);
        System.out.println("A área do trapézio é: "+A);
        input.close();
    }
    if(triângulo){
        Double B,h,A;
        //coletando valores do usuário
        System.out.println("Informe o valor de B = base: ");
        B = input.nextDouble();
        System.out.println("Informe o valor de h = altura: ");
        h = input.nextDouble();
        //calculando
        A = (B*h)/2;
        System.out.println("O valor dado é B: "+B+" h: "+h);
        System.out.println("A área do triângulo é: "+A);
        input.close();
    }   


}

}

  • You can make a kind of options menu for the user by asking him to type 1- to triangle, 2 to square etc. Using a if, will evaluate which option he chose and then call the method accordingly for that geometric shape (you can build one for each shape). Within each method, you can ask the user for the parameters you need for that particular shape (radius to circle, side to square etc.). It’s a very basic way of doing it, there’s better, but for starters it’s okay.

  • I made some changes, can you take a look ?

  • You have set booleans, but lack to think how you will associate what the user has typed and these booleans to run the block of code that interests you. Think first, "run" the step by step of your program in your head and only then code. Programming is spending 90% thinking about the solution of the problem and 10% currently typing.

  • Yes, I understand that mental exercise is essential, but I’ve been in it for about 5 hours kkkk, seriously, I’ve said a lot, I’ve done it again, I’ve tried a class-oriented and a main (I don’t know if that’s how you define it) but it’s complicated.

  • Forget object orientation for now. You have more basic questions than that to solve (something normal for a beginner) before. For example, explain yourself why you do a certain thing. You have stated booleans, but you do not know what you want to do with them. How about initializing them with false and only assign true for the boolean that represents the geometric shape the user has chosen? Of course for this you need to first evaluate what the user wrote to then know which boolean to modify, correct?

  • 1

    Well, I’ve declared booleans so that when the user types a geometric object on the console it becomes true and runs his code block, and the rest of the geometric objects are false, not running any, but I’m not getting through it in practice, I feel that something is missing, in the code now I put the ''if'' = true, when I put false in both the console did not let me type anything, so I conclude that I need to find a way to consider false all but what was typed in the console

  • It doesn’t let you type because you practically offer no chance for the user to type something out of the if, only when the code falls on any of the blocks inside it. So, if the variables are initialized with false, no block will run and you won’t even be able to type anything. It is concluded then that you should offer him a chance first of all. After he type, you will have to take what he typed and compare: if he typed X, then I must set the boolean X to true, if he typed Y, then Y must be true etc.

  • You also concluded that if you leave the variables as true, your code will always run all blocks, right? That’s why they should start as false and only one being true at a time.

  • But regardless of what I do it always starts to execute the code by the first 'if' and if I for example leave trapeze as false and triangle as true it hangs on the trapeze and I can’t get it to ''jump' this step, something I didn’t add to the code for when it appears on the console " Tell me which object you want to calculate: "and I type ''triangle', it automatically jumps the trapeze, or I lack knowledge or am doing something wrong.

Show 4 more comments

1 answer

1


    public static void main(String[] args) {
        /*Descubra a área do que o usuário passar
         * Trapézio: A = ((B + b)/2) * h
         * B = Base maior, b = base menor, h = altura
         *
         * Triângulo: A = (B*h)/2
         * B = Base, h = altura
         */

        boolean trapezio = false, triangulo = false;

        System.out.println("Bem-vindo ao calculador de área!");
        System.out.println("Informe qual objeto quer calcular: ");

        Scanner input = new Scanner(System.in);

        String escolhaDoUsuario = new String();
        escolhaDoUsuario = input.next();

        if(escolhaDoUsuario.equals("trapezio")) {
            trapezio = true;
        } else if(escolhaDoUsuario.equals("triangulo")) {
            triangulo = true;
        }

        if(trapezio){
            Double B,b,h,A;
            //coletando os valores do usuário
            System.out.println("Informe o valor de B = base maior: ");
            B = input.nextDouble();
            System.out.println("Informe o valor de b = base menor: ");
            b = input.nextDouble();
            System.out.println("Informe o valor de h = altura");
            h = input.nextDouble();
            //calculando
            A = ((B + b)/2)*h;
            System.out.println("O valor dado é B: "+B+" b: "+b+" h: "+h);
            System.out.println("A área do trapézio é: "+A);
            input.close();
        }
        if(triangulo){
            Double B,h,A;
            //coletando valores do usuário
            System.out.println("Informe o valor de B = base: ");
            B = input.nextDouble();
            System.out.println("Informe o valor de h = altura: ");
            h = input.nextDouble();
            //calculando
            A = (B*h)/2;
            System.out.println("O valor dado é B: "+B+" h: "+h);
            System.out.println("A área do triângulo é: "+A);
            input.close();
        }
    }

This is a very naive implementation, but the idea was just to finish it using the logic you developed. A "version 2.0" could have several improvements:

  • And if the user type "TRIANGLE"?
  • Use methods instead of code blocks.
  • What if the user types a geometric shape not foreseen by the program? What if he does not type anything?
  • Ask the user if he wants to calculate another figure instead of the program simply exiting after running once.
  • Use only a structure of if.
  • Eliminate the booleans.

The list could be even bigger. It remains for you to study repetition structures like while, flow structures such as the switch, investigate the method equals() I used etc.

  • I know that here is written to avoid a "thank you", but thank you, today was the first day I used stackoverflow and I did not think that would be answered within what I asked, anyway, I will do yes a "2.0" and add information, thanks!.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.