![]() ![]() Not only is it easier to see the patten at a glance, it is also more efficient, in that it compiles to a single tableswitch opcode. ![]() The compiler will embed just one long string constant in the bytecode.)Ī chain of if-elses would be better written using a switch block. "Enter 3 to find the area of a Triangle or " + "Enter 2 to find the area of a Square, " + "Enter 1 to find the area of a Rectangle, " + Instead of writing the menu as a very long string, it would be more manageable to split it: ("Welcome to the Area Calculator! " + You would be better off naming the class AreaCalculator, which would be more meaningful than Main. Making a package for a one-class program seems like overkill. Int radius = Integer.parseInt(scanner.next()) ("Please enter the radius of the circle: ") Int triangleHeight = Integer.parseInt(scanner.next()) ("Please enter the height of the triangle: ") Int triangleBase = Integer.parseInt(scanner.next()) ("Please enter the base of the triangle: ") Int lengthOfASide = Integer.parseInt(scanner.next()) Int width = Integer.parseInt(scanner.next()) ("Please enter the width of the rectangle: ") Int length = Integer.parseInt(scanner.next()) ("Please enter the length of the rectangle: ") Int userInput = Integer.parseInt(scanner.next()) ("Welcome to the Area Calculator! Enter 1 to find the area of a Rectangle, Enter 2 to find the area of a Square, Enter 3 to find the area of a Triangle or Enter 4 to find the area of a Circle") ![]() Scanner scanner = new Scanner(System.in) What this does is that it prompts the user to select a shape, and asks for the details needed to calculate the area of that specific shape. Good programmer and crack the java software developer interview with ease.This is a simple Area Calculator coded in Java. Public static void calculate(int sideLength)Įxplore complete java concepts from the Java programming examples and get ready to become a In this method, first the side length of square will be taken by the user and this value will be passed to the user defined method calculate() where the logic to find area and perimeter is implemented. Perimeter of Square = 16 Method-3: Java Program to Find Area and Perimeter of Square By Using User Defined Method ("Enter length of any side of square : ") taking the length of any side of square from the user Scanner class object created fro taking user input Method-1 and Method-2 is same the difference is only in assigning side length value of square. In this method, the side length of square will be taken as user input and then the area and perimeter of square will be calculated as per method-1. Perimeter of Square = 20 Method-2: Java Program to Find Area and Perimeter of Square By Using Dynamic Values length of each side of square is given Which will be used to calculate the area and perimeter of square by using the formula. In this method, the side length of square is already declared in the program and stored by the variable sideLength. Method-1: Java Program to Find Area and Perimeter of Square By Using Static Values Then area of square => perimeter = 4*sideĭon’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews. => area = 16 Example- To find Perimeter of Square 4*side represents the 4*length of side of the square.side*side represents the length*breadth of the square.Program to Find Area and Perimeter of Squareīefore going into the program, let’s see how we find the area and perimeter of a square. In this article we will discuss about how to find area and perimeter of a square. In the previous article we have discussed Java Program to Find Area and Perimeter of Rectangle ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |