#Failed to do this in Lab Exam today
1 messages · Page 1 of 1 (latest)
not really, I mean I know the logic to delete duplicates
but the initial ask is the problem
how do i make those classes and instances
I will send the duplicate logic deletion code if that helps
package lab2;
import java.util.Arrays;
import java.util.Scanner;
public class DealershipRecords {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int numDealers = scanner.nextInt();
scanner.nextLine();
int[] inventorySizes = new int[numDealers];
for (int i = 0; i < numDealers; i++) {
inventorySizes[i] = scanner.nextInt();
scanner.nextLine();
}
int totalInventories = Arrays.stream(inventorySizes).sum();
double[] allInventories = new double[totalInventories];
int currentIndex = 0;
for (int i = 0; i < numDealers; i++) {
for (int j = 0; j < inventorySizes[i]; j++) {
allInventories[currentIndex++] = scanner.nextDouble();
}
}
Arrays.sort(allInventories);
for (int i = 0; i < totalInventories; i++) {
if (i == 0 || allInventories[i] != allInventories[i - 1]) {
System.out.print(allInventories[i] + " ");
}
}
}
}
this is "completely wrong" my TA said
Detected code, here are some useful tools:
Formatted code
package lab2;
import java.util.Arrays;
import java.util.Scanner;
public class DealershipRecords {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int numDealers = scanner.nextInt();
scanner.nextLine();
int [] inventorySizes = new int [numDealers] ;
for (int i = 0; i < numDealers; i++) {
inventorySizes[i] = scanner.nextInt();
scanner.nextLine();
}
int totalInventories = Arrays.stream(inventorySizes).sum();
double [] allInventories = new double [totalInventories] ;
int currentIndex = 0;
for (int i = 0; i < numDealers; i++) {
for (int j = 0; j < inventorySizes[i] ; j++) {
allInventories[currentIndex++] = scanner.nextDouble();
}
}
Arrays.sort(allInventories);
for (int i = 0; i < totalInventories; i++) {
if (i == 0 || allInventories[i] != allInventories[i - 1] ) {
System.out.print(allInventories[i] + " ");
}
}
}
}
he said use class and object principles