You are on page 1of 17

Pir Mehr Ali Shah

Arid Agriculture University, Rawalpindi


Office of the controller of Examinations
Final Exam (THEORY) / Spring 2021 (Paper Duration 12 hours)
To be filled by Teacher

Course No.: CS-432…………………Course Title:…… Modern Programming Languages…………………………


Total Marks:……….……20……………Date of Exam:……5th July 2021…………………....................................
Degree: …BSCS…………………………….Semester:……… 8th ………… Section:………… B ………………………
Marks
Q. No. 1 2 3 4 5 6 7 8 9 10 Obtained/
TotalMarks
Marks
Obtained
Total Marks in Words:
Name of the teacher: Mr. Seemab Zafar
Who taught the course: Signature of teacher / Examiner:

To be filled by Student

Registration No.: 17-Arid-6459 Name: SIBGHA ZAIB

Answer the following questions.

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
*/

RegisteredCourses(String courseCode, String creditHours,String grades){


this.courseCode= courseCode;
this.creditHours= creditHours;
this.grades= grades;
System.out.println("constructor with Two parameters");
}

public String getCourseCode() {


return courseCode;
}

public void setCourseCode(String newCourseCode) {


this.courseCode = newCourseCode;
}
public String getCreditHours() {
return creditHours;
}

public void setCreditHours(String newCreditHours) {


this.creditHours = newCreditHours;
}

public String getGrades() {


return grades;
}

public void setGrades(String newGrades) {


this.grades = newGrades;
}

@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 = new javax.swing.JCheckBox();


jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jTextField3 = new javax.swing.JTextField();
jTextField4 = new javax.swing.JTextField();
btnsubmit = new javax.swing.JButton();
jTextField5 = new javax.swing.JTextField();
btnupdate = new javax.swing.JButton();

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);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());


getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(37, 37, 37)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel3)
.addComponent(jLabel2)
.addComponent(jLabel4))
.addGap(64, 64, 64)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false)
.addComponent(jTextField1)
.addComponent(jTextField2)
.addComponent(jTextField3)
.addComponent(jTextField4, javax.swing.GroupLayout.DEFAULT_SIZE, 156,
Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel5)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(45, 45, 45)
.addComponent(btnsubmit)
.addGap(18, 18, 18)
.addComponent(btnupdate)
.addGap(0, 28, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(75, 75, 75)
.addComponent(jTextField5)))))
.addContainerGap(97, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(37, 37, 37)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(30, 30, 30)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnsubmit)
.addComponent(btnupdate))
.addContainerGap(57, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void btnsubmitActionPerformed(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 = "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>

/* Create and display the form */


java.awt.EventQueue.invokeLater(() -> {
try {
new student().setVisible(true);
} catch (ClassNotFoundException | SQLException ex) {
Logger.getLogger(student.class.getName()).log(Level.SEVERE, null, ex);
}
});
}

// Variables declaration - do not modify


private javax.swing.JButton btnsubmit;
private javax.swing.JButton btnupdate;
private javax.swing.JCheckBox jCheckBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField4;
private javax.swing.JTextField jTextField5;
// End of variables declaration

You might also like