List at least 6 suggestions for improving this class. Highlight the code using a box and the draw an arrow from that box to write your comments in the right column (example shown).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 25 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; /** * Maintains a bike inventory. Each bike in the inventory * is identified using a unique Stock Number. The bike inventory * provides three functionalities. 1) Adding a new bike to the * inventory 2) Removing a bike from the inventory using the unique * stock number */ public class BikeInventory { private ArrayList<Bike> list_of_bikes; public boolean addBike(int stockNumber, String bikeMake, String bikeModel,int bikeYear, int bikeMileage, float bikePrice) { Bike newEntry; try{ newEntry = new Bike(stockNumber,bikeMake, bikeModel,bikeYear, bikeMileage,bikePrice); }catch(InvalidBikeException ice){
} this.list_of_bikes.add(newEntry); return true; } public boolean removeBike(int stockNumber) { Iterator<Bike> itr = list_of_bikes.iterator(); while (itr.hasNext()) { if (itr.next().stockNumber == stockNumber) itr.remove(); } return true; } private class Bike { public int stockNumber; // stock number of the bike, unique public String bikeMake; // Make of the bike e.g. Toyota public String bikeModel; // Model of the bike, e.g. Camry public int bikeYear; // Year of the bike public int bikeMileage; // bike mileage public float bikePrice; // listed price
public Bike(int stockNumber, String bikeMake, String bikeModel,int bikeYear, int bikeMileage, float bikePrice) throws InvalidBikeException{
if(stockNumber<0) return InvalidBikeException(“Invalid inventory!”); newEntry.stockNumber = stockNumber; newEntry.bikeMake = bikeMake; newEntry.bikeModel = bikeModel; newEntry.bikeYear = bikeYear; newEntry.bikeMileage = bikeMileage; newEntry.bikePrice = bikePrice;
} @Override public String toString() { return ("Stock#" + stockNumber + ": " + bikeYear + " " + bikeMake + " " + bikeModel + " \n Mileage:" + bikeMileage + "\n Price: " + bikePrice + "\n\n"); } } } |
|
I HAVE ADDED THE 6 NEW SUGGESTION AS COMMENTS IN UPPER CASE
import java.util.ArrayList; // THIS IS NOT NEEDED AS LONG AS YOU IMPORT JAVA.UTIL.*
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
/**
* Maintains a bike inventory. Each bike in the inventory
* is identified using a unique Stock Number. The bike inventory
* provides three functionalities. 1) Adding a new bike to the
* inventory 2) Removing a bike from the inventory using the unique
* stock number
*/
public class BikeInventory {
private ArrayList<Bike> list_of_bikes; //CHANGE IT TO PUBLIC AS IT WILL NOT BE ACCESSIBLE BY main
public boolean addBike(int stockNumber, String bikeMake,
String bikeModel,int bikeYear, int bikeMileage, float bikePrice) {
Bike newEntry;
try{
newEntry = new Bike(stockNumber,bikeMake,
bikeModel,bikeYear, bikeMileage,bikePrice);
}catch(InvalidBikeException ice){
}
this.list_of_bikes.add(newEntry);
return true;
}
public boolean removeBike(int stockNumber) {
Iterator<Bike> itr = list_of_bikes.iterator();
while (itr.hasNext()) {
if (itr.next().stockNumber == stockNumber)
itr.remove();
}
return true;
}
private class Bike { //CAN BE WRITTEN A PUBLIC AND A SEPERATE CLASS OF ITS OWN
public int stockNumber; // stock number of the bike, unique // PRIVATE AS INCLUDUNG ABSTRACTION WHEN BIKE IS A SEPERATE CLASS
public String bikeMake; // Make of the bike e.g. Toyota
public String bikeModel; // Model of the bike, e.g. Camry
public int bikeYear; // Year of the bike
public int bikeMileage; // bike mileage
public float bikePrice; // listed price
public Bike(int stockNumber, String bikeMake,
String bikeModel,int bikeYear, int bikeMileage,
float bikePrice) throws InvalidBikeException{
if(stockNumber<0)
return InvalidBikeException(“Invalid inventory!”); // THROWING EXCEPTION INSIDE CONSTRUCTION IS A BAD PRACTICE .ONE CAN A NEW FUNCTION FOR IT
newEntry.stockNumber = stockNumber;
newEntry.bikeMake = bikeMake;
newEntry.bikeModel = bikeModel;
newEntry.bikeYear = bikeYear;
newEntry.bikeMileage = bikeMileage; // USE (this.) INSTEAD OF newEntry .IT WILL MAKE THE CODE LESS COMPLEX
newEntry.bikePrice = bikePrice;
}
@Override
public String toString() {
return ("Stock#" + stockNumber + ": " + bikeYear + "
" + bikeMake + " " + bikeModel + " \n Mileage:" +
bikeMileage + "\n Price: " + bikePrice + "\n\n");
}
}
}
List at least 6 suggestions for improving this class. Highlight the code using a box and...
I need some help with some homework questions. How would I implement the to-do's? ----------------------------------------------------------------------------------------------------------------------------------------------------------- AbstractArrayHeap.Java package structures; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.NoSuchElementException; public abstract class AbstractArrayHeap<P, V> { protected final ArrayList<Entry<P, V>> heap; protected final Comparator<P> comparator; protected AbstractArrayHeap(Comparator<P> comparator) { if (comparator == null) { throw new NullPointerException(); } this.comparator = comparator; heap = new ArrayList<Entry<P, V>>(); } public final AbstractArrayHeap<P, V> add(P priority, V value) { if (priority == null || value...
FOR JAVA: Summary: Create a program that adds students to the class list (see below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...
CONVERT THIS JAVA CODE WITH THE JAVA DOC GUIDELINES WHERE APPROPRIATE. DO NOT CHANGE THE CODE. SAME CODE SHOULD BE USED FOR THE DOCUMENTATION PURPOSES. ONLY INSERT THE JAVA DOC. //Vehicle.java public class Vehicle { private String manufacturer; private int seat; private String drivetrain; private float enginesize; private float weight; //create getters for the attributes public String getmanufacturer() { return manufacturer; } public String getdrivetrain() { return drivetrain;...
ANNOTATE BRIEFLY LINE BY LINE THE FOLLOWING CODE: package shop.data; import junit.framework.Assert; import junit.framework.TestCase; public class DataTEST extends TestCase { public DataTEST(String name) { super(name); } public void testConstructorAndAttributes() { String title1 = "XX"; String director1 = "XY"; String title2 = " XX "; String director2 = " XY "; int year = 2002; Video v1 = Data.newVideo(title1, year, director1); Assert.assertSame(title1, v1.title()); Assert.assertEquals(year, v1.year()); Assert.assertSame(director1, v1.director()); Video v2 = Data.newVideo(title2, year, director2); Assert.assertEquals(title1, v2.title()); Assert.assertEquals(director1, v2.director()); } public void testConstructorExceptionYear()...
JAVA Modify the code to create a class called box, wherein you find the area. I have the code in rectangle and needs to change it to box. Here is my code. Rectangle.java public class Rectangle { private int length; private int width; public void setRectangle(int length, int width) { this.length = length; this.width = width; } public int area() { return this.length * this.width; } } // CONSOLE / MAIN import java.awt.Rectangle; import java.util.Scanner; public class Console...
import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; public class FindWordInMaze { private char grid[][]; private ArrayList<String> words; private HashSet<String> foundWords = new HashSet<String>(); public FindWordInMaze(char[][] grid) { this.grid = grid; this.words = new ArrayList<>(); // add dictionary words words.add("START"); words.add("NOTE"); words.add("SAND"); words.add("STONED"); Collections.sort(words); } public void findWords() { for(int i=0; i<grid.length; i++) { for(int j=0; j<grid[i].length; j++) { findWordsFromLocation(i, j); } } for(String w: foundWords) { System.out.println(w); } } private boolean isValidIndex(int i, int j, boolean visited[][]) { return...
a) Create a class MinHeap implementation using an Array. Find the kth smallest value in a collection of n values, where 0 < k < n. Write a program that uses a minheap method to find the kth smallest value in a collection of n values. Use the MinHeap class defined in part a. public final class MinHeap<T extends Comparable<? super T>> implements MinHeapInterface<T> { private T[] heap; // Array of heap entries; ignore heap[0] private int...
Writing 3 Java Classes for Student registration /** * A class which maintains basic information about an academic course. */ public class Course { /** * Attributes. */ private String code; private String title; private String dept; // name of department offering the course private int credits; /** * Constructor. */ public Course(String code, String title, int credits) { // TODO : initialize instance variables, use the static method defined in // Registrar to initialize the dept name variable...
Need help debugging. first class seems fine. second class is shooting an error on s = super.getString(prompt); third class is giving me an error in the while loop where int num = console.getInt("Enter an integer:"); //-------------------------------------------------------------------------- import java.util.Scanner; public class Console { private Scanner sc; boolean isValid; int i; double d; public Console() { sc = new Scanner(System.in); } public String getString(String prompt) { System.out.print(prompt); return sc.nextLine();...
Analyze the code to determine what the code does, how the data is structured, and why it is the appropriate data structure (i.e. linked list, stack or queue). Note that these are examples of classes and methods and do not include the "main" or "test" code needed to execute. When you are done with your analysis, add a header describing the purpose of the program (include a description of each class and method), the results of your analysis, and add...