Looking for some Java help with a compbrable sorting featureing I am trying to g
ID: 3832126 • Letter: L
Question
Looking for some Java help with a compbrable sorting featureing I am trying to get working.
I would like the program to sort the games by the system they are on but I am having trouble getting it to work.
Thanks for the help
Here is my code.
main.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class main {
public static void main(String args[]) throws FileNotFoundException {
Random rand = new Random();
Scanner scan = new Scanner(new File("game.txt"));
ArrayList < Game > games = new ArrayList < Game > ();
//used a switch statement for the scan as it seemed easier for this question error checking can be found at end of the statement
while (scan.hasNext()) {
Game f = null;
String name = scan.next();
switch (name) {
case "Xbox":
f = new Xbox();
break;
case "PS4":
f = new PS4();
break;
case "Switch":
f = new Switch();
break;
case "PC":
f = new PC();
break;
case "Multiplatform":
f = new Multiplatform();
break;
default:
System.out.println("No file loaded ");
break;
}
f.name = name;
f.year = scan.nextInt();
f.rating = scan.nextInt();
f.title =scan.next();
f.publisher = scan.next();
games.add(f);
}
System.out.println(games);
}
}
Game.java
abstract public class Game implements Comparable {
public String name;
int year;
int rating;
String title;
String publisher;
abstract void game(Game f);
public String toString() {
return String.format("(%s,%s,%d,%d,%s)", title ,publisher , year, rating, name);
}
public String getTitle() {
return title;
}
public int compareTo(Object otherObject) {
Game otherGame = (Game) otherObject;
return getTitle().compareTo(otherGame.getTitle());
}
}
xbox.java
public class Xbox extends Game {
@Override
void game(Game f) {
// TODO Auto-generated method stub
System.out.println(this.title+" " + this.publisher + " " + this.year + " " + this.rating + " " + this.name);
}
}
PS4.java
public class PS4 extends Game {
@Override
void game(Game f) {
// TODO Auto-generated method stub
System.out.println(this.title+" " + this.publisher + " " + this.year + " " + this.rating + " " + this.name);
}
}
Switch.java
public class Switch extends Game {
@Override
void game(Game f) {
// TODO Auto-generated method stub
System.out.println(this.title+" " + this.publisher + " " + this.year + " " + this.rating + " " + this.name);
}
}
Mulitplatform
public class Multiplatform extends Game {
@Override
void game(Game f) {
// TODO Auto-generated method stub
System.out.println(this.title+" " + this.publisher + " " + this.year + " " + this.rating + " " + this.name);
}
}
PC.java
public class PC extends Game {
@Override
void game(Game f) {
// TODO Auto-generated method stub
System.out.println(this.title+" " + this.publisher + " " + this.year + " " + this.rating + " " + this.name);
}
}
Sorting.java
public class Sorting {
public static void selectionSort(Comparable[] list) {
int min;
Comparable temp;
for (int index = 0; index < list.length - 1; index++) {
min = index;
for (int scan = index + 1; scan < list.length; scan++)
if (list[scan].compareTo(list[min]) < 0)
min = scan;
temp = list[min];
list[min] = list[index];
list[index] = temp;
}
}
}
Explanation / Answer
Scanner scan = new Scanner(new File("D://game.txt"));
You didn't give the path of the text file. I have done using the below data in a text file
Xbox
2010
5
PSA
publish
PS4
2015
4
asd
pbs
And the output generated is [(PSA,publish,2010,5,Xbox), (asd,pbs,2015,4,PS4)]
You didn't mention on what basis the games to be sorted. As you are taking it from a text file, there will be a different appraoch for that other than mentioned in Sorting class. Please specify the exact condition and output you want.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.