/Chapter 9, Debugging Exercise; Java Programming; Joyce Farraell, 8th ed 1 impor
ID: 3665898 • Letter: #
Question
/Chapter 9, Debugging Exercise; Java Programming; Joyce Farraell, 8th ed
1 import javax.swing.*;
2 public class DebugNine2
3 {
4 public static void main(String[] args)
5 {
6 Movie[] movies = new Movie[8];
7 int i;
8 String message, entry;
9 Movies[0] = new Movie("The Godfather", 1972);
10 movies[1] = new Movie("The Good, the Bad, and the Ugly", 1966);
11 movies[2] = new Movie("Pulp Fiction", 1994);
12 Movie[3] = new Movie("Shindler's List", 1993);
13 Movie[4] = new Movie("Casablanca", 1942);
14 movies[5] = new Movie("Wizard of Oz", 1939);
15 movies[6] = new Movie("Citizen Kane", 1941);
16 moVies[7] = new Movie("Some Like It Hot", 1959);
17 entry = JOptionPane.showInputDialog(null,
18 "Sort Movies by (N)ame, or (Y)ear");
19 if(entry == 'N')
20 {
21 nameSort(Movie);
22 message = "Sorted by Name ";
23 }
24 else
25 {
26 yearSort(year);
27 message = "Sorted by Year ";
28 }
29 display(movie, message);
30 }
31 public static void nameSort(Movie[] array)
32 {
33 int a, b;
34 int highSub = array.length - 1;
35 for(a = 0; a < highSub; ++a)
36 {
37 for(b = 0; b < highSub; ++b)
38 {
39 String first = array[b].getName();
40 String second = array[b + 1].getName();
41 if(first.compareTo(second) > 0)
42 {
43 temp = array[b];
44 array[b] = array[b + 1];
45 array[b + 1] = temp;
46 }
47 }
48 }
49 }
50 public static void yearSort(Movie[] array)
51 {
52 int a, b;
53 Movie temp;
54 int highSub = array.length;
55 for (a = 0; a < highSub; ++a)
56 {
57 for (b = 0; b < highSub; ++b)
58 if (array[b].getYear() > array[b + 1].getYear())
59 {
60 temp = array[b];
61 array[b] = array[b + 1];
62 array[b + 1] = temp;
63 }
64 }
65 }
66 public static void display(Movie s, String msg)
67 {
68 for (int i = 0; i < len; i++)
69 msg = msg + s[i].getName() + ", " + s[i].getYear() + " ";
70 JOptionPane.showMessageDialog(null, msg);
71 }
72 }
Explanation / Answer
There is a problem with the declaration and the value used. Java is case sensitive so only movies[0],movies[1] can be used. I have modified the above code:
1 import javax.swing.*;
2 public class DebugNine2
3 {
4 public static void main(String[] args)
5 {
6 Movie[] movies = new Movie[8];
7 int i;
8 String message, entry;
9 movies[0] = new Movie("The Godfather", 1972);
10 movies[1] = new Movie("The Good, the Bad, and the Ugly", 1966);
11 movies[2] = new Movie("Pulp Fiction", 1994);
12 movies[3] = new Movie("Shindler's List", 1993);
13 movies[4] = new Movie("Casablanca", 1942);
14 movies[5] = new Movie("Wizard of Oz", 1939);
15 movies[6] = new Movie("Citizen Kane", 1941);
16 movies[7] = new Movie("Some Like It Hot", 1959);
17 entry = JOptionPane.showInputDialog(null,
18 "Sort Movies by (N)ame, or (Y)ear");
19 if(entry == 'N')
20 {
21 nameSort(Movie);
22 message = "Sorted by Name ";
23 }
24 else
25 {
26 yearSort(year);
27 message = "Sorted by Year ";
28 }
29 display(movie, message);
30 }
31 public static void nameSort(Movie[] array)
32 {
33 int a, b;
34 int highSub = array.length - 1;
35 for(a = 0; a < highSub; ++a)
36 {
37 for(b = 0; b < highSub; ++b)
38 {
39 String first = array[b].getName();
40 String second = array[b + 1].getName();
41 if(first.compareTo(second) > 0)
42 {
43 temp = array[b];
44 array[b] = array[b + 1];
45 array[b + 1] = temp;
46 }
47 }
48 }
49 }
50 public static void yearSort(Movie[] array)
51 {
52 int a, b;
53 Movie temp;
54 int highSub = array.length;
55 for (a = 0; a < highSub; ++a)
56 {
57 for (b = 0; b < highSub; ++b)
58 if (array[b].getYear() > array[b + 1].getYear())
59 {
60 temp = array[b];
61 array[b] = array[b + 1];
62 array[b + 1] = temp;
63 }
64 }
65 }
66 public static void display(Movie s, String msg)
67 {
68 for (int i = 0; i < len; i++)
69 msg = msg + s[i].getName() + ", " + s[i].getYear() + " ";
70 JOptionPane.showMessageDialog(null, msg);
71 }
72 }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.