Professional Documents
Culture Documents
Author(s)
Authorized by
Creation/Revision Date
Version
S.Meenakshi
Lesitha Mary James
Mr. Srikantan Moorthy
Mar-2009/Aug-2009
1.2
CONFIDENTIAL
COPYRIGHT NOTICE
All ideas and information contained in this document are the intellectual property of
Education and Research Department, Infosys Technologies Limited. This document is
not for general distribution and is meant for use only for the person they are
specifically issued to. This document shall not be loaned to anyone, within or outside
Infosys, including its customers. Copying or unauthorized distribution of this
document, in any form or means including electronic, mechanical, photocopying or
otherwise is illegal.
Education and Research Department
Infosys Technologies Limited
Electronic City
Hosur Road
Bangalore - 561 229, India.
Tel: 91 80 852 0261-270
Fax: 91 80 852 0362
www.infosys.com
mailto:E&R@infosys.com
CONFIDENTIAL
Date
Mar-2009
1.1
Jun-2009
1.2
Aug-2009
ER/CORP/CRS/ LA1026
i
Author(s)
Vani Vasudevan
Lesitha Mary James
S.Meenakshi
Lesitha Mary James
Reviewer(s)
Meenakshi S
Nishit Goel
Nishit Goel
S.Meenakshi
Lesitha Mary James
Nishit Goel
CONFIDENTIAL
Comments
Restructure 2009
Changes made as per
feedback from pilot batch
rollout
Modified
assignments
based on case study
Contents
ER/CORP/CRS/ LA1026
CONFIDENTIAL
1 of 68
ER/CORP/CRS/ LA1026
CONFIDENTIAL
2 of 68
Context
This document contains assignments to be completed as part of the hands on for the subject
Introduction to Object Oriented Programming using Java.
Note: In order to complete the course, assignments in this document must be completed in
the sequence mentioned.
Day 1 Assignments
Assignment 1: Case Study
Objective: Analysis of the case study and identify the complex system attributes and using
OO features to overcome the same.
Problem Description: Course Management System
A Course Management System needs to be developed for an engineering college. The college
wants an automated system to replace its manual system for purpose of allocation of students
to courses and lecturers to courses. There are 10000 students and 500 faculty members in the
college. The engineering college provides graduation and post graduation in the various
branches of engineering.
In any branch, the entire duration is divided into semesters. Each semester includes various
courses. Students admitted into the college can apply for various courses. The courses are
elective in nature. The student needs to select two electives. The electives are offered from
5th semester onwards for graduate students and from 3rd semester onwards for the post
graduate students. The graduate students need to undergo a final year project in the fourth
year of engineering whereas the post graduate student undergoes a thesis submission in the
second year. The complete list of courses, lecturers and students is maintained in the college.
The list is constant and cannot be updated by the system. The automated system uses this list
for allocation of courses.
The lecturers can be categorized into full time lecturers and visiting members. The full time
lecturers have target days and are paid salary every month. The visiting members get paid per
lecture basis and do not have any target days.
The system is used by the admin staff to register students admitted to college to the courses
opted. The system needs to be authenticated with a login id and password. The system would
be used by the student to view the marks and the attendance details also. The system is used
by the HOD (Head of the department) to allocate courses to the lecturers. At the beginning of
the semester, the HOD will do the allocation of lecturers to the classes for his department.
The HOD may make changes in the allocation during the progress of the course. The system
maintains a history of all the lecturers who have handled the courses throughout the semester
.The lecturer will use the system to update the marks of the student (project, assignment,
ER/CORP/CRS/ LA1026
CONFIDENTIAL
3 of 68
internal test marks and the semester end examination marks). The lecturer will also mark
the attendance of the students through the system.
In addition to the above, the system also keeps track of the residential status of the student.
The student may be a host-elite or a day scholar. If he is host-elite, the system will maintain
his / her hostels name, room number and the fees pertaining to the same.
Answer the following:
a. Identify two cases of hierarchy in the given problem statement
b. Identify the common behavior among the day scholar, host-elite, graduate and post
graduate students
c. Identify the different layers possible in this system in terms of the users of the systems
d. The admin staff wants to generate a report containing the scores of students in a batch in
the following manner:
assignment.
a. Identify the attributes of the Student class. What are the details that can be exposed and
what details can be hidden?
b. Consider the calculation of grade for the Student. There are several student types. Let us
assume that we need to write a method for the calculation. How many methods would be
written? Will they have the same name and signature? Will the functionality /code in
these functions remain same?
c. The fees have to be paid by the host-elite and by the day scholars. The host-elites need to
pay hostel fees along with semester fees. The day scholars need to pay only semester
fees. How many function/s would you code in the Student class and why?
d. For the options a,b and c identify the OOP features that would be implemented
Estimated time: 20 mins
Summary of this assignment:
ER/CORP/CRS/ LA1026
CONFIDENTIAL
4 of 68
1
2
7
3
9
4
ER/CORP/CRS/ LA1026
CONFIDENTIAL
5 of 68
ER/CORP/CRS/ LA1026
CONFIDENTIAL
6 of 68
Step 4: Go to File menu and select Save As menu option to save the file as Welcome.java in
the chosen folder (This would enable the java color setting for the code to be typed)
Step 6: Compile the program (press Ctrl + 1 or go to Tools menu and select Compile Java)
ER/CORP/CRS/ LA1026
CONFIDENTIAL
7 of 68
ER/CORP/CRS/ LA1026
CONFIDENTIAL
8 of 68
Note: Accessing an uninitialized local variable will result in a compiletime error. If you cannot initialize your local variable where it is
declared, make sure to assign a value before you attempt to use it.
Assignment 6: Operators
Objective: Understand the various operators in Java
Problem Description: To implement and understand the usage of modulus operator, string
concatenation, equality operator and logical operators in Java.
Step 1: Type the below program in the textpad, save, compile and execute
// Program to understand different types of operators
class Operator{
public static void main(String[] args){
int intVal=10;
float floatVal=3.0f;
boolean bool1 = true;
boolean bool2 = false;
boolean bool3 = true;
ER/CORP/CRS/ LA1026
CONFIDENTIAL
9 of 68
(intVal % floatVal));
System.out.println();
// String Concatenation : Observe the difference
System.out.println("String Concatenation");
System.out.println("Day " + 2 + " Session");
System.out.println("\n" + 2 + 3 +"\n" +(2+3));
System.out.println();
// Relational: Equality operator
System.out.println("Relational");
System.out.println(intVal + " == " + floatVal + " = "
+(intVal==floatVal));
floatVal = 10.0f;
System.out.println(intVal + " == " + floatVal + " = "
+(intVal==floatVal));
System.out.println();
bool2 = false;
if(bool1 || (bool1 && (bool2=false))){
System.out.println("Success");
}
else{
System.out.println("Failure");
}
System.out.println("bool2 value : " + bool2);
}
}
Estimated time: 25 mins
Summary of this assignment:
In this assignment, you have learnt
Usage of some operators available in Java
ER/CORP/CRS/ LA1026
CONFIDENTIAL
Failure
bool2 value : true
Press any key to continue . . .
Step 3: Make changes to the code so that the output reads :
Success
bool2 value : true
Press any key to continue . . .
Note: The logical operators and the Boolean operators may be changed to achieve this
result. Although the if control structure is used here, the focus is on the operators and
Boolean datatype
class Operator{
public static void
boolean bool1
boolean bool2
boolean bool3
main(String[] args){
= false;
= true;
= true;
ER/CORP/CRS/ LA1026
CONFIDENTIAL
Day 2 Assignments
Assignment 1: Typecasting
Objective: Understand Conversion and Typecasting
Problem Description: Modify Assignment No. 8 of Day 1 so that the addition of the numbers is
possible and can be stored and displayed.
Estimated time: 15 minutes
Summary of this assignment:
In this assignment, you have learnt
Typecasting Data types
ER/CORP/CRS/ LA1026
CONFIDENTIAL
Program 2:
//Program to understand the loop and continue
class ControlContinue{
public static void main(String args[]){
boolean bool = true;
for(int i= 0; i<5 ; i++){
for(int j = 0; j<10; j++){
System.out.print(j +"\t");
if(j > 5){
System.out.println();
ER/CORP/CRS/ LA1026
CONFIDENTIAL
continue;
}
}
System.out.println("Outer Loop");
}
System.out.println("End");
}
}
Note:
You cant use a number or anything that does not evaluate to a
Boolean value as a condition in selection / iteration statement
Grade
A
B
C
D
E
Range of marks
80-100
73-79
65-72
55-64
<55
ER/CORP/CRS/ LA1026
CONFIDENTIAL
Step 4: Change the values of the grade variable and test the working of the program for all
the grades.
Estimated time: 20 minutes
Summary of this assignment:
In this assignment, you have learnt
Switch case statements
Note:
Using a class , you can create any number of objects
For every instance variable there should be a setter and getter
method associated to it. Setter method is to set the value of the
instance variable and getter method is to return the value of the
instance variable
studentType instance variable can have F (fresher) or L (lateral)
ER/CORP/CRS/ LA1026
CONFIDENTIAL
c.
Invoke the corresponding setter methods to set the instance variable with the
given values stored in temporary variables.
d. Using getter methods, display the student details.
Step 4: Compile the program, fix the errors if any
Step 5: Execute the program and verify the output
Estimated time: 30 mins
Note:
Create a folder for storing every assignment
Summary of this assignment:
In this assignment, you have learnt
How to implement ADT in Java
How to implement getter and setter methods
How to create reference variables
How to create an object
ER/CORP/CRS/ LA1026
CONFIDENTIAL
ER/CORP/CRS/ LA1026
CONFIDENTIAL
ER/CORP/CRS/ LA1026
CONFIDENTIAL
ER/CORP/CRS/ LA1026
CONFIDENTIAL
}
public boolean Method2(int j)
{
if (j>=14000 && j<20000)
{
return true;
}
else
{
return false;
}
}
}
Estimated time: 15 minutes
Summary of this assignment:
In this assignment, you have learnt
Coding standards
ER/CORP/CRS/ LA1026
CONFIDENTIAL
studentName=firstName+lastName
o displayDetails (Student obj):Should accept the object as an argument
and display the details like studentId, studentType, studentName of that
object
Step 2: Define a main method ,
In the main method
o Create an object of Student(D,Bony,Thomas) and assign it to
reference variable studentOne;
o Call the displayDetails() method and display the details of studentOne
o Create an object of Student(H,Dinil,Bose) and assign it to
reference variable studentTwo;
o Call the displayDetails() method and display the details of studentTwo
Step 3: Compile the program, fix the errors if any
Step 4: Execute the program and verify the output
Estimated time: 20 mins
Summary of this assignment:
In this assignment, you have learnt
How to use this, passing object to an argument, Overloaded constructors
Self Review
Predict the output of the following programs and analysis your answer by executing the code:
1.
class TypeCasting{
public static void main(String args[]){
int intVal=10;
double doubleVal= 20.0;
// Explicit Casting is required
intVal = doubleVal;
System.out.println(intVal);
}
}
2.
class Demo{
public static void main(String args[]){
if(1){
System.out.println("True Block");
}
else{
System.out.println("False Block");
}
}
ER/CORP/CRS/ LA1026
CONFIDENTIAL
}
Note : You can only the Boolean type in the conditional statement
0 wont be interpreted as false and nonzero (>0) wont be interpreted
as true.
3.
class Example{
public static void main(String[] args){
int intValOne = 0;
int intValTwo = 0;
for(short index=0; index < 5; index++){
if((++intValOne > 2) || (++intValTwo > 2)){
intValOne++;
}
}
System.out.println(intValOne + " " + intValTwo);
}
}
4.
class Example{
public static void main(String[] args){
int intValOne = 5,intValTwo = 7;
System.out.println((( intValTwo * 2) % intValOne));
System.out.println(intValTwo % intValOne);
}
}
5.
class Example{
public static void main(String[] args){
int val1 = 9;
int val2 = 6;
for (int val3 = 0; val3 < 6; va13++,val2--){
if(val1 > 2){
val1--;
}
if(val1 > 5){
System.out.print(val1 + );
--val1;
ER/CORP/CRS/ LA1026
CONFIDENTIAL
continue;
}
val1--;
}
}
}
7.
Find the output of the following program:
public class switchCase {
public static void main(String args[])
{
String comment;
int val = (int)(4 * 3);
switch (val) {
case 0: comment = "Case
break;
case 1: comment = "Case
break;
case 2: comment = "Case
break;
default: comment = "Oops
}
System.out.print(comment);
}
0";
1";
2";
-- something is wrong ";
Day 3 Assignments
Assignment 1:Static variable, block and method
Objective: Understand the static variable, static block and static method
Problem Description:
Modify the Assignment 10 of Day 2 to get the total number of student objects
Static variable and static method:
ER/CORP/CRS/ LA1026
CONFIDENTIAL
Step 1: Add static data member studentCount to store the total number of
objects of
type Student
Step 2: Add static member method getStudentCount () to return the value
studentCount.
Step 3: In default constructor Student(), increment the studentCount by 1 and assign
to studentId
Static block:
Step 4: Assign studentCount = 10 (as already 10 students have enrolled)
Step 5: Save and Compile the program, fix the errors if any
Step 6: Execute the program and verify the output
Estimated time: 20 mins
Summary of this assignment:
In this assignment, you have learnt
How to use static variable, method and block
Assignment 2: 2D arrays
Objective: Understand 2D arrays and its working
Problem Description: Create class DemoArray to add 2D arrays. Write the business logic to
display the sum of the matrices. Below mentioned are a few guidelines that can be followed
Initialize the 2D arrays as follows:
int firstArrayMatrix[][]={{1,1,1},{1,1,1}};
int secondArrayMatrix[][]={{2,2,2},{2,2,2}};
ER/CORP/CRS/ LA1026
CONFIDENTIAL
Tony
Thomas
Dinil
JAVA
JAVA
Linux
C
UNIX
Oracle
C++
Delvin
RDBMS
C#
ORACLE
Initialize the array based on the above table and display the details of Delvin inside the main
method written in a class JaggedArray
Estimated time: 20 min
Summary of this assignment:
In this assignment, you have learnt
Multidimensional Array
Assignment 4:Strings
Objective: To understand the concept of strings
Problem Description:
Enhance the Assignment 5 of Day 2 and do the necessary modification as mentioned below:
Student
-studentId : integer
-studentType:char
-studentName:String
+Student()
+Student(id,type,firstName,lastName)
+setStudentId(id:int):void
ER/CORP/CRS/ LA1026
CONFIDENTIAL
Version No. 1.2 25 of 68
+setStudentType(type:char):void
+setStudentName(firstName:String,lastName:String)
+getStudentId():int
ER/CORP/CRS/ LA1026
CONFIDENTIAL
ER/CORP/CRS/ LA1026
CONFIDENTIAL
Student
-studentId : integer
-studentName:String
-residentialStatus:String
- feesPerMonth: long int
+Student()
+setStudentId(id:int):void
+setStudentName(firstName:String,lastName:String)
+setResidentialStatus(status:String)
+getStudentId():int
+getStudentName():String
+getResidentialStatus():String
+getFees():long int
Step 1: Through the command line argument get the value of studentId, studentName
residentialStaus, Semester fees and Hostel fees(only if the residential status is Hostelite)
Step 2: Set the value of the data members with the help of the setter method
Step 3: In the main method, create a reference variable student of type Student.
a. Using getter methods, display the student details
Step 4: Compile the program, fix the errors if any
Step 5: Execute the program and verify the output
Estimated time: 20 mins
Summary of this assignment:
In this assignment, you have learnt
How to implement command line argument and parseInt()method
ER/CORP/CRS/ LA1026
CONFIDENTIAL
Student
-studentId : integer
-studentType:char
-studentName:String
-residentialStatus:String
- feesPerMonth: double
+Student()
+Student(id,type,firstName,lastName)
+setStudentId(id:int):void
+setStudentType(type:char):void
+setStudentName(firstName:String,lastName:String)
+setResidentialStatus(status:String)
+getStudentId():int
+getStudentType():char
+getStudentName():String
+getResidentialStatus():String
+getFees():String
+calculateFees(semesterFees:double):void
+calculateFees(semesterFees:double,hostelFees:double):void
ER/CORP/CRS/ LA1026
CONFIDENTIAL
Note:
Use boolean equalsIgnoreCase(String anotherString)
Method to check the content of the string objects for equality
Estimated time: 40 mins
Summary of this assignment:
In this assignment, you have learnt
How to implement method overloading
Self Review
What would be the output of the following programs?
1.
class StringEqual{
public static void main(String arg[]){
String name1,name2;
name1 = "God";
name2= name1;
if(name1 == name2){
System.out.println("Equal");
}
else{
System.out.println("Not Equal");
}
}
}
2.
ER/CORP/CRS/ LA1026
CONFIDENTIAL
class StringEqual{
public static void main(String arg[]){
String name1,name2;
name1 = new String("God");
name2= new String("God");
if(name1 == name2){
System.out.println("Equal");
}
else{
System.out.println("Not Equal");
}
}
}
3.
class StringEqual{
public static void main(String arg[]){
String name1,name2;
name1 = new String("God");
name2= new String("God");
if(name1.equals(name2)){
System.out.println("Equal");
}
else{
System.out.println("Not Equal");
}
}
}
4.
class StaticDemo
{
static int count=10;
StaticDemo(){
count++;
}
static void display(){
System.out.println(count);
}
static{
System.out.println("Static Block written Before Main");
ER/CORP/CRS/ LA1026
CONFIDENTIAL
}
public static void main(String args[]){
StaticDemo t1=new StaticDemo();
StaticDemo t2=new StaticDemo();
StaticDemo.display();
t2.display();
}
static{
System.out.println("Static Block written After Main");
}
}
5.
class MultiDimArray {
public static void main(String[] args) {
String[][] names = {{"Mrs.","Ms." , "Mr."},
{"Vani","Lesitha","Nishit","Meenakshi"}};
System.out.println(names[0][0] + names[1][0]);
System.out.println(names[0][2] + names[1][2]);
}
}
6.
class StaticDemo1{
private int num;
static void display(){
System.out.println(num);
}
ER/CORP/CRS/ LA1026
CONFIDENTIAL
area);
}
}
public class Square {
int length;
void display(int length){
this.length=length;
}
int calculateArea(){
return(length*length);
}
}
Day 4 Assignments
Assignment 1: Inheritance
Objective: Understand the concept of Inheritance
Problem Description:
To implement the following class diagram
Student
-studentId : integer
-studentType:char
-studentName:String
-feesPerMonth: double
+Student()
+Student(id,type,firstName,lastName)
+setStudentId(id:integer):void
+setStudentType(type:char):void
+setStudentName(firstName:String,lastName:String)
+setFeesPerMonth(double)
+getStudentId():integer
+getStudentType():char
+getStudentName():String
+getStudentType():char
+getStudentName():String
+getFeesPerMonth():double
ER/CORP/CRS/ LA1026
CONFIDENTIAL
" +
Hostelite
-hostelName : String
-roomNumber : integer
+Hostelite()
+Hostelite(integer,char,String,double,String,integer)
+getHostelName():String
+getRoomNumber():integer
Step 1: Create a class Hostelite by reusing Student class.
Step 2: Implement all the methods specified in the Hostelite and store it in Hostelite.java
Step 3: In main method,
a. Create an object of type Hostelite by invoking the parameterized constructor
with the required parameters.
b. Display all the details of Hostelite (id, type, name, feesPerMonth, hostelName,
roomNo) by invoking appropriate getter methods.
Step 4: Compile the program, fix the errors if any
Step 5: Execute the program and verify the output
Estimated time: 25 mins
Summary of this assignment:
In this assignment, you have learnt
How to implement simple inheritance.
ER/CORP/CRS/ LA1026
CONFIDENTIAL
Student
#studentId : integer
#studentType:char
#studentName:String
#feesPerMonth: double
+Student()
+Student(id,type,firstName,lastName)
+setStudentId(id:integer):void
+setStudentType(type:char):void
+setStudentName(firstName:String,lastName:String)
+setFeesPerMonth(double)
+getStudentId():integer
+getStudentType():char
+getStudentName():String
+getStudentType():char
+getStudentName():String
+getFeesPerMonth():double
Hostelite
-hostelName : String
-roomNumber : integer
+Hostelite()
+Hostelite(integer,char,String,double,String,integer)
+getHostelName():String
+getRoomNumber():integer
ER/CORP/CRS/ LA1026
CONFIDENTIAL
ER/CORP/CRS/ LA1026
CONFIDENTIAL
Student
#studentId : integer
#studentType:char
#studentName:String
#feesPerMonth: double
+Student()
+Student(id,type,firstName,lastName)
+setStudentId(id:integer):void
+setStudentType(type:char):void
+setStudentName(firstName:String,lastName:String)
+setFeesPerMonth(double)
+getStudentId():integer
+getStudentType():char
+getStudentName():String
+getFeesPerMonth():double
+displayStudentDetails():void
Hostelite
DayScholar
-hostelName : String
-roomNumber : integer
+Hostelite()
-residentialAddress : String
+DayScholar()
+DayScholar(integer,char,String
,double,String)
+getResidentialAddress():String
+displayStudentDetails():void
CONFIDENTIAL
Version No. 1.2 37 of 68
+Hostelite(integer,char,String,double
,String,integer)
+getHostelName():String
+getRoomNumber():integer
ER/CORP/CRS/ LA1026
+displayStudentDetails():void
ER/CORP/CRS/ LA1026
CONFIDENTIAL
class Student{
protected int studentId;
protected char studentType;
protected String studentName;
protected int semesterFees;
protected int feesPerMonth;
Student(int studentId){
this.studentId=studentId;
}
Student(int studentId,char studentType,String studName,int
semFees){
super();
studentType=studentType;
studentName=studName;
semesterFees=semFees;
}
void displayDetails(){
}
}
class DayScholar extends Student{
private String residentialAddress;
DayScholar(int studentId,char studentType ,String
studName,int semFees,String residentialAddress){
this.residentialAddress=residentialAddress;
super(int studentId,char studentType,String
studName,int semFees);
}
void displayDetails(){
}
}
class CourseReg{
public static void main(String args[]){
DayScholar dayscholar = new DayScholar
(1001,'D',"Eugene",12000,"No 32/68
dayscholar.displayDetails();
}
}
Vijayanagar");
ER/CORP/CRS/ LA1026
CONFIDENTIAL
ER/CORP/CRS/ LA1026
CONFIDENTIAL
studentName=studName;
semesterFees=semFees;
}
}
class DayScholar extends Student{
private String residentialAddress;
DayScholar(int studentId,char studentType, String studName,
int semFees, String residentialAddress, String cName){
super(studentId,studentType,studName, semFees, cName);
this.residentialAddress=residentialAddress;
}
void displayDetails(){
//need to display the Details of the Student and
//DayScholar
}
}
class CourseReg{
public static void main(String args[]){
DayScholar dayscholar = new DayScholar
(1001,'D',"Eugene",12000,"No32/68 Vijayanagar","OOP");
dayscholar.displayDetails();
}
}
Debug the above code. Once done , follow the below stepsStep 1: In main method,
a. Create object of type Dayscholar by invoking the corresponding
constructor and assign it to reference variable dayscholar
b. Both the constructor of the Student class and CourseRegistration class
need to be invoked
c. Fill the displayDetails() of DayScholar class
d. dayscholar.displayDetails()
should
invoke
DayScholar
and
CourseRegistration displayDetails()
Step 2: Compile the program, fix the errors if any
Step 3: Execute the program and verify the output
Summary of this assignment:
In this assignment, you have learnt
Uses of super keyword
Estimated time: 25 mins
ER/CORP/CRS/ LA1026
CONFIDENTIAL
Student
#studentId
#studentType
#studentName
: int
:char
:String
+Student()
+Student(int,char,String)
#postCourseId
#postCourseName
#postCoursefees
PostGradStudent
: int
:String
:int
+PostGradStudent(int,String,int,int,char,String)
+getpostCourseId():int
+getpostCourseName:String
DayScholar
-residentialAddress : String
+DayScholar(int,String,int,char,String,String)
+calculateFees():void
+getFees():int
Step 1:In the Student class
ER/CORP/CRS/ LA1026
CONFIDENTIAL
Define constructor
o Student(): Used to increment the value of studentId each and every time
object is created. Hint : Use a static variable
o Student(sid,sType,sName): Used to initialize the instance variables
studentId,studentType,studentName and to invoke the constructor
Student()
Step 2:In the PostGradStudent class
Define constructor
o PostGradStudent(pCourseId,pCourseName,pCourseFees,
sid,sType,sName): Used to initialize the instance variables studentId ,
studentType studentName to invoke the constructor
Student(sid,sType,sName)
getter method
o Using getter methods, display the PostGraduation Student details.
Step 2:In the DayScholar class
Define constructor
DayScholar(pCourseId,pCourseName,pCourseFees, sid,sType,sName
,rAddress): Used initialize the instance variable residentialAddress and to
invoke the constructor PostGradStudent
(pCourseId,pCourseName,pCourseFees, sid,sType,sName)
calculateFees()
o calculate the fees as below.
calulateFees(){
fees= postCoursefees /6;
}
getter method
o Using getter methods, display the fees details.
Step 3:In the main method
Create
an
object
of
type
DayScholar
with
the
values(5,OOP,16000,1001,D,Manu,N0 32/3 Vijayanagar)and assign
it to reference variable named dayScholar
Call the corresponding methods
Step 4: Compile the program, fix the errors if any
Step 5: Execute the program and verify the output
Note:
super() can be used to invoke the base class constructor
ER/CORP/CRS/ LA1026
CONFIDENTIAL
ER/CORP/CRS/ LA1026
CONFIDENTIAL
super.getDetails();
System.out.println("Student Name "+studentName);
System.out.println("Residential
Address"+residentialAddress);
}
}
class DynamicEx{
public static void main(String args[]){
Student dayScholar=new DayScholar();
dayScholar.setStudentId(1001);
dayScholar.setStudentType('D');
dayScholar.setStudentName("Thomas");
dayScholar.setResAddress();
dayScholar.getDetails();
}
}
Note:
A new method defined in the sub class cannot be called using the
super class reference
ER/CORP/CRS/ LA1026
CONFIDENTIAL
Student
#studentId : integer
#studentType:char
#studentName:String
# semesterFees: int
#feesPerMonth:int
+Student()
+setStudentId(id:integer):void
+setStudentType(type:char):void
+setStudentName(firstName:String,lastName:String)
+calculateFees():void
+displayDetail():void
DayScholar
-residentialAddress : String
+DayScholar()
+DayScholar(integer,char,String,doubl
e,String)
+displayDaySDetails():void
Step
Step
Step
Step
ER/CORP/CRS/ LA1026
CONFIDENTIAL
ER/CORP/CRS/ LA1026
CONFIDENTIAL
Review
What would be output for the following programs? If there is an error in the program, analyze
it, fix it and execute it
1.
class Parent{
private int num;
public void setNum(int val){
num = val;
}
public int getNum(){
return num;
}
public void display(){
System.out.println("Number :"+num);
}
}
class Child extends Parent{
private int val;
ER/CORP/CRS/ LA1026
CONFIDENTIAL
ER/CORP/CRS/ LA1026
CONFIDENTIAL
3.
class Animal {
public static void testClassMethod() {
System.out.println("The class method in Animal.");
}
public void testInstanceMethod() {
System.out.println("The instance method in Animal.");
}
}
class Cat extends Animal {
public static void testClassMethod() {
System.out.println("The class method in Cat.");
}
public void testInstanceMethod() {
ER/CORP/CRS/ LA1026
CONFIDENTIAL
}
class Child extends Parent{
private int val;
Child(int num,int val){
// To invoke super class constructor
super(num);
this.val = val;
}
public int getVal(){
return val;
}
}
class CourseMgmt{
public static void main(String args[]){
Child child;
ER/CORP/CRS/ LA1026
CONFIDENTIAL
}
}
5.
public class ClassA {
public void methodOne(int i) {
}
public void methodTwo(int i) {
}
public static void methodThree(int i) {
}
public static void methodFour(int i) {
}
}
public class ClassB extends ClassA {
public static void methodOne(int i) {
}
public void methodTwo(int i) {
}
public void methodThree(int i) {
}
public static void methodFour(int i) {
}
}
Day 5 Assignments
Assignment 1: Package (Create, Compile & Execute )
Objective: Create and use Packages
Problem Description: To display student details using package program
Step1: Write the following program in a textpad
package courseregistration;
ER/CORP/CRS/ LA1026
CONFIDENTIAL
class Student {
protected int studentId;
protected char studentType;
protected String studentName;
public void setStudentId(int sId){
this.studentId=sId;
}
public void setStudentType(char sType){
this.studentType=sType;
}
public void setStudentName(String sName){
this.studentName=sName;
}
public void getDetails(){
System.out.println("Student ID :"+studentId);
System.out.println("Student Type :"+studentType);
System.out.println("Student Name :"+studentName);
}
}
class PackageEx{
public static void main(String args[]){
Student student=new Student();
student.setStudentId(1001);
student.setStudentType('D');
student.setStudentName("Thomas");
student.getDetails();
}
}
ER/CORP/CRS/ LA1026
CONFIDENTIAL
Step 3: If the command prompt does not appear during the compilation or an execution do
the (Step4 & Step 5)
Step 4: Go to configure tab and select the Preferences-Tools-Compile Java and select
Prompt for parameters
ER/CORP/CRS/ LA1026
CONFIDENTIAL
ER/CORP/CRS/ LA1026
CONFIDENTIAL
Step 7:Give ctrl+2 for run the program and give as below in the prompt
courseregistration/PackageEx
ER/CORP/CRS/ LA1026
CONFIDENTIAL
ER/CORP/CRS/ LA1026
CONFIDENTIAL
package studentpack;
public class Student {
int studentId;
private char studentType;
protected String studentName;
public void setStudentId(int sId){
this.studentId=sId;
}
public void setStudentType(char sType){
this.studentType=sType;
}
public void setStudentName(String sName){
this.studentName=sName;
}
}
package registrationpack;
class DayScholar extends Student{
private String residentialAddress;
public void setResAddress(String resAddress){
this.residentialAddress=resAddress;
}
public void getDetails(){
System.out.println("Student Id:"+studentId);
System.out.println("Student Type:"+studentType);
System.out.println("Student Name:"+studentName);
}
}
class Registration{
public static void main(String args[]){
DayScholar dayScholar=new DayScholar();
dayScholar.setStudentId(1001);
dayScholar.setStudentType('D');
dayScholar.setStudentName("Dinil");
dayScholar.getDetails();
}
}
Note: Compile the class Student separately in a package called courseregistration. There
is no error in this code. Compile the DayScholar and Registration classes separately in the
package registrationpack. There are some access related errors, analyse them, debug and
execute to get the output.
ER/CORP/CRS/ LA1026
CONFIDENTIAL
Assignment 3:Interfaces
Objective: Understand the significance of interface
Problem Description:
Type the following code, debug the code and execute the same.
interface InterOne{
int varOne;
int varTwo=100;
void methodOne();
void methodTwo();
}
class Example implements InterOne{
void methodOne(){
varTwo=varTwo+10;
System.out.println("Value of varTwo:"+varTwo);
System.out.println("Good Morning");
}
public void methodTwo(){
System.out.println("Good Afternoon");
}
}
class DemoInterface{
public static void main(String args[]){
Example obj=new Example();
obj.methodOne();
obj.methodTwo();
}
}
ER/CORP/CRS/ LA1026
CONFIDENTIAL
ER/CORP/CRS/ LA1026
CONFIDENTIAL
Step 1: Identify all the nouns i.e. the actors in the problem statement.
Step 2: Identify the activities i.e. the use cases. Connect the actors to the use cases.
Step 3: Identify the relationships.(Generalization, specialization, Includes)
Estimated time: 30 mins
Summary of this assignment:
In this assignment, you have learnt
How to analyze and design the Use case diagram for a given problem statement.
Step 1: Identify all the nouns (type of objects or classes) in the case study given in previous
case study.
Step 2: Identify the commonalities between classes (Generalization) if it is obvious.
Step 3: Identify all the member variables and methods the class should have.
Step 4: Identify the member variables and methods (getter and setter methods for each and
every attribute) for the rest of the classes.
Step 5: Ensure that the class is fully independent of other classes and contains all the
attributes and methods necessary.
Step 6: Keep all data members private (-) or protected (#).
Step 7: The methods in class should completely abstract the functionality using
encapsulation. The methods in the class should not be a force fit of procedural code into a
class.
Step 8: Inherit and extend classes from the base class ONLY IF situation has scope for it. Shift
the commonalities among classes to the base class
Step 9: Define the Has-A and Uses-A relationship among classes
(Is-A has been defined already in previous step)
ER/CORP/CRS/ LA1026
CONFIDENTIAL
Step 10: Keep the number of classes in your application under check (Do not create any
unnecessary classes)
Step 11: Review your design
Estimated time: 30 mins
Summary of this assignment:
In this assignment, you have learnt
How to analyze design a class diagram for the given problem statement.
CONFIDENTIAL
Self review
What would be the output of the following programs?
1.
abstract class Animal {
abstract static void testClassMethod();
public void testInstanceMethod() {
System.out.println("The instance method in Animal.");
}
}
class Cat extends Animal {
public static void testClassMethod() {
System.out.println("The class method in Cat.");
}
public void testInstanceMethod() {
System.out.println("The instance method in Cat.");
}
public static void main(String[] args) {
Cat myCat = new Cat();
Animal myAnimal = myCat;
Animal.testClassMethod();
myAnimal.testInstanceMethod();
}
}
2.
interface Count {
short counter=0;
void countUp();
}
public class TestCount implements Count{
public static void main(String args[]){
TestCount test=new TestCount();
test.countUp();
ER/CORP/CRS/ LA1026
CONFIDENTIAL
}
public void countUp(){
for (int x=6;x>counter;x--,++counter){
System.out.println(" "+counter);
}
}
}
3.
interface ParentOne{
int pOne=1;
void printParentOne();
}
interface ParentTwo{
int pTwo=2;
void printParentTwo();
}
interface Child extends ParentOne, ParentTwo{
int child=3;
void printChild();
}
class InheritClass implements Child{
public void printParentOne(){
System.out.println(pOne);
}
public void printParentTwo(){
System.out.println(pTwo);
}
public void printChild(){
System.out.println(child);
}
}
class TestInterface{
public static void main(String args[]){
InheritClass inclass=new InheritClass();
inclass.printParentOne();
inclass.printParentTwo();
inclass.printChild();
}
}
ER/CORP/CRS/ LA1026
CONFIDENTIAL
ER/CORP/CRS/ LA1026
CONFIDENTIAL
MODE OF TRAVEL
F(FOR FLIGHT)
500
T(FOR TRAIN)
100
B(FOR BUS)
50
ER/CORP/CRS/ LA1026
CONFIDENTIAL
Here,
o YYY denotes travel company code 115
o X denotes mode of travel F-Flight, T-Train, B-Bus
o NNN denotes Flight/Train/Bus Code such as 101-105 for
flight,201-205 for Train and 301-305 for Bus
o ZZZZ denotes a unique code, which starts from 0001 in
a branch and increments sequentially by 1 for each
booking request
Step 3: Test the code by providing the following inputs to the fields as follows:
SE NO.
VALUES VALUES
FOR X
NNN
FOR VALUES
TO
BE
GENERATED FOR ZZZZ
CASE 1
102
0001
CASE 2
202
0002
CASE 3
304
0003
Note: The values for the different fields may be provided through the program itself
statically.
Estimated time: 20 minutes
Summary of this assignment:
In this assignment, you have learnt
If else conditional statements and Strings
ER/CORP/CRS/ LA1026
CONFIDENTIAL
ER/CORP/CRS/ LA1026
CONFIDENTIAL