Informatics College Pokhara: Programming

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

CS4001NA Programming

Informatics College Pokhara

Programming
CS4001NA
Coursework 1

Submitted By: Submitted To:


Name: Satish Nepali Mr. Sushil Paudel
London Met ID: 19031165 Module leader
Group: C3 Programming
Date: 01/17/2020

Satish Nepali
CS4001NA Programming

Contents
1. Introduction ........................................................................................................................ 4
2. Class Diagrams ................................................................................................................... 5
3. Pseudocode ......................................................................................................................... 6
3.1 StaffHire class ............................................................................................................. 6
3.2 FullTimeStaffHire ....................................................................................................... 6
3.3 PartTimeStaffHire ....................................................................................................... 7
4. Method Description .......................................................................................................... 10
4.1 StaffHire class ........................................................................................................... 10
4.2 FullTimeStaffHire class ............................................................................................ 10
4.3 PartTimeStaffHire class ............................................................................................ 10
5. Testing .............................................................................................................................. 12
5.1 Test 1: ........................................................................................................................ 12
5.2 Test 2:-....................................................................................................................... 13
5.3 Test 3 ......................................................................................................................... 14
5.4 Test 4 ......................................................................................................................... 15
6. Error and debugging ......................................................................................................... 16
7. Conclusion ........................................................................................................................ 18
8. Appendix .......................................................................................................................... 19
1) StaffHire class ........................................................................................................... 19
2) FullTimeStaffHire ..................................................................................................... 20
3) PartTimeStaffHire ..................................................................................................... 23
9. References ........................................................................................................................ 28

Satish Nepali
CS4001NA Programming

Figure 1: Class Diagram of the Classes ..................................................................................... 5


Figure 2:- Inspect of FullTimeStaffHire before appointing ..................................................... 12
Figure 3:- Inspect FullTimeStaffHire class after appointing ................................................... 12
Figure 4:- Inspect PartTimeStaffHire class before appointing ................................................ 13
Figure 5:- Inspect PartTimeStaffHire class after appointing ................................................... 13
Figure 6:- Inspect PartTimeStaffHire class before terminating ............................................... 14
Figure 7:- Inspect PartTimeStaffHire class after terminating .................................................. 14
Figure 8:- Displaying output of FullTimeStaffHire Class ....................................................... 15
Figure 9:- Displaying output of PartTimeStaffHire ................................................................. 15
Figure 10:- Logical error .......................................................................................................... 16
Figure 11:- Syntax Error .......................................................................................................... 16
Figure 12:- Semantic error while compiling ............................................................................ 16
Figure 13:- Semantic error in the program .............................................................................. 17

Satish Nepali
CS4001NA Programming

1. Introduction
“Java is an object-oriented programming language developed by James Goshling. It
was released in 1995 by Sun Microsystem and since then it has been run in more than 3
Billion devices. The core components required to run the Java are JDK, JVM and JRE.
 JDK: JDK stands for Java Development Kit. It provides important tools required for
the compiling, debugging and executing of the java programs.
 JVM: JVM stands for Java Virtual Machine. It is known as the heart of the java
program since it converts the byte codes to the machine-specific codes. It also
provides the functions like security, memory management, garbage collection etc.
 JRE:- JRE stands for Java Runtime Environment. It is the platform that provides the
facility to execute or run the java program (Pankaj, 2019).”

 Advantages of Java
“There are many advantages of Java programming language which are listed below:
 It is easy to learn, write, compile and debug.
 It is object-oriented language so it allows us to create modular programs and
reusable codes.
 Java is platform-independent so a java program can run in any hardware and
software platform and can easily be transferred from one device to another.
 The unique identifier in JVM identifies and verifies the bytecode making java
a secure programming language.
 There are many online java course on the internet so it can be learned
anywhere, by anyone at the free of cost.

 Disadvantages of Java
With the advantages there are disadvantages of java too. The disadvantages of the
java programming language are listed below:
 It is slow and consumes more memory than some other programming
languages.
 The default look and feel of the GUI is very different than native applications
(ADMIN, 2015).”

4
Satish Nepali
CS4001NA Programming

2. Class Diagrams
“A type of static system structure diagram that describes the structure of the system’s
classes, their attributes, methods and constructors and the relationship between them is
known as Class Diagrams (Paradigim, 2017).”
In the coursework we have been assigned to create three classes StaffHire,
FullTimeStaffHire and PartTimeStaffHire. Each of the classes have their own methods and
attributes which are showed in the class diagrams. The relation between the classes are also
shown in the class diagrams.

