Advertisement
karlakmkj

Debugging using terminal

Dec 4th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.43 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class AreaCalculator {
  4.  
  5.     public static void main(String[] args) {
  6.    
  7.       Scanner keyboard = new Scanner(System.in);
  8.    
  9.       System.out.println("Shape Area Calculator");
  10.    
  11.       while(true) {   //indicates an infinite loop, hence break is written at the end to exit the loop
  12.    
  13.         System.out.println();
  14.         System.out.println("-=-=-=-=-=-=-=-=-=-");
  15.         System.out.println();
  16.         System.out.println("1) Triangle");
  17.         System.out.println("2) Rectangle");
  18.         System.out.println("3) Circle");
  19.         System.out.println("4) Quit");
  20.         System.out.println();
  21.      
  22.         System.out.print("Which shape: ");
  23.    
  24.       int shape = keyboard.nextInt();
  25.       System.out.println();
  26.    
  27.         if (shape == 1) {
  28.             area_triangle(5,6);   //arguments here act as 'default' -- not used here since each method ask user input  
  29.         } else if (shape == 2) {
  30.             area_rectangle(4,5);
  31.         } else if (shape == 3) {  //corrected to 3
  32.             area_circle(4);
  33.         } else if (shape == 4) {
  34.             quit();
  35.             break;
  36.         }
  37.       }
  38.   }
  39.    
  40.   public static double area_triangle(int base, int height) {
  41.    
  42.         Scanner keyboard = new Scanner(System.in);
  43.    
  44.         System.out.print("Base: ");
  45.         base = keyboard.nextInt();
  46.    
  47.         System.out.print("Height: ");
  48.         height = keyboard.nextInt();
  49.    
  50.         System.out.println();
  51.    
  52.         int A = (base * height) * 1/2;  //corrected to 1/2
  53.    
  54.         System.out.println("The area is " + A + ".");
  55.        
  56.         return A;
  57.     }
  58.    
  59.   public static int area_rectangle(int length, int width){
  60.    
  61.         Scanner keyboard = new Scanner(System.in);
  62.    
  63.         System.out.print("Length: ");
  64.         length = keyboard.nextInt();
  65.    
  66.         System.out.print("Width: ");
  67.         width = keyboard.nextInt();
  68.    
  69.         System.out.println();
  70.    
  71.         int A = length * width;
  72.    
  73.         System.out.println("The area is " + A + ".");
  74.    
  75.         return A;
  76.     }
  77.  
  78.   public static double area_circle(int radius) {
  79.    
  80.         Scanner keyboard = new Scanner(System.in);
  81.    
  82.         System.out.print("Radius: ");
  83.         radius = keyboard.nextInt();
  84.    
  85.         System.out.println();
  86.    
  87.         double A = Math.PI * radius * radius;
  88.    
  89.         System.out.println("The area is " + A + ".");
  90.    
  91.         return A;
  92.     }
  93.  
  94.     public static String quit() {
  95.         System.out.println("GoodBye");
  96.         return null;
  97.     }
  98. }
  99. /*
  100. To test for syntax error using the compiler, type:
  101. javac AreaCalculator.java
  102.  
  103. To test program for logic error, type:
  104. java AreaCalculator
  105. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement