Download solutionshomework_java

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
/*It is not the complete solution, for complete solution, please reach me. I
will send you solutions immediately. */
Question 1:
There are two kinds of UML diagrams. These are:
1. Structural Diagram
2. Behavioral Diagram
1. Structural Diagram:
a. Class Diagram: It used to model a system in object oriented
manner. It represents different classes their attributes, methods
and relations with other classes.
b. Object Diagram: It is the instance of class diagram. It shows
relationship among objects. It gives the real life snapshot of
the system in a particular time.
c. Component Diagram: Complex systems are consisting of many simple
components. Component diagram shows the relation among these
simple components and the interface throw which they communicate.
d. Deployment Diagram: It shows set of processing nodes and their
relationship. It gives the final deployment view of the system.
e. Composite Structure Diagram:
f. Package Diagram:
2. Behavioral Diagram:
a. Use case Diagram:
b. State chart Diagram:
c. Activity Diagram:
d. Sequence Diagram:
Question: 2.
Answer:
We need to override toString() method of class H2ClassA and H2ClassB. Please
find below code with overridden toString() method in both class.
public class H2ClassA {
ArrayList<H2ClassB> list = new ArrayList<H2ClassB>();
public static void main(String args[]) {
H2ClassA y = new H2ClassA();
int[] v = { 4, 3, 7, 5, 99, 3 };
for (int m : v)
y.list.add(new H2ClassB(m));
System.out.println(y);
} // end main
@Override
public String toString(){
String s = "";
for(H2ClassB t : list){
s = s+t.toString() + " ";
}
return s;
}
} // end class H2ClassA
class H2ClassB {
int x;
H2ClassB(int a) {
x = a;
}
@Override
public String toString() {
return Integer.toString(x);
}
}
Solution: 3:
Answer 1:
Class H2ClassD was calling default constructor implicitly, but there was no
default constructor in its base class H2ClassC. To overcome this error, we
need to add default constructor.
public class H2ClassC {
/*
* This class does not have default constructor. So we need to add
default constructor.
*
*/
H2ClassC(){}
H2ClassC (int a) {}
} // end class H2ClassC
class H2ClassD extends H2ClassC{
// default constructor
H2ClassD(){
super();
}
} // end class H2ClassD
Answer: 2. Implicit super constructor H2ClassC() is undefined for default
constructor. Must define an explicit constructor for class H2ClassD.
public class H2ClassC {
H2ClassC (int a) {}
} // end class H2ClassC
class H2ClassD extends H2ClassC{
// Explicit constructor
H2ClassD(int a) {
super(a);
}
} // end class H2ClassD
Solution: 4. Constructor call should be the first call in any constructor. In this case x = a; is the first
statement and this(5, 12) is second statement. So we need to move this(5, 12) in first statement. Please
see the solution code below:
public class H2ClassE {
int x, y, z;
H2ClassE(int a) {
// constructor Call should be the first statement in constructor
this(5, 12);
x = a;
// this (5, 12);
}
H2ClassE(int b, int c) {
y = b;
z = c;
}
}
Solution: 5. Here problem is type mismatch. We are assigning a double value to a int variable. So type
mismatch happened. Please see below solution code assigning int and double.
public static final int myNumber = 17;
public static final double mydouble = 17.32;
Solution: 6. Here problem is final variable x is not assigned any value in
the default constructor. Please see the below solution code.
public class H2ClassG {
final int x;
H2ClassG() {
x = 2; // assign value to final variable. This value cant be
change as x is a final(constant) variable.
}
H2ClassG(int a) {
x = a;
}
} // end class H2ClassG
Solution: 7. Final variables are constant throughout program. Even changing its value throws error. We
need to initialize the final variable either in declaration or in constructor. Here we are initializing x in
declaration time only.
1st method ) initialize final variable x during declaration
public class H2ClassH {
final int x = 7; /* we can take different value than 7 if want to
return 2 */
int H2ClassH() {
if (x == 7)
return 1;
return 2;
} // end
}
2nd method ) initialize final variable x within constructor
public class H2ClassH {
final int x ;
H2ClassH()// we have taken default constructor we may change as per need
{
x=7; /* we can take different value than 7 if want to return 2 */
}
int H2ClassH() {
if (x == 7)
return 1;
return 2;
} // end
}
Solution: 8. Final variables are constant throughout program. Even changing its value throws error. We
need to initialize the final variable either in declaration or in constructor. Here we are initializing x in
declaration time only.
1st method ) initialize final variable x during declaration
public class H2ClassI {
final int x = 24;
public static void main (String args []) {
//
H2ClassI h = new H2ClassI ();
h.x = 24; It is line having error
} // end main
} // end
class H2ClassI
2nd method ) initialize final variable x within constructor
public class H2ClassI {
final int x ;
H2ClassI() // we have taken default constructor we may change as per need
{
x=24;
}
public static void main (String args []) {
//
H2ClassI h = new H2ClassI ();
h.x = 24; It is line having error
} // end main
} // end
class H2ClassI
Solution: 9. There is error in line number 8 because we are using anonymous class implementing
MouseListener interface but it has not implemented all
the methods of MouseListener interface it is implementing
only one method we have to implement remaining method
4 methods to implement:
- java.awt.event.MouseListener.mouseEntered()
- java.awt.event.MouseListener.mouseExited()
- java.awt.event.MouseListener.mousePressed()
- java.awt.event.MouseListener.mouseReleased()
import javax.swing.*;
import java.awt.event.*;
public class H2ClassJ extends JFrame {
public static final long serialVersionUID = 22;
public H2ClassJ () {
addMouseListener (new MouseListener () {
public void mouseClicked (MouseEvent e) {}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mousePressed(MouseEvent arg0) {
}
public void mouseReleased(MouseEvent arg0) {
}
});
} // end constructor
}
Solution: 10.
Due to introduction of Generics into JComboBox component in Java 7 it was showing warning
in this line ‘JComboBox<String> jcbA = new JComboBox<> (sa);’
To fix it use the following code now it is showing no warning
public class H2ClassK {
String [] sa = {"a", "b", "c"};
JComboBox<String> jcbA = new JComboBox<> (sa);
}