0% found this document useful (0 votes)
151 views

Bit 2204a Bbit 310 Bac 2201 BSD 2107 Bisf 2201 Java Programming Rayfrankmuriithi

The document contains instructions for a Java programming exam question. It asks the candidate to develop a class called Number with methods like isZero(), isPositive() etc. It also asks the candidate to develop classes for processing exam results and managing employee information.

Uploaded by

Frank Ray
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
151 views

Bit 2204a Bbit 310 Bac 2201 BSD 2107 Bisf 2201 Java Programming Rayfrankmuriithi

The document contains instructions for a Java programming exam question. It asks the candidate to develop a class called Number with methods like isZero(), isPositive() etc. It also asks the candidate to develop classes for processing exam results and managing employee information.

Uploaded by

Frank Ray
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

NAME: RAYFRANK MURIITHI

KIBAARA
REGISTRATION NUMBER: 20/03618
MODE OF STUDY: DAY
CAMPUS: MAIN CAMPUS
UNIVERSITY EXAMINATIONS: 2020/2021
EXAMINATION FOR DEGREE IN BACHELOR OF SCIENCE IN
INFORMATION TECHNOLOGY/ BUSINESS I.T/APPLIED
COMPUTING/ SOFTWARE DEV./INFO. SECURITY& FORENSICS
BIT 2204A/ BBIT 310 /BAC 2201/ BSD 2107/BISF 2201:
JAVA PROGRAMMING
PART TIME/FULL TIME/DISTANCE LEARNING
ORDINARY EXAMINATIONS
DATE: AUGUST, 2021 TIME: 3 HOURS

INSTRUCTIONS: Answer ALL the questions


QUESTION ONE [20 MARKS]
You are required to develop a simple application to demonstrate the use of classes in Java. The
program will require you to create class called Number with only one private instance variable as
a double primitive type. In addition, you will include the following methods (include respective
constructors) isZero( ), isPositive(), isNegative( ), isOdd( ), isEven( ). The above methods return
boolean primitive type. Further create methods getSqr(), sumDigits(), and getReverse() the above
methods return double primitive type.
a). Create a class diagram for this case
b). Write a java program to implement your class diagram
import java.lang.Math. *; import java.lang.Number.*;

class Num4
{
private double db1;
private long lg;
public Num4 ()
{
db1 = 108.0d;
lg = 249;
}
public Num4(double d, long l)
{
db1 = d;
lg = 1;
}
public boolean isZero()
{
if (db1 == 0.0)
return true;
else
return false;
}
public boolean isPositive()
{
if (db1 > 0.0)
return true;
else
return false;
}
public boolean isNegative()
{
if (db1 < 0.0)
return true;
else
return false;
}
public boolean isodd()
{
if (db1 % 2 != 0.0)
return true;
else return false;
}
public boolean isEven()
{
if (db1 % 2 == 0.0)
return true;
else return false;
}
public boolean isPrime()
{
int i, lastn;
double a;
boolean flag;
a = Math.sqrt(lg);
lastn = (int)a;
flag = true;
for (i = 2; i < lastn; i++)
{
if (lg != i)
{
if (lg % i == 0)
{
flag = false;
break;
}
}
}
if (flag)
return true;
else return false;
}
public boolean isAmstrong()
{
if (db1 == 0.0)
return true;
else return false;
}
public double getFactorial()
{
double d = 1;
for (int i = 1; i < lg; i++)
d *= i;
return d;
}
public double getSqrt()
{
double d;
d = (double)lg;
d = Math.sqrt(d);
return d;
}
public double getSqr()
{
double d;
d = (double)lg;
d = d * d;
return d;
}
public double sumDigits()
{

double d = 0;
while (lg > 9)
{
d += lg % 10;
lg = lg / 10;
}
d += lg;
return d;
}
public double getReverse()
{
double d = 0;
double temp;
while (lg > 9)
{
temp = lg % 10;
d = d * 10 + temp;
lg = lg / 10;
System.out.println("\n" + temp + "\t" + d + " \t " + lg);
}
d = d * 10 + lg;
System.out.println("Inside class" + d);
return d;
}
public void dispBinary()
{
System.out.println("ByteValue of lg :" + Long.toBinaryString(lg));
}
public static void main(String args [ ])
{
Num4 mynum = new Num4();
double d = 199;
System.out.println(" The given numbers are 108.0d and 249");
System.out.println("isZero " + mynum.isZero());
System.out.println("isPositive " + mynum.isPositive());
System.out.println("isNegative " + mynum.isNegative());
System.out.println("isOdd " + mynum.isodd());
System.out.println("isEven " + mynum.isEven());
System.out.println(" isPrime " + mynum.isPrime());
System.out.println("getFactorial " + mynum.getFactorial());
System.out.println("getSqrt " + mynum.getSqrt());
System.out.println("getSqr " + mynum.getSqr());
System.out.println("sumDigits " + mynum.sumDigits());
System.out.println("getReverse " + mynum.getReverse());
mynum.dispBinary();
System.out.println(" isPrime " + mynum.isPrime());
}
}

(8 Marks)

QUESTION TWO [15 MARKS]


You are required to develop a simple application for processing examination results. You will
need to come up with a class Result that contains registration no, name and marks of four
subjects. The marks are stored in an array of integers. The class also contains the following
member methods.
i) The input() method is used to input values
ii) The show() method is used to display values
iii) The total() returns the total marks a student
iv) The avg() method returns the average marks of a student
Required:
a). Create a class diagram for the above case (8 Marks)
b). Write a java program for the above class diagram. (7 Marks)
import java.io.*;

import java.util.*;

class Student

Scanner sc=new Scanner(System.in);

int RegNo,Total=0,subjects;

String name;

int marks[];

Student()

System.out.print("Enter Registration No.: ");

RegNo=sc.nextInt();

System.out.print("Enter Student Name: ");

name=sc.next();;

getDisMarks();

}
public void getDisMarks()

marks=new int[3];

System.out.print("Enter marks of Physics: ");

marks[0]=sc.nextInt();

System.out.print("Enter marks of Chemistry: ");

marks[1]=sc.nextInt();

System.out.print("Enter marks of Maths: ");

marks[2]=sc.nextInt();

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

Total+=marks[i];

System.out.println("Total Marks of student "+name+": " +Total);

class StudentDemo

public static void main(String args[])

Student s[]=new Student[5];

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

s[i]=new Student();

}
}

QUESTION THREE [15 MARKS]


You have been tasked to come up with an application to manage employee information in a
certain company. To this end you are required to design a new class SalariedEmployee that
extends from class Employee and overrides the method earnings ( ) of class Employee to
calculate the fixed monthly salary of each employee. You will also need to create object of
SalariedEmployee class in EmployeeTest class(i.e, main class) to set the record for new
employee and print it using toString()method.

Required:
a). Develop a class diagram for this case

Reference: https://www.researchgate.net/figure/Class-diagram-for-a-management-
employee-system_fig1_320246921
b). Write a java program to implement the design above. (7 Marks)
public class Employee {

private String firstName;

private String lastName;

private double monthlySalary;

public Employee(String firstName, String lastName, double monthlySalary) {

super();

this.firstName = firstName;

this.lastName = lastName;

if(monthlySalary <= 0)
{
this.monthlySalary = 0.0;

else
this.monthlySalary = monthlySalary;
}

public String getFirstName() {


return firstName;
}

public String getLastName() {


return lastName;
}

public double getMonthlySalary() {


return Math.round(monthlySalary*100)/100.0;
}

import java.util.Scanner;

public class EmployeeTest {


public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter Employee One's first name: ");


String firstName = scanner.nextLine();

System.out.print("Enter Employee One's last name: ");


String lastName = scanner.nextLine();

System.out.print("Enter Employee One's monthly salary: ");


double monthlySalary = scanner.nextDouble();

Employee employee1 = new Employee(firstName, lastName,


monthlySalary);

scanner.nextLine();

System.out.print("Enter Employee Two's first name: ");


firstName = scanner.nextLine();

System.out.print("Enter Employee Two's last name: ");


lastName = scanner.nextLine();

System.out.print("Enter Employee Two's monthly salary: ");


monthlySalary = scanner.nextDouble();

Employee employee2 = new Employee(firstName, lastName,


monthlySalary);

System.out.print("\nEmployee One's name: ");


System.out.println(employee1.getLastName()+",
"+employee1.getFirstName());
System.out.print("Employee One's yearly salary: $ ");

System.out.println(Math.round(employee2.getMonthlySalary()*12*100)/100.00);

System.out.print("Employee Two's name: ");


System.out.println(employee2.getLastName()+",
"+employee2.getFirstName());
System.out.print("Employee Two's yearly salary: $ ");

System.out.println(Math.round(employee1.getMonthlySalary()*12*100)/100.00);

scanner.close();

You might also like