Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Keep Laptop class unchanged Do folowing changes in LaptopList class 1. Remove public int countExpensive() public Laptop[] expensiveLaptops() 2. Make public void printList() public void

Keep Laptop class unchanged

Do folowing changes in LaptopList class

1. Remove

public int countExpensive()

public Laptop[] expensiveLaptops()

2. Make

public void printList()

public void printListReverse()

public Laptop mostExpensive()

recursive.

Redesign main method in TestLaptopList class to:

1. Instantiate an object from LaptopList class

2. Invoke all of the two methods from LaptopList methods mentioned before

-------The Code-----

File: Laptop.java

public class Laptop { // instance variables private int size; private String brand; private double price;

/** * Constructor for objects of class Laptop */ public Laptop() { // initialise instance variables size = 0; brand=""; price=0.0; } /** * Constructor for objects of class Laptop */ public Laptop(String brand, int size, double price) { // initialise instance variables this.size = size; this.brand = brand; this.price = price; } //Getter method for brand public String getBrand() { return this.brand; }

//Getter method for size public int getSize() { return this.size; }

//Getter method for price public double getPrice() { return this.price; }

// returns true if price is greater than $2,000.00 public boolean isExensive() { //Checking price if(this.price > 2000) { return true; } return false; } }

File: LaptopList.java

import java.io.*; import java.util.*;

public class LaptopList { // instance variable private ArrayList laptops;

/** * Constructor for objects of class LaptopList */ public LaptopList() throws IOException { String brand; int size; double price; laptops = new ArrayList(); //Opening input file for reading Scanner reader = new Scanner(new File("d:\\Java\\inData.txt")); //Reading data while(reader.hasNext()) { price = reader.nextDouble(); brand = reader.next(); size = reader.nextInt(); //Creating Laptop object Laptop temp = new Laptop(brand, size, price); //Adding to array list laptops.add(temp); } } // prints brand followed by price public void printList() { System.out.println(" Laptop brands and their prices: "); //Iterating over laptops for(int i=0; i mostExp.getPrice()) { //Updating most expensive mostExp = laptops.get(i); } } return mostExp; } // returns count of expensive laptops public int countExpensive() { int cnt=0; //Iterating over laptops for(int i=0; i 2000) { //Incrementing count cnt = cnt + 1; } } return cnt; } // returns an array of expensive laptops public Laptop[] expensiveLaptops() { //Creating an array Laptop[] expLaptops = new Laptop[countExpensive()]; int j=0; //Iterating over laptops for(int i=0; i 2000) { //Storing in array expLaptops[j] = laptops.get(i); j++; } } return expLaptops; } }

File: TestLaptopList.java

import java.io.*;

public class TestLaptopList { //Main method public static void main(String[] args) throws IOException { //LaptopList object LaptopList laptopsLst = new LaptopList(); //Calling methods laptopsLst.printList(); Laptop mostExpen = laptopsLst.mostExpensive(); System.out.println("Most Expensive Laptop is: Brand: " + mostExpen.getBrand() + ", Size: " + mostExpen.getSize() + ", Price: $" + mostExpen.getPrice()); //Array of expensive laptops Laptop[] expensiveArr = laptopsLst.expensiveLaptops(); System.out.println(" Most Expensive Laptops: "); for(int i=0; i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M Kroenke, David J Auer

6th Edition

0132742926, 978-0132742924

More Books

Students also viewed these Databases questions