0% found this document useful (0 votes)
52 views11 pages

Mid Exam CS 432 Programming Questions

The document is a midterm exam paper containing 3 programming questions in Java. Question 1 asks to define an Atom class with fields for protons, neutrons, electrons and related methods. Question 2 asks to write a sorting method to sort a character array into the string "pakistan". Question 3 asks to write a method that takes an integer array, finds the factorial of the middle element and returns it. The student code provides full definitions and implementations of the Atom class and related methods for Question 1, a sorting class and main method for Question 2, and an ArrayDemo class with the required numFind method for Question 3.

Uploaded by

Lucky Asr Awais
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views11 pages

Mid Exam CS 432 Programming Questions

The document is a midterm exam paper containing 3 programming questions in Java. Question 1 asks to define an Atom class with fields for protons, neutrons, electrons and related methods. Question 2 asks to write a sorting method to sort a character array into the string "pakistan". Question 3 asks to write a method that takes an integer array, finds the factorial of the middle element and returns it. The student code provides full definitions and implementations of the Atom class and related methods for Question 1, a sorting class and main method for Question 2, and an ArrayDemo class with the required numFind method for Question 3.

Uploaded by

Lucky Asr Awais
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Pir Mehr Ali Shah

Arid Agriculture University, Rawalpindi


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

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


Total Marks:…12……………………………Date of Exam:……26/4/21 …………………......................................
Degree: BSCS …………………….Semester:……… 8Th ……………… Section:…B………………………………
Marks
Q. No. 1 2 3 4 5 6 7 8 9 10 Obtained/
TotalMarks
Marks
Obtaine
d
Total Marks in Words:
Name of the teacher:
Who taught the course: Signature of teacher / Examiner:

To be filled by Student

Registration No.: ………17-Arid-6463.……… Name:……Sundas Mumtaz …..

Answer the following questions.

Question#1

Q1: There are different kinds of atoms with having neutrons , protons and electrons
with their specific atomic masses. Hydrogen is one of them. [4 marks]

A) In Java language of your choice, write a definition for an atom class that contains:

(i) Fields for storing the numbers of protons, neutrons and electrons with appropriate
visibility.
(ii) Setter and getter methods for manipulating these fields, ensuring that the minimum
value for electrons and protons is 1, and the minimum value for neutrons is 0.
(iii) A parameterized constructor that initializes new objects of atom to be the smallest
element (Hydrogen), for which the number of protons is 1, the number of neutrons is 0,
and the number of electrons is 1.
B) Write a new method for the atom class called isIon that will return true or false, depending
upon whether the atom is an ion. An atom is an ion if it is charged (i.e., if the number of
electrons ≠ the number of protons).
C) Write a new method for the atom class called getAtomicMassNumber that will calculate and
return the atomic mass number of the atom. Atomic mass number of an atom (often
denoted A) is defined as the number of protons plus the number of neutrons.

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 midpaper.question.pkg1;

/**

* @author Sundas Mumtaz

*/

public class MidPaperQuestion1 {

/**

* @param args the command line arguments

*/

public static void main(String[] args){

System.out.println("For Helium Add Protons Neutrons and Electrons:");

atom a=new atom();

a.setNewtrons(2);

a.setElectrons(2);

a.setProtons(2);

System.out.println("Helium: "+a.getProtons()+" Protons "+a.getNeutrons()

+" Newtrons "+a.getElectrons()+" Electrons");

System.out.println("isIon Helium:"+a.isIon());

System.out.println("Atomic Mass Number of Helium:"+a.getAtomicMassNumber()+"\n");

System.out.println("For Boron Add Protons Neutrons and Electrons:");

atom b=new atom();

b.setNewtrons(6);

b.setElectrons(5);

b.setProtons(5);

System.out.println("Boron: "+b.getProtons()+" Protons "+b.getNeutrons()


+" Newtrons "+b.getElectrons()+" Electrons");

System.out.println("isIon Boron:"+b.isIon());

System.out.println("Atomic Mass Number of Boron:"+b.getAtomicMassNumber());

atom.java:

/*

* 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 midpaper.question.pkg1;

/**

* @author Sundas Mumtaz

*/

public class atom {

private int protons;

private int neutrons;

private int electrons;

public atom()

this.protons=1;

this.neutrons=0;

this.electrons=1;

}
public void setProtons(int protons)

if(protons>=1)

this.protons=protons;

System.out.println("Protons added");

else

System.out.println("Proton Must have postitive value >1");

public void setElectrons(int electrons)

if(electrons>=1)

this.electrons=electrons;

System.out.println("Electrons added");

else

System.out.println("Electrons Must have postitive value >1");

public void setNewtrons(int neutrons)

if(neutrons>=0)

this.neutrons=neutrons;
System.out.println("Neutrons added");

else

System.out.println("Neutrons Must have postitive value >0");

public int getElectrons()

return this.electrons;

public int getNeutrons()

return this.neutrons;

public int getProtons()

return this.protons;

public boolean isIon()

if(this.electrons==this.protons)

return false;

else

return true;

public int getAtomicMassNumber()

int AtomicMass=this.neutrons+this.protons;

return AtomicMass;
}

Output:

Question# 2:
Write a class named Sorting with sort function in order to sort the following array as
pakistan.Call sort function in main function. [4 marks]

char[] p= {‘a’, ‘p’, ‘i’, ‘k’, ‘t’, ‘a’, ‘n’, ‘s’};

Output as = pakistan
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 question2;
/**

* @author Sundas Mumtaz

*/

public class Question2 {

public static void main(String[] args) {

sorting myObj = new sorting ();

char[] p= {'a', 'p', 'i', 'k', 't', 'a', 'n', 's'};

System.out.println(p);

System.out.println(myObj.sort(p));

/*

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

* @author Sundas Mumtaz

*/

public class sorting {

char [] sort (char[] s) {

char[] p= {'p', 'a', 'k', 'i', 's', 't', 'a', 'n'};

char[] sorted = {'a', 'p', 'i', 'k', 't', 'a', 'n', 's'};

int i,j;

for (i = 0; i < p.length; i++) {

for (j = 0; j < s.length; j++) {

if (p[i] == s[j]){

sorted[i] = s[j];

};

return sorted;

}
Question3

/*

* 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 question3;

import java.util.Scanner;

/**

* @author Sundas Mumtaz

*/

public class Question3 {


public static void main(String[] args) {

ArrayDemo myObj = new ArrayDemo ();

Scanner inp = new Scanner(System.in);

System.out.print("Enter array (upto 10) ");

int[] inp_array = new int[10];

for(int i=0; i<10; i++)

inp_array[i]=inp.nextInt();

System.out.println("Factorial is : " + myObj.numFind(inp_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 question3;

/**

* @author Sundas Mumtaz

*/
public class ArrayDemo {

public int factorial(int n){

if (n == 0)

return 1;

else

return(n * factorial(n-1)); }

int numFind (int [] inp) {

int middle = inp.length >>1;

System.out.println("middle value is : "+ inp[middle]);

int out = factorial( inp[middle]);

return out ;

You might also like