Access over 35 million academic & study documents

6 (10 pts) What is wrong with the following code How should it be

Content type
User Generated
Rating
Showing Page:
1/4
6. (10 pts) What is wrong with the following code? How
should it be fixed?
1 public class H2ClassG {
2 final int x;
3
4 H2ClassG () {}
5 H2ClassG (int a) {x = a;}
6 } // end class H2ClassG
7. (10 pts) What is wrong with the following code? How
should it be fixed?
1 public class H2ClassH {
2 final int x;
3
4 int H2ClassH () {
5 if (x == 7) return 1;
6 return 2;
7 } // end
8 } // end class H2ClassH
8. (10 pts) What is wrong with the following code? x should
be given a value of 24. What are two ways this can be
legally accomplished?
1 public class H2ClassI {
2 final int x;
3
4 public static void main (String args []) {
5 H2ClassI h = new H2ClassI ();
6 h.x = 24;
7 } // end main
8 } // end class H2ClassI
9. (10 pts) What is wrong with the following code? Give
two effective ways to fix it.
1 import javax.swing.*;
2 import java.awt.event.*;
3
4 public class H2ClassJ extends JFrame {

Sign up to view the full document!

lock_open Sign Up
Showing Page:
2/4
5 public static final long serialVersionUID = 22;
6
7 public H2ClassJ () {
8 addMouseListener (new MouseListener () {
9 public void mouseClicked (MouseEvent e) {}
10 });
11 } // end constructor
12
13 } // end class H2ClassJ
10. (10 pts) Why does the following code give a compiler
warning? (Use javac -Xlint) How should it be fixed?
1 import javax.swing.*;
2
3 public class H2ClassK {
4 String [] sa = {\"a\", \"b\", \"c\"};
5 JComboBox jcbA = new JComboBox (sa);
6 } // end class H2ClassK
Solution
with the first one,you declared the variable as final that
means you cannot modify the value if you make the
variable as final so,x=a; not works.here the modified code
is given
public class H2ClassG {
final int x;

Sign up to view the full document!

lock_open Sign Up
Showing Page:
3/4

Sign up to view the full document!

lock_open Sign Up
End of Preview - Want to read all 4 pages?
Access Now
Unformatted Attachment Preview
6. (10 pts) What is wrong with the following code? How should it be fixed? 1 public class H2ClassG { 2 final int x; 3 4 H2ClassG () {} 5 H2ClassG (int a) {x = a;} 6 } // end class H2ClassG 7. (10 pts) What is wrong with the following code? How should it be fixed? 1 public class H2ClassH { 2 final int x; 3 4 int H2ClassH () { 5 if (x == 7) return 1; 6 return 2; 7 } // end 8 } // end class H2ClassH 8. (10 pts) What is wrong with the following code? x should be given a value of 24. What are two ways this can be legally accomplished? 1 public class H2ClassI { 2 final int x; 3 4 public static void main (String args []) { 5 H2ClassI h = new H2ClassI (); 6 h.x = 24; 7 } // end main 8 } // end class H2ClassI 9. (10 pts) What is wrong with the following code? Give two effective ways to fix it. 1 import javax.swing.*; 2 import java.awt.event.*; 3 4 public class H2ClassJ extends JFrame { 5 public static final long serialVersionUID = 22; 6 7 public H2ClassJ () { 8 addMouseListener (new MouseListener () { 9 public void mouseClicked (MouseEvent e) {} 10 }); 11 } // end constructor 12 13 } // end class H2ClassJ 10. (10 pts) Why does the following code give a compiler warning? (Use javac -Xlint) ...
Purchase document to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Anonymous
I was stuck on this subject and a friend recommended Studypool. I'm so glad I checked it out!

Studypool
4.7
Indeed
4.5
Sitejabber
4.4