StaffHire

FullTimeStaffHire

Figure 1: Class Diagram of the Classes

5
Satish Nepali
CS4001NA Programming

3. Pseudocode
3.1 StaffHire class
Start program.
Creating Class StaffHire.
Declare integer variable vacancy number.
Declare String variable designation.
Declare String variable job type.
Create a constructor with parameters vacancy number, designation, job type.
Initialize the parameters value into respective variables.
Create an accessor method for each variables.
Create a setter method for each variables.
Create a method display.
Display the variables.
End program.

3.2 FullTimeStaffHire
Start Program
Create a sub class FullTimeStaffHire taking StaffHire as super class.
Declare integer variable salary.
Declare integer variable working hour.
Declare string variable staff name.
Declare string variable joining date.
Declare string variable qualification.
Declare string variable appointed by.
Declare Boolean variable joined.
Create a constructor with parameters vacancy number, designation, job type, salary,
working hour.
Call constructor from super class with parameters vacancy number, designation, job type.
Assign the values of salary and working hour from the parameters to the variables.
Assign the variables staff name, joining date, qualification and appointed by as empty
string.
Assign the variable joined as false.

6
Satish Nepali
CS4001NA Programming

Create an accessor method for each variables.


Create a setter method for salary with parameter new salary.
If joined is false then assign the new salary to salary.
Else display it cannot be changed
End if
Create a setter method for working hour with parameter new working hour.
Assign the value of new working hour to working hour.
Create a method hireFullTimeStaff with parameters staff name, joining date, qualification,
appointed by.
If joined is true then
Display staff name and joining date with proper message.
Else
Assign the values of the parameters to variables respectively.
End if
Create a method display
Call the method display from super class.
If joined is true display the values of the variables staff name, working hour, salary,
joining date, qualification, appointed by.
End if
Stop program.

3.3 PartTimeStaffHire
Start program
Create a sub class PartTimeStaffHire taking StaffHire as super class.
Declare integer variable working hour.
Declare integer variable wages per hour.
Declare string variable staff name.
Declare string variable joining date.
Declare string variable qualification.
Declare string variable appointed by.
Declare string variable shifts.
Declare boolean variable joined.

7
Satish Nepali
CS4001NA Programming

Declare boolean variable terminated.


Create a constructor having parameters vacancy number, designation, job type, working
hour, wages per hour, and shifts.
Call constructor from super class using the parameters vacancy number, designation, job
type.
Assign the value of the parameters working hour, wages per hour and shifts into
respective variables.
Assign the variables staff name, joining date, qualification and appointed by as empty
strings.
Assign the variables joined and terminated as false.
Create an accessor method for each variables.
Create a setter method for shifts with parameter as new shifts.
If joined is false then
Assign the value of new shifts into shifts.
End if
Create a method hirePartTimeStaff with parameters staff name, joining date, qualification
and appointed by.
If joined is true then
Display staff name and joining date with a proper message.
Else
Assign the values of the parameters into the variables respectively.
Assign the variable joined as true and terminated as false.
End if.
Create a method terminatTheStaff
If terminated is true then
Display already terminated.
Else
Assign the variables staff name, joining date, qualification, appointed by as empty
string.
Assign the variable joined as false and terminated as true.
End if.
Create a method display.

8
Satish Nepali
CS4001NA Programming

Call the method display from the super class.


If joined is true then
Display the variables staff name, wages per hour, working hour, joining date,
qualification and appointed.
Display the product of wages per hour and working hour as Income per day.
End if
Stop program.

9
Satish Nepali
CS4001NA Programming

4. Method Description
4.1 StaffHire class
 getVacancyNumber():- It returns the value of the variable vacancyNumber.
 getDesignation():- It returns the value of the variable designation.
 getJobType():- It returns the value of the variable jobType.
 setVacancyNumber():- It assigns the value of the parameter into variable
vacancyNumber.
 setDesignation():- It assigns the value of the parameter into the variable designation.
 display():- It displays values of all the variables.

4.2 FullTimeStaffHire class


 getSalary():- It returns the value of the variable salary.
 getWorkingHour():- It returns the value of the variable workingHour.
 getStaffName():- It returns the value of the variable staffName.
 getJoiningDate():- It returns the value of the variable joiningDate.
 getQualification():- It returns the value of the variable qualification.
 getAppointedBy():- It returns the value of the variable appointedBy.
 getJoined():- It returns the value of the variable joined.
 setSalary():- It assigns the value of the parameter to the variable salary if variable
joined is true else it prints a suitable message that staff has already joined.
 setWorkingHour():- It assigns the parameter into the variable workingHour.
 hireFullTimeStaff():- It prints the staffName and joiningDate with a suitable message
if joined is true else it assigns the parameter values to the respective variables.
 display():- It calls the display method from the super class and then it prints the
staffName, salary, workingHour, joiningDate, qualification and appointed by in a
proper way if variable joined is true.

4.3 PartTimeStaffHire class


 getWorkingHour():-It returns the value of the variable workingHour.
 getWagesPerHour():-It returns the value of the variable wagesPerHour.
 getStaffName():-It returns the value of the variable staffName.
 getJoiningDate():-It returns the value of the variable joiningDate.
 getQualification():-It returns the value of the variable qualification.
 getAppointedBy():-It returns the value of the variable appointedBy.
 getShifts():-It returns the value of the variable shifts.
 getJoined():-It returns the value of the variable joined.
 getTerminated():-It returns the value of the variable terminated.
 setShifts():- It assigns the value of the parameter into variable shifts if variable joined
is false.
 hirePartTimeStaff():- It displays the staffName and joiningDate with a proper
message if the variable joined is true else it assigns the parameters value to the
respective variables.
 terminateTheStaff():- It prints that the staff is terminated if terminated is true else it
assigns the variables staffName, joiningDate, qualification and appointedBy as empty
string and assigns variable joined as false where as terminated as true.

10
Satish Nepali
CS4001NA Programming

 display():- It calls the display method from the super class and prints the variables of
staffName, wagesPerHour,WorkingHour, joiningDate, qualification, appointedBy and
product of wagesPerHour and workingHour as income per day if joined is true.

11
Satish Nepali
CS4001NA Programming

5. Testing
5.1 Test 1:

Figure 2:- Inspect of FullTimeStaffHire before appointing

Figure 3:- Inspect FullTimeStaffHire class after appointing

12
Satish Nepali
CS4001NA Programming

5.2 Test 2:-

Figure 4:- Inspect PartTimeStaffHire class before appointing

Figure 5:- Inspect PartTimeStaffHire class after appointing

13
Satish Nepali
CS4001NA Programming

5.3 Test 3

Figure 6:- Inspect PartTimeStaffHire class before terminating

Figure 7:- Inspect PartTimeStaffHire class after terminating

14
Satish Nepali
CS4001NA Programming

5.4 Test 4

Figure 8:- Displaying output of FullTimeStaffHire Class

Figure 9:- Displaying output of PartTimeStaffHire

15
Satish Nepali
CS4001NA Programming

6. Error and debugging


While doing the coursework there were many errors in the program which had to be
fixed by debugging them. There were different type of errors faced during programming
some were easy to find and debug while some were hard to find while easy to debug. Some
of the errors faced during programming are shown below:-
a) Logical error

Figure 10:- Logical error

After I finished the developing of FullTimeHire class and compiled it there were no
errors shown but when I created an object and inspected it I found that there was an error in
the value of workinhgHour. No matter what I input the value it always showe its value 0.
After consulting the module leader I found the error and debugged it by changing the
parameter to workingHour which was written as workingHours.
b) Syntax error

Figure 11:- Syntax Error

The second error faced while developing the programs were syntax errors which were
easy to find and debug. This type of errors were pointed out by Blue J while programming
and was easy to debug since I just had to correct the syntax.
c) Semantic error

Figure 12:- Semantic error while compiling

16
Satish Nepali
CS4001NA Programming

Figure 13:- Semantic error in the program

This type of error were faced only few times. This type of error was shown by the
Blue J while running the compiled program. These errors were easy to find since Blue J
showed it to us and was easy to debug. These type of errors were debugged by writing the
proper keywords required. In above error I forgot to write “super.” Before calling the
display() method form the super class.

17
Satish Nepali
CS4001NA Programming

7. Conclusion
The course was given to us by our module teacher on Dec 06 -2019 and the deadline
given was Jan 17-2020. There was a lot of time given to us for the completion of the
coursework and the coursework was also a bit tough. In this coursework we had to develop a
program that would be used to hire the staff for different organizations. The program
contained of three classes where there was one super class and two sub classes. With the use
of the three class the program was used to hire full time and part time staff and store the
information about the staff.
The concept used in the development of the program was inheritance, method
overriding and encapsulation. For completing the course work we were needed to have
knowledge about these concepts. So for gaining the proper knowledge to I went through the
lecture slides provided by our module leader. I also did some research on the internet to gain
some extra knowledge to do the course work.
After completing the coursework I gained the experience on real time programming.
It helped me to learn more about the inheritance, method overriding and encapsulation. It also
helped me in remembering the codes and their syntax and their uses. The coursework made
me realize about the things I was not clear about. The coursework not only helped me in
remembering the codes, their uses and their syntax but also gave me the experience on
writing the pseudo codes. After doing this course work I learned how to find the errors and
how to fix them.
Therefore the coursework was the best way and platform for me to gain the
knowledge and the experience required on programming in java. Since it helped me to gain a
lot of knowledge and gain real time experience about developing a program using java. I
would like to thank our module teacher for giving this coursework to us.

18
Satish Nepali
CS4001NA Programming

8. Appendix
1) StaffHire class
class StaffHire{ //Declaring a class StaffHire.
//Declaring three private instance variables
private int vacancyNumber;
private String designation;
private String jobType;
/*Creating a parameterized constructor and assigning the parameter's value
*in instance variables respectively*/
public StaffHire(int vacancyNumber,String designation,String jobType){
this.vacancyNumber=vacancyNumber;
this.designation=designation;
this.jobType=jobType;
}
//Creating accessor methods for each variables
public int getVacancyNumber(){
return vacancyNumber;
}
public String getDesignation(){
return designation;
}
public String getJobType(){
return jobType;
}
//creating mutator method for each variables
public void setVacancyNumber(int vacancyNumber){
this.vacancyNumber=vacancyNumber;
}
public void setDesignation(String designation){
this.designation=designation;
}

19
Satish Nepali
CS4001NA Programming

public void setJobType(String jobType){


this.jobType=jobType;
}
//creating the method display for displaying the variables
public void display(){
System.out.println("Vacancy Number: "+vacancyNumber);
System.out.println("Designation : "+designation);
System.out.println("Job Type: "+jobType);
}
}

2) FullTimeStaffHire
//Declaring sub-class FullTimeStaffHire where super-class is StaffHire
public class FullTimeStaffHire extends StaffHire{
//Declaring the attributes of the class FullTimeStaffHire
private int salary;
private int workingHour;
private String staffName;
private String joiningDate;
private String qualification;
private String appointedBy;
private boolean joined;
/*Creating a parameterized constructor FullTimeStaffHire with parameters vacancy
number,Designation,
* job type,salary,working Hour*/
public FullTimeStaffHire(int vacancyNumber,String designation,String jobType,int
salary,int workingHour){
super(vacancyNumber,designation,jobType);//calling constructor form super class
//assigning values of parameters salary and workingHour to the attributes
this.salary=salary;
this.workingHour=workingHour;
//assining the attributes as empty string

20
Satish Nepali
CS4001NA Programming

staffName="";
joiningDate="";
qualification="";
appointedBy="";
//assigning the joined attribute as false
joined=false;
}
//Creating an accessor method for all attributes attributes
public int getSalary(){
return salary;
}
public int getWorkingHour(){
return workingHour;
}
public String getStaffName(){
return staffName;
}
public String getJoiningDate(){
return joiningDate;
}
public String getQualification(){
return qualification;
}
public String getAppointedBy(){
return appointedBy;
}
public boolean getJoined(){
return joined;
}
//creating method to set salary having parameter newSalary

21
Satish Nepali
CS4001NA Programming

public void setSalary(int newSalary){


//Assigning the value of newSalary to the attribute salary if joined is false else
displaying salary can't be changed
if(joined==false){
salary=newSalary;
} else {
System.out.println("Staff has joined it is therefore not possible to change the salary");
}
}
//creating a method to set woking Hour having parameter newWorkingHour
public void setWorkingHour(int newWorkingHour){
this.workingHour=newWorkingHour;//Assining the value of newWorkingHour to
attribute workingHour
}
//Creating method to hire full-time staff
public void hireFullTimeStaff(String staffName,String joiningDate,String
qualification,String appointedBy){
/*Display staffName and joiniingDate if joined is true
* else assign the values of the paremeters to respective attributes
* and change joined to true.
*/
if(joined==true){
System.out.println(staffName+" you were hired on "+joiningDate);
} else{
this.staffName=staffName;
this.joiningDate=joiningDate;
this.qualification=qualification;
this.appointedBy=appointedBy;
joined=true;
}
}

22
Satish Nepali
CS4001NA Programming

//Creating a method to display the details of the employees


public void display(){
super.display();//calling ethod Display() from super class
//if joined is true display the attributes
staffName,salary,workingHour,joiningDate,qualification,appointedBy
if(joined==true){
System.out.println("Staff Name: "+staffName);
System.out.println("Salary: "+salary);
System.out.println("WorkingHour: "+workingHour);
System.out.println("Joining Date: "+joiningDate);
System.out.println("Qualification: "+qualification);
System.out.println("Appointed by: "+appointedBy);
}
}
}

3) PartTimeStaffHire
//Declaring part time staff hire class as sub class of staff hire
public class PartTimeStaffHire extends StaffHire{
//declaring the attrubutes of class part time staff hire
private int workingHour;
private int wagesPerHour;
private String staffName;
private String joiningDate;
private String qualification;
private String appointedBy;
private String shifts;
private boolean joined;
private boolean terminated;
/*creating parameterized constructor with parameters
* vacancy number,designation,job type,working hour,
* wages per hour and shifts*/

23
Satish Nepali
CS4001NA Programming

public PartTimeStaffHire(int vacancyNumber,String designation,String jobType,int


workingHour,int wagesPerHour,String shifts){
super(vacancyNumber,designation,jobType);//calling constructor from super class
//assignig value of paremeters into respective attributes
this.workingHour=workingHour;
this.wagesPerHour=wagesPerHour;
this.shifts=shifts;
//assigning the string attributes with empty string value
staffName="";
joiningDate="";
qualification="";
appointedBy="";
//assigning the boolean attributes with value false
joined=false;
terminated=false;
}
//creating an accessor method for all attributes of the class
public int getWorkingHour(){
return workingHour;
}
public int getWagesPerHour(){
return wagesPerHour;
}
public String getStaffName(){
return staffName;
}
public String getJoiningDate(){
return joiningDate;
}
public String getQualification(){
return qualification;

24
Satish Nepali
CS4001NA Programming

}
public String getAppointedBy(){
return appointedBy;
}
public String getShifts(){
return shifts;
}
public boolean getJoined(){
return joined;
}
public boolean getTerminated(){
return terminated;
}
//creating a method to set shifts with parameter newShifts
public void setShifts(String newShifts){
//if joined is false value of newShifts is assigned to shifts else not
if(joined==false){
shifts=newShifts;
}
}
/*Creating a method HirePartTimeStaff taking parameters
* Staff name, joining date, qualification and appointed by*/
public void hirePartTimeStaff(String staffName,String joiningDate,String
qualification,String appointedBy){
/*Dispaly the staffName and joiningDate if joined is true
* else assign the values of parameters to respective attributes
* and assign joined as true and terminated as false*/
if(joined==true){
System.out.println(staffName+" joined on "+joiningDate);
} else{
this.staffName=staffName;

25
Satish Nepali
CS4001NA Programming

this.joiningDate=joiningDate;
this.qualification=qualification;
this.appointedBy=appointedBy;
joined=true;
terminated=false;
}
}
//a method is created to terminate the staff
public void terminateTheStaff(){
/*if termination is true a message is displayed
* else the staffName,joiningDate,qualification,appointed by are set as empty string
* joined is set as false and terminated is set as true*/
if(terminated==true){
System.out.println("The staff has been already terminated");
} else{
staffName="";
joiningDate="";
qualification="";
appointedBy="";
joined=false;
terminated=true;
}
}
//creating a method Display to display the values of attributes
public void display(){
super.display();//calling method Display() from super class
//if joined is equal to true the display all attributes and incomeper day
if(joined==true){
System.out.println("Staff name: "+staffName);
System.out.println("Wages per hour: "+wagesPerHour);

26
Satish Nepali
CS4001NA Programming

System.out.println("Working hour: "+workingHour);


System.out.println("Joined Date: "+joiningDate);
System.out.println("Qualification: "+qualification);
System.out.println("Appointed by: "+appointedBy);
System.out.println("Income per day: "+wagesPerHour*workingHour);
}
}
}

27
Satish Nepali
CS4001NA Programming

9. References
ADMIN. (2015, July 23). JAVA / J2EE. Retrieved from Java Advantages and Disadvantages:
https://www.mindsmapped.com/java-advantages-and-disadvantages/
Pankaj. (2019, Sep 13). JournalDev. Retrieved from What is Java Programming Language?:
https://www.journaldev.com/32975/what-is-java-programming-language
Paradigim, V. (2017, Sep 28). Visual Paradigim. Retrieved from What is Class Diagram?:
https://www.visual-paradigm.com/guide/uml-unified-modeling-language/what-is-
class-diagram/

28
Satish Nepali

You might also like