Can someone help rewrite the main method without the use of try, catch and input stream please?

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Can someone help rewrite the main method without the use of try, catch and input stream please?



 

1 import java.io.*;
2
3
// test class
4 public class Main {
5
// main method
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public static void main(String[] args) {
// required variables
int n = 0, i = 0, count = 0;
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// array to store the Toy objects
Toy[] toys = null;
try {
// create a read stream to input file
BufferedReader br = new Buffered Reader (new FileReader("Toys.txt"));
String line;
// read the first line from the file as the number of toys
n = Integer.parseInt(br.readLine().trim());
// initialize the size of the toys array
toys = new Toy[n];
// loop through each line in the file
while ((line = br.readLine()) != null) {
// create an Educational Toy object with the tokenized details
toys[i] = new Educational Toy(new Toy (tokens [1].trim(),
32 Double.parseDouble (tokens [2].trim()), Integer.parseInt(tokens [3].trim())), tokens [4].trim());
33
34
35
36
// tokenize the line w.r.t "*
String[] tokens = line.split(" ");
// if the toy is an educational toy
if (tokens [0].trim().equals IgnoreCase ("Educational Toy")) {
}
// else if the toy is an electronic toy
else if(tokens [0].trim().equals IgnoreCase ("ElectronicToy")) {
// create an ElectronicToy object with the tokenized details
toys[i] = new ElectronicToy (tokens [1].trim(), Double.parseDouble (tokens [2].trim()),
Integer.parseInt(tokens [3].trim()), tokens [4].trim());
}
i++;
}catch (IOException e) { System.out.println(e.getMessage()); }
// print the toys for testing purpose
System.out.println("\nToys read from the file for testing purpose: ");
// loop through the toys array
for (i = 0; i<n; i++) {
// display each toy
System.out.println (toys[i]);
System.out.println("=====
// declare and initialize cheapest & cheapest Toy
double cheapest = toys [0].getPrice ();
Toy cheapestToy = toys [0];
System.out.println("\nThe educational toys are: ");
// loop through the toys array
for (i = 0; i<n; i++) {
// if the current toy is cheaper than the cheapest toy
if(toys [i].getPrice () < cheapest) {
// reset the chepest and cheapest Toy to the current toy
cheapest = toys[i].getPrice();
cheapestToy = toys[i];
}
// if the current toy is an educational toy
if(toys [i] instanceof EducationalToy) {
=");
Transcribed Image Text:1 import java.io.*; 2 3 // test class 4 public class Main { 5 // main method 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 public static void main(String[] args) { // required variables int n = 0, i = 0, count = 0; 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 // array to store the Toy objects Toy[] toys = null; try { // create a read stream to input file BufferedReader br = new Buffered Reader (new FileReader("Toys.txt")); String line; // read the first line from the file as the number of toys n = Integer.parseInt(br.readLine().trim()); // initialize the size of the toys array toys = new Toy[n]; // loop through each line in the file while ((line = br.readLine()) != null) { // create an Educational Toy object with the tokenized details toys[i] = new Educational Toy(new Toy (tokens [1].trim(), 32 Double.parseDouble (tokens [2].trim()), Integer.parseInt(tokens [3].trim())), tokens [4].trim()); 33 34 35 36 // tokenize the line w.r.t "* String[] tokens = line.split(" "); // if the toy is an educational toy if (tokens [0].trim().equals IgnoreCase ("Educational Toy")) { } // else if the toy is an electronic toy else if(tokens [0].trim().equals IgnoreCase ("ElectronicToy")) { // create an ElectronicToy object with the tokenized details toys[i] = new ElectronicToy (tokens [1].trim(), Double.parseDouble (tokens [2].trim()), Integer.parseInt(tokens [3].trim()), tokens [4].trim()); } i++; }catch (IOException e) { System.out.println(e.getMessage()); } // print the toys for testing purpose System.out.println("\nToys read from the file for testing purpose: "); // loop through the toys array for (i = 0; i<n; i++) { // display each toy System.out.println (toys[i]); System.out.println("===== // declare and initialize cheapest & cheapest Toy double cheapest = toys [0].getPrice (); Toy cheapestToy = toys [0]; System.out.println("\nThe educational toys are: "); // loop through the toys array for (i = 0; i<n; i++) { // if the current toy is cheaper than the cheapest toy if(toys [i].getPrice () < cheapest) { // reset the chepest and cheapest Toy to the current toy cheapest = toys[i].getPrice(); cheapestToy = toys[i]; } // if the current toy is an educational toy if(toys [i] instanceof EducationalToy) { =");
}
}
// print the toy
System.out.println(toys[i]);
// increment the count by 1
count++;
System.out.println("The count of educational toys are:
System.out.println("=======
// display the cheapest toy
System.out.println("\nThe cheapest toy is: ");
System.out.println(cheapestToy);
+ count);
System.out.println("==
System.out.println("\nToys after applying 10% discount:");
// loop through the toys array
for (i = 0; i<n; i++) {
// apply 10% discount on each toy
// applying 10% means that the 90% percent of the price is preserved
toys[i].setPrice (toys[i].getPrice () 0.90);
// display each toy
System.out.println(toys[i]);
");
Transcribed Image Text:} } // print the toy System.out.println(toys[i]); // increment the count by 1 count++; System.out.println("The count of educational toys are: System.out.println("======= // display the cheapest toy System.out.println("\nThe cheapest toy is: "); System.out.println(cheapestToy); + count); System.out.println("== System.out.println("\nToys after applying 10% discount:"); // loop through the toys array for (i = 0; i<n; i++) { // apply 10% discount on each toy // applying 10% means that the 90% percent of the price is preserved toys[i].setPrice (toys[i].getPrice () 0.90); // display each toy System.out.println(toys[i]); ");
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Basics of loop
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education