Professional Documents
Culture Documents
To be filled by Student
Question No 1:
As a developer you have to create the incomplete class with the name Movie. It should
contain movie title and its price with proper access levels and data types. This class have a
constructor that requires the movie title, and add two get methods , one that returns the title
and one that returns the price. Include an incomplete function having name setAmount().
Now you have to create two sub classes of movie class. Adventure and Horror. Both classes
have a setAmount() function that sets the price for all Adventure movie to 39.99 dollars and
for all others Horror movies to 45.99 dollars. Build a constructor for both classes, contains a
call to setAmount() within each class.Write an application in which build both a Adventure
and a Horror Book, and display their fields.What if we don’t have setAmount not in subclass.
What type of inheritance it is ? if we make reference variable of Movie and instantiate with
sub class which functions are called ? Explain . (5 Marks)
Solution:
CODE:
Movie:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplicationpprquestion1;
/**
*
* @author SIBGHA ZAIB
*/
abstract class Movie {
private String movieTitle;
protected double moviePrice;
Movie(String movieTitle){
this.movieTitle = movieTitle;
}
public String getMovieTitle()
{
return this.movieTitle; }
public double getMoviePrice()
{
return moviePrice;
}
public void setAmount()
{
}
}
Main:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplicationpprquestion1;
/**
*
* @author SIBGHA ZAIB
*/
public class JavaApplicationPPRQuestion1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Movie obj1 = new Adventure(“PEACE OF LIFE.");
System.out.println(" Movie name: "+obj1.getMovieTitle()+"\n Movie price: "+
obj1.getMoviePrice());
Movie obj2 = new Horror("ANIMATEDS");
System.out.println("Horror movie name: "+obj2.getMovieTitle()+"\n Movie price: "+
obj2.getMoviePrice());
}
Horror:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplicationpprquestion1;
/**
*
* @author SIBGHA ZAIB
*/
class Horror extends Movie{
Horror(String movieTitle) {
super(movieTitle); this.setAmount();
}
@Override public void setAmount()
{
this.moviePrice = 45.99;
}
}
ADVENTURE:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplicationpprquestion1;
/**
*
* @author SIBGHA ZAIB
*/
class Adventure extends Movie{
Adventure(String movieTitle)
{
super(movieTitle); this.setAmount();
}
@Override public void setAmount() {
this.moviePrice = 39.99;
}
}
Theory:
1. If we don't have set_ Amount function in subclass then we need to declare the subclass with
abstract, (i.e. abstract public subclass).
2. it is a Hierarchical inheritance.
3. The function in the sub class will be called if we make reference of movie and instantiate with sub
class.
QUESTION NO 2:
Create a class Array Demo which also have main function. Create a method in this class with
name Find Mid Element which should find the mid number in the array without using /
operator. If it is even number print factorial number, if it is odd calculate the sum of all
numbers in an array. This array should be initialized by user. (5 Marks)
Solution:
Code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package q1;
import java.util.Scanner;
/**
*
* @author SIBGHA ZAIB
*/
public class Q1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int n;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number of elements you want to store: ");
n=sc.nextInt();
int[] array = new int[n];
for(int i=0; i<n; i++)
{
System.out.println("Enter the elements of the array: ");
array[i]=sc.nextInt();
}
ArrayDemo demo = new ArrayDemo();
demo.findMidElement(array);
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package q1;
import java.util.Arrays;
/**
*
* @author SIBGHA ZAIB
*/
public class ArrayDemo {
public void findMidElement(int array[]){
if(array.length>0){
int mid = findMidNumber(array);
System.out.println("Mid value is: " + mid);
if(mid % 2 == 0){
int fact=1;
for(int i=1;i<=mid;i++){
fact=fact*i;
}
System.out.println("Number is even and factorial is: " + fact);
} else {
int sum = 0;
for(int i=1;i<array.length;i++){
sum= sum + array[i];
}
System.out.println("Number is odd and sum is: " + sum); }
} else {
System.out.println("Array Size must be greater than 0");
}
}
private static int findMidNumber(int array[]){
Arrays.sort(array);
return array[array.length >> 1];
}
}
Question No 3:
Write a class having name Registered Courses. It contains course code, credit hours and
grades as fields. These fields have getter and setter functions. Add an interface which
contains a method name submit Fee that takes an argument as int and return type as String.
Write another function name Show with return type String in that interface with no
arguments. Implements the above interface to Registered Courses class. Define two
constructors, one default and other is parameterized to initialize the class members . Now
submit Fee has to be implemented in such a way that its displays “Submitted” if functions
takes non zero value otherwise “not Submited”.Show function will display course code ,
credit hours and grade. Write Main method in another class and call only submit Fee and
Show method. Of class Registered Courses. Why interface don’t have constructors ?
(5 Marks)
Solution:
Code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package q2;
/**
*
* @author SIBGHA ZAIB
*/
public interface Course {
String submitFee(int a);
String show();
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package q2;
/**
*
* @author SIBGHA ZAIB
*/
public class Q2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
RegisteredCourses course = new RegisteredCourses("Mathematics","4","B");
System.out.println(course.submitFee(100));
System.out.println(course.show());
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package q2;
/**
*
* @author SIBGHA ZAIB
*/
public class RegisteredCourses implements Course {
private String courseCode;
private String creditHours;
private String grades;
RegisteredCourses(){
System.out.println("Default constructor");
}
/* Parameterized constructor with
* two integer arguments
*/
@Override
public String submitFee(int a) {
if(a>0){
return "Fee Submission Successful ";
} else {
return "Fee Submission Unsccessful";
}
}
@Override
public String show() {
return "Course code: " + courseCode + ", credit hours: " + creditHours + ", grade: " + grades;
}
}
Question No 4:
Write the JDBC code for the following form. Assume database table has been created with
table name Registration with attributes RegId , Name ,Password , Email , Address and
Gender. You have to insert data from form into database ONLY IF no data exists otherwise
update that. You can assume other fields. Show this record also. (5 Marks)
Answer:
CODE:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package question4;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
*
* @author SIBGHA ZAIB
*/
public class student extends javax.swing.JFrame {
Connection conn;
String URL = "jdbc:sqlserver://DESKTOP-7IS2ILT;databaseName=17-Arid-
6459;integratedSecurity=true";
/** Creates new form student
* @throws java.lang.ClassNotFoundException
* @throws java.sql.SQLException */
public student() throws ClassNotFoundException, SQLException {
initComponents();
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn=DriverManager.getConnection(URL);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jCheckBox1.setText("jCheckBox1");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Name");
jLabel2.setText("Password");
jLabel3.setText("Email");
jLabel4.setText("Address");
jLabel5.setText("Gender");
btnsubmit.setText("Register");
btnsubmit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnsubmitActionPerformed(evt);
}
});
btnupdate.setText("Update");
btnupdate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnupdateActionPerformed(evt);
}
});
pack();
}// </editor-fold>
try {
s = conn.createStatement();
} catch (SQLException ex) {
Logger.getLogger(student.class.getName()).log(Level.SEVERE, null, ex);
}
String q;
q = "insert into Students
(name,password,email,address,gender)values('"+jTextField1.getText()+"','"+jTextField2.getText()+"','
"+jTextField3.getText()+"','"+jTextField4.getText()+"','"+jTextField5.getText()+"')";
try {
i = s.executeUpdate(q);
} catch (SQLException ex) {
Logger.getLogger(student.class.getName()).log(Level.SEVERE, null, ex);
}
if(i>0){
JOptionPane.showMessageDialog(null, "Record Inserted");
}
else{
JOptionPane.showMessageDialog(null, "Record Not Inserted");
}
}
private void btnupdateActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int i=0;
Statement s = null;
try {
s = conn.createStatement();
} catch (SQLException ex) {
Logger.getLogger(student.class.getName()).log(Level.SEVERE, null, ex);
}
String q;
q = "update Students set
name='"+jTextField1.getText()+"',password='"+jTextField2.getText()+"',email='"+jTextField3.getText(
)+"',address='"+jTextField4.getText()+"',gender='"+jTextField5.getText()+"' where
email='"+jTextField3.getText()+"'";
try {
i = s.executeUpdate(q);
} catch (SQLException ex) {
Logger.getLogger(student.class.getName()).log(Level.SEVERE, null, ex);
}
if(i>0){
JOptionPane.showMessageDialog(null, "Record updated");
}
else{
JOptionPane.showMessageDialog(null, "Record Not exist");
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(student.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(student.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(student.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(student.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
}
//</editor-fold>