Have a java problem that is giving me some trouble. I need to add recursion to t
ID: 3832179 • Letter: H
Question
Have a java problem that is giving me some trouble.
I need to add recursion to this program and have added a recurisive search but cannot get it to work and find the word.
I am not 100% sure on how to get it to work properly I think it has to do with the arraylist as line 70 will not work correct.
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 > ();
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(games);
//recruive search
gameSearch si;
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.*;
import java.io.*;
import java.lang.*;
//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 = games[t].compareTo(searchstring);
//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
program:
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(games);
//recruive search
gameSearch si;
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.*;
import java.io.*;
import java.lang.*;
//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 = games[t].compareTo(searchstring);
//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);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.