Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Looking for some help finshing up a java program. I have one more requirment to

ID: 3832327 • Letter: L

Question

Looking for some help finshing up a java program.

I have one more requirment to meet and it is to make a linked list (Use a Linked List (that you build) into your program). Not really sure on the best way to do this for this program so any help is apperiacte. I have most of the code ready to go.

Thanks for the help

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>();
try {
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);
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(games.toString().replace("[", "").replace("]", "".replace("(", "").replace(")", "")));
Sorting.insertionSortTitle(games);
System.out.println(" After insertion sort: " + games);

// recruive search
gameSearch si = new gameSearch();
String searchstring;
Scanner scan1 = new Scanner(System.in);
System.out.println(" ENTER KEY STRING TO SEARCH:");

searchstring = scan1.nextLine();

System.out.println("NUMBER OF ELEMENTS IN THE GIVEN LIST:" + games.size());
System.out.println("NUMBER OF COMPARISONS REQUIRED:" + si.recStringSearch(games, searchstring, 0, games.size()));

}
}

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

import java.util.ArrayList;

public class Sorting {

public static void selectionSort(Comparable[] list) {

int min;
Comparable temp;
try{
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;
}} catch (NumberFormatException e) {
           e.printStackTrace();
       }
}

   public static void selectionSortValue(ArrayList games) {
      
       int max, i ,j;
       Game temp;
           try{
       for (i = 0; i < games.size()-1; i++)
       {
       max = i;
       for (j = i+1; j < games.size(); j++)
       {
       if (games.get(max).getValue().compareTo(games.get(j).getValue()) > 0)
       max = j;
       }
       temp = games.get(i);
       games.set(i, games.get(max));
       games.set(max, temp);
}} catch (NumberFormatException e) {
           e.printStackTrace();
       }      
   }
  
  


   public static void insertionSortTitle(ArrayList games) {
       int max, i ,j;
       Game temp;
      
           try{
       for (i = 0; i < games.size()-1; i++)
       {
       max = i;
       for (j = i+1; j < games.size(); j++)
       {
       if (games.get(max).compareTo(games.get(j)) > 0)
       max = j;
       }
       temp = games.get(i);
       games.set(i, games.get(max));
       games.set(max, temp);
}} catch (NumberFormatException e) {
           e.printStackTrace();
       }          
   }
  

}

gamesearch.java

import java.util.ArrayList;

//CLASS THAT IMPLEMENTS RECURSIVE SEARCH
public class gameSearch {
// BINARY RECURSIVE SEARCH TO FIND GIVEN STRING
public int recStringSearch(ArrayList<Game> games, String searchstring, int s, int l) {
if (s > l)
return 0;
else {
int t = (l + s) / 2;
int tp = searchstring.compareTo(games.get(t).getTitle());
  
// IF ELEMENTS EQUAL PRINT ELEMNT FOUND
if (tp == 0) {
System.out.println("STRING FOUND");
return 1;
}
// IF STRING IS NOT EQUAL THEN FIND WHICH HALF OF THE LIST THE KEY
// STRING IS PRESENT ANDMAKE RECURSIVE CALL TO SEARCH UNTILL KEY
// STRING FOUND OR REACHED END OF THE LIST
else if (tp < 0) {
return 1 + recStringSearch(games, searchstring, t + 1, l);
} else
return 1 + recStringSearch(games, searchstring, s, t - 1);
}
}
}

game.txt

PC 2000 7 Sims EA
Multiplatform 2015 7 Titanfall EA
Xbox 2001 9 Halo Microsoft
Switch 2017 10 Zelda Nintendo
PC 2013 9 DOTA Valve
PC 1998 9 Starcraft Blizzard
PC 2011 9 Minecraft Mojang
PS4 2016 9 Uncharted Sony
PS4 2017 8 Horizon Sony
Xbox 2016 6 Recore Mircosoft
Multiplatform 2008 8 Battlefield EA

Explanation / Answer

NOte: use this linked list in your program instead of arraylist.

/*
* Java Program to Implement Singly Linked List
*/


/* Class linkedList */
public class linkedList<T>
{
  
   /* Class Node */
   class Node<T>
   {
   protected T data;
   protected Node link;
     
   /* Constructor */
   public Node()
   {
   link = null;
   data = null;
   }
   /* Constructor */
   public Node(T d,Node n)
   {
   data = d;
   link = n;
   }
   /* Function to set link to next Node */
   public void setLink(Node n)
   {
   link = n;
   }
   /* Function to set data to current Node */
   public void setData(T d)
   {
   data = d;
   }
   /* Function to get link to next node */
   public Node getLink()
   {
   return link;
   }
   /* Function to get data from current Node */
   public T getData()
   {
   return data;
   }
   }
  
protected Node start;
protected Node end ;
public int size ;

/* Constructor */
public linkedList()
{
start = null;
end = null;
size = 0;
}
/* Function to check if list is empty */
public boolean isEmpty()
{
return start == null;
}
/* Function to get size of list */
public int getSize()
{
return size;
}
/* Function to insert an element at begining */
public void insertAtStart(T val)
{
Node nptr = new Node(val, null);
size++ ;
if(start == null)
{
start = nptr;
end = start;
}
else
{
nptr.setLink(start);
start = nptr;
}
}
/* Function to insert an element at end */
public void insertAtEnd(T val)
{
Node nptr = new Node(val,null);
size++ ;
if(start == null)
{
start = nptr;
end = start;
}
else
{
end.setLink(nptr);
end = nptr;
}
}
/* Function to insert an element at position */
public void insertAtPos(T val , int pos)
{
Node nptr = new Node(val, null);
Node ptr = start;
pos = pos - 1 ;
for (int i = 1; i < size; i++)
{
if (i == pos)
{
Node tmp = ptr.getLink() ;
ptr.setLink(nptr);
nptr.setLink(tmp);
break;
}
ptr = ptr.getLink();
}
size++ ;
}
/* Function to delete an element at position */
public void deleteAtPos(int pos)
{
if (pos == 1)
{
start = start.getLink();
size--;
return ;
}
if (pos == size)
{
Node s = start;
Node t = start;
while (s != end)
{
t = s;
s = s.getLink();
}
end = t;
end.setLink(null);
size --;
return;
}
Node ptr = start;
pos = pos - 1 ;
for (int i = 1; i < size - 1; i++)
{
if (i == pos)
{
Node tmp = ptr.getLink();
tmp = tmp.getLink();
ptr.setLink(tmp);
break;
}
ptr = ptr.getLink();
}
size-- ;
}
/* Function to display elements */
public void display()
{
System.out.print(" Singly Linked List = ");
if (size == 0)
{
System.out.print("empty ");
return;
}
if (start.getLink() == null)
{
System.out.println(start.getData() );
return;
}
Node ptr = start;
System.out.print(start.getData()+ "->");
ptr = start.getLink();
while (ptr.getLink() != null)
{
System.out.print(ptr.getData()+ "->");
ptr = ptr.getLink();
}
System.out.print(ptr.getData()+ " ");
}
  
  
public static void main(String args[])
{
   linkedList<String> list=new linkedList<String>();
  
   list.insertAtEnd("hello");
   list.insertAtEnd("world");
   list.display();
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote