Question

Write a java code based on the below question.

Q4 revision: Assume the following is ur input file. 5 Rectangle Length Width A 3 B 7 6 с 3 8 Write a program to read the abov

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Find the code for the above question below, read the comments provided in the code for better understanding. If found helpful please do upvote this.
Please refer to the screenshot of the code to understand the indentation of the code.

Copyable Code

import java.util.*;//for SCanner class

import java.io.*;//for file opeartions

//class

public class RectArea {

    // main driver code of the program

    public static void main(String args[]) {

        try {

            Scanner x = new Scanner(new File("input.txt"));

            //output file to save the output

            PrintWriter out = new PrintWriter("output.txt");

            out.println("Rectangle\tArea");

            //read the first line

            x.nextLine();

            while(x.hasNext()){

                //read th line and tokenize the data

                char rectName =x.next().charAt(0);

                int len=x.nextInt();

                int bre=x.nextInt();

                //calulate the area

                int area  = len*bre;

                //save into file

                out.println(rectName+"\t\t"+area);

            }

            //close both the files

            x.close();

            out.close();

            System.out.println("output.txt file created");

        } catch (Exception e) {

            System.out.println("File Not Found : "+e.getMessage());

            e.printStackTrace();

        }

    }

}

SCreenshot of code

1 import java.util.*;//for Scanner class port java.io.*;//for file opeartions 2 3 4 5 6 7 8 9 10 11 12 13 //class public clas

Input File (input.txt)

Rectangle Length Width
A 5 3
B 7 6
C 3 8   

input.txt 1 Rectangle 2 А 3 B 4 C Length Width 5 3 7 6 3 8

Output

Console.output

PS F:\HomeworkLib\java> cd f:\che output.txt file created

output.txt file

output.txt - Notepad File Edit Format View Help Rectangle Area A 15 B 42 с 24

Add a comment
Know the answer?
Add Answer to:
Write a java code based on the below question. Q4 revision: Assume the following is ur...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (NEED FULL CODE PLEASE)write a java program class to calculate and display the areas of 10 rectan...

    (NEED FULL CODE PLEASE)write a java program class to calculate and display the areas of 10 rectangles using arrays. * input validation sizes must be between 0-50 inches output to be displayed on the screen and send to an output file. *the output must appear in the following format. rectangle length width area 1    2.0    3.0    6.0 2 - - - - 10 display the rectangle with largest area. rectangle1 area= display the rectangle with smallest area. rectangle2 area=

  • solve it with Java please Write a Java program that prompts the user to input length...

    solve it with Java please Write a Java program that prompts the user to input length and width of a shape. If length is equal to width, call a method to calculate the area of the shape and send only one side as parameter to this method. This meth return the area of the shape. If length is not equal to width, call a method to calculate the area of the shape and send both width and length as parameters...

  • 4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java...

    4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java source code file named H01_43.java (your main class is named H01_43). Problem: Write a program that prompts the user for the name of a Java source code file (you may assume the file contains Java source code and has a .java filename extension; we will not test your program on non-Java source code files). The program shall read the source code file and output...

  • In Java Exercise #2: (Java) Design and implement a program (name it ComputeAreas) that defines four...

    In Java Exercise #2: (Java) Design and implement a program (name it ComputeAreas) that defines four methods as follows: Method squareArea (double side) returns the area of a square. Method rectangleArea (double width, double length) returns the area of a rectangle. Method circleArea (double radius) returns the area of a circle. Method triangleArea (double base, double height) returns the area of a triangle. In the main method, test all methods with different input value read from the user. Document your...

  • Java only please Write a program that displays the following menu: Geometry Calculator 1.       Calculate the...

    Java only please Write a program that displays the following menu: Geometry Calculator 1.       Calculate the Area of a Circle 2.       Calculate the Area of a Triangle 3.     Calculate the Area of a Rectangle 4.       Quit Enter your choice (1-4): If the user enters 1, the program should ask for the radius of the circle and then display its area. Use the formula:      area = ∏r2    Use 3.14159 for ∏. If the user enters 2 the program should ask for...

  • Description Create an object-oriented program that uses inheritance to perform calculations on a rectangle or a...

    Description Create an object-oriented program that uses inheritance to perform calculations on a rectangle or a square. Sample Output (Your output should be similar to the text in the following box) Rectangle Calculator Rectangle or square? (r/s): r Height: 5 Width: 10 Perimeter: 30 Area: 50 Continue? (y/n): y Rectangle or square? (r/s): s Length: 5 Perimeter: 20 Area: 25 Continue? (y/n): n Thank you for using my app Specifications Use a Rectangle class that provides attributes to store the...

  • What if you had to write a program that would keep track of a list of...

    What if you had to write a program that would keep track of a list of rectangles? This might be for a house painter to use in calculating square footage of walls that need paint, or for an advertising agency to keep track of the space available on billboards. The first step would be to define a class where one object represents one rectangle's length and width. Once we have class Rectangle, then we can make as many objects of...

  • (PYTHON) Write a program that displays the following menu:. , Write Algorithm for Code Geometry Calculator...

    (PYTHON) Write a program that displays the following menu:. , Write Algorithm for Code Geometry Calculator 1. Calculate the Area of a Circle 2. Calculate the Area of a Rectangle 3. Calculate the Area of a Triangle 4. Quit Enter your choice (1 - 4): If the user enters 1, the program should ask for the radius of the circle and then display its area. If the user enters 2, the program should ask for the length and width of...

  • Program Description: A Java program is to be created to produce Morse code. The Morse code...

    Program Description: A Java program is to be created to produce Morse code. The Morse code assigns a series of dots and dashes to each letter of the alphabet, each digit, and a few special characters (such as period, comma, colon, and semicolon). In sound-oriented systems, the dot represents a short sound and the dash represents a long sound. Separation between words is indicated by a space, or, quite simply, the absence of a dot or dash. In a sound-oriented...

  • Homework description::::: Write JAVA program with following description. Sample output with code will be helful... A...

    Homework description::::: Write JAVA program with following description. Sample output with code will be helful... A compiler must examine tokens in a program and decide whether they are reserved words in the Java language, or identifiers defined by the user. Design a program that reads a Java program and makes a list of all the identifiers along with the number of occurrences of each identifier in the source code. To do this, you should make use of a dictionary. The...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT