I am working in Eclipse and this is java I need help with an array in my GUI. I
ID: 3807809 • Letter: I
Question
I am working in Eclipse and this is java I need help with an array in my GUI.
I am looking to convert this normal array I have into a 2d array.
Any ideas? below is my program
Moives.java
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class Moives extends JFrame
{
private static Game movies[];
private static JButton sort = new JButton("Perform Sorting");
private static JTextArea textarea = new JTextArea(20, 35);
public Moives(){
sort.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Sorting.selectionSort(movies);
//Displaying result in textarea
display();
}
});
setLayout(new FlowLayout());
add(new JScrollPane(textarea));
add(sort);
}
public static void setFrame(final JFrame frame, final int width, final int height) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
frame.setTitle(frame.getClass().getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width, height);
frame.setVisible(true);
}
});
}
public static void display()
{
String ans="";
for(Game Game:movies)
ans+=Game+" ";
textarea.setText(ans);
}
public static void main(String[] args){
//Game[] movies=new Game[7];
movies=new Game[7];
movies[0]= new Game ("The Godfather", "Francis Ford Coppola", 1972, 24.95, true);
movies[1]= new Game ("District 9", "Neill Blokamp", 2009, 19.95, false);
movies[2]= new Game ("Iron Man", "Jon Favreau", 2008, 15.95, false);
movies[3]= new Game ("All About Eve", "Joseph Mankiewicz", 1950, 17.50, false);
movies[4]= new Game ("The Matrix", "Andy & Lana Wachowski", 1999, 19.95, true);
movies[5]= new Game ("Iron Man 2", "Jon Favreau", 2010, 22.99, false);
movies[6]= new Game ("Casablanca", "Michael Curtiz", 1942, 19.95, false);
setFrame(new Moives(), 500, 500);
display();
}
}
DVD.java
import java.text.NumberFormat;
public class DVD implements Comparable{
private String tile, director;
private int year;
private double cost;
private boolean bluRay;
private String title;
public DVD (String title, String director, int year, double cost, boolean BluRay){
this.title=title;
this.director=director;
this.cost=cost;
this.bluRay=BluRay;
}
public String getTitle(){
return title;
}
public String toString(){
NumberFormat fmt = NumberFormat.getCurrencyInstance();
String description;
description = fmt.format(cost) + " " + year + " ";
description += title + " " + director;
if (bluRay)
description += " " + "Blu-Ray";
return description;
}
public int compareTo(Object otherObject){
DVD otherDvd=(DVD)otherObject;
return getTitle().compareTo(otherDvd.getTitle());
}
}
Sorting.java
public class Sorting {
public static void selectionSort(Comparable[] list){
int min;
Comparable temp;
for(int index=0; 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
Hi, I have converted your main function code with using 2-D array.
Please let me know in case of any issue.
public static void main(String[] args) {
DVD[][] movies=new DVD[4][7]; // row = 4, column = 7
for(int i=0; i<movies.length; i++){ // row interation
movies[i][0]= new DVD ("The Godfather", "Francis Ford Coppola", 1972, 24.95, true);
movies[i][1]= new DVD ("District 9", "Neill Blokamp", 2009, 19.95, false);
movies[i][2]= new DVD ("Iron Man", "Jon Favreau", 2008, 15.95, false);
movies[i][3]= new DVD ("All About Eve", "Joseph Mankiewicz", 1950, 17.50, false);
movies[i][4]= new DVD ("The Matrix", "Andy & Lana Wachowski", 1999, 19.95, true);
movies[i][5]= new DVD ("Iron Man 2", "Jon Favreau", 2010, 22.99, false);
movies[i][6]= new DVD ("Casablanca", "Michael Curtiz", 1942, 19.95, false);
}
for(int i=0; i<movies.length; i++){ // sorting each row
Sorting.selectionSort(movies[i]);
}
for(DVD dvds[] : movies)
for(dvd : dvds)
System.out.println(dvd);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.