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

Need some java program help. Needing some extra help implenting a GUI for my pro

ID: 3832460 • Letter: N

Question

Need some java program help.

Needing some extra help implenting a GUI for my program. This program is a game sorting program and what I would like is a GUI that has buttons that when clicked do my selction sorts Title and Value and then prints the choosen choice on screen.

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

Added new File MyView.java and modified the files Main.java and Sorting.java(fixed a bug)

MyView.java:

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;


public class MyView extends javax.swing.JFrame {

/**
* Creates new form MyView
*/
  
Main main = null;
public MyView() {
initComponents();
}

  
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

sortBtn = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
searchField = new javax.swing.JTextField();
searchBtn = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
outputArea = new javax.swing.JTextArea();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

sortBtn.setText("Do Sorting");
sortBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sortBtnActionPerformed(evt);
}
});

jLabel1.setText("ENTER KEY STRING TO SEARCH:");

searchBtn.setText("Search");
searchBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
searchBtnActionPerformed(evt);
}
});

outputArea.setEditable(false);
outputArea.setColumns(20);
outputArea.setRows(5);
jScrollPane1.setViewportView(outputArea);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(24, 24, 24)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(searchField, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(searchBtn))
.addComponent(sortBtn))
.addContainerGap(69, Short.MAX_VALUE))
.addComponent(jScrollPane1)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(sortBtn)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(searchField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(searchBtn))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void sortBtnActionPerformed(java.awt.event.ActionEvent evt) {
  
outputArea.setText("");
try {
main = new Main(outputArea);
} catch (FileNotFoundException ex) {
Logger.getLogger(MyView.class.getName()).log(Level.SEVERE, null, ex);
}
}   

private void searchBtnActionPerformed(java.awt.event.ActionEvent evt) {

String searchstring = searchField.getText();
if(main == null ||searchstring==null || searchstring.equalsIgnoreCase("")){
return ;
}
ArrayList<Game> games = main.getGames();
gameSearch si = new gameSearch();

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

}   

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
  
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MyView().setVisible(true);
}
});
}

// Variables declaration - do not modify   
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea outputArea;
private javax.swing.JButton searchBtn;
private javax.swing.JTextField searchField;
private javax.swing.JButton sortBtn;
// End of variables declaration   
}

Main.java :

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import javax.swing.JTextArea;
public class Main {

ArrayList<Game> games;
JTextArea outputArea;
public Main() throws FileNotFoundException{
init();
}
public Main(JTextArea outputArea) throws FileNotFoundException{
this.outputArea = outputArea;
init();
}
public static void main(String args[]) throws FileNotFoundException {
new Main();
}

private void init() throws FileNotFoundException{

Random rand = new Random();
Scanner scan = new Scanner(new File("game.txt"));
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();
}
outputArea.append(games.toString().replace("[", "").replace("]", "".replace("(", "").replace(")", "")));
System.out.println(games.toString().replace("[", "").replace("]", "".replace("(", "").replace(")", "")));
Sorting.insertionSortTitle(games);
outputArea.append(" After insertion sort: " + games);
System.out.println(" After insertion sort: " + games);
// recruive search
}

public ArrayList<Game> getGames(){
return games;
}
}

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<Game> 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();
}
}
  
  

public static void insertionSortTitle(ArrayList<Game> 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();
}
}
  
}

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