CS 213: OBJECT ORIENTED IN JAVA PAPER 2018/2019 SOLUTION
Qn 1
a) A constructor for initializing properties of class B.
class A{
int y;
public A(int y){
this.y=y;
}
}
class B extends A{
public B(int x){
this.y=x;
}
}
b) The following is result output after executing each line
ewiaroft
Qn 2
a. public MyClass (int num, String title) {
numOfitems=num;
reportTitle=title;
}
b. MyClass myObject= new MyClass(1,”University od Dodoma”);
c. [Link]();
d. [Link](2,”SUA”);
Qn3
a. The error is at child class on throwing SQLException, the child class overriding method cannot
through exception if parent class method does not.
b. 1
Qn4
a. Below program create database connection and retrieve data from Employee Database in
EmployeeInfo Table
import [Link].*;
public class DataRetrieval {
public static void main(String[] args) {
Connection connection = null;
try {
[Link]("[Link]");
String url = "jdbc:mysql:3306//localhost/EmployeeDB";
String username = "root";
String password = "root";
connection = [Link](url, username, password);
} catch (Exception e) {
[Link]([Link]());
try {
Statement statement = [Link]();
ResultSet results = [Link]("SELECT first_name,last_name,pnone_number FROM
EmployeeInfo");
while ([Link]()) {
String firstName = [Link]("first_name");
String lastName = [Link]("last_name");
String phone_number= [Link]("pnone_number");
[Link]("FirstName: %s LastName: %s PhoneNumber: \n
",firstName,lastName,phone_number); }
} catch (SQLException e) {
[Link]("Could not retrieve data from the database " + [Link]());
}
}
}
b. Below is the code which implement the given scenario
public class myMain {
public static void main(String[] args) {
Rectangle r= new Rectangle();
Triangle t= new Triangle();
[Link](10.5);
[Link](20.2);
[Link](30.5);
[Link](40.65);
[Link]([Link]());
[Link]([Link]());
}
}
abstract class Polygon{
double height;
double width;
void setHeight(double height){
[Link]=height;
}
void setWidth(double width){
[Link]=width;
}
abstract double area();
}
Continue…...
class Rectangle extends Polygon{
//formula for calculating area of rectangle= L x W
private double polygonArea;
@Override
double area(){
polygonArea= height*width;
return polygonArea;
}
}
class Triangle extends Polygon{
double triangleArea;
@Override
double area(){
triangleArea=0.5*width*height;
return triangleArea;
}
}
Qn5
Coming Soon…………………………………
Qn6
a. …
Import [Link];
public class myMain{
public static void main(String[] args) {
Age a= new Age();
try{
[Link]();
} catch(CiveException e) {
[Link](e);
}
}
}
class Age{
int age;
Scanner ageScanner= new Scanner([Link]);
Age(){
[Link]("Enter electorate's age");
age=[Link]();
}
void ageValidator()throws CiveException{
if(age<18){
throw new CiveException();
}else{
[Link](“ok”);
}
}
}
class CiveException extends Exception{
CiveException(){
Super(“Under 18 is not allowed”);
}
}
b. …….
import [Link];
public class AreaOfCircle extends JOptionPane{
AreaOfCircle(){
String x= showInputDialog(null,"Enter Radius of Circle","Calculator",PLAIN_MESSAGE);
double radius=[Link](x);
double area=3.14*radius*radius;
if(x!=null){
showMessageDialog(null,"The Area of a circle is : "+area+" Unit
Square","Calculator",PLAIN_MESSAGE);
}
}
public static void main(String args[]){
AreaOfCircle area= new AreaOfCircle();
}
}