OOP 1-2 assignment

public class Demo {
 class Student {
 private int studentId;
 private float qualifyingExamMarks;
 private char residentialStatus;
 private int yearOfEngg;
 public float getQualifyingExamMarks() {
  return qualifyingExamMarks;
 }
 public void setQualifyingExamMarks(float qualifyingExamMarks) {
  this.qualifyingExamMarks = qualifyingExamMarks;
 }
 public char getResidentialStatus() {
  return residentialStatus;
 }
 public void setResidentialStatus(char residentialStatus) {
  this.residentialStatus = residentialStatus;
 }
 public int getStudentId() {
  return studentId;
 }
 public void setStudentId(int studentId) {
  this.studentId = studentId;
 }
 public int getYearOfEngg() {
  return yearOfEngg;
 }
 public void setYearOfEngg(int yearOfEngg) {
  this.yearOfEngg = yearOfEngg;
 }
}
 public static void main(String[] args){
  Student student = new Student();
  student.setStudentId(1001);
  student.setQualifyingExamMarks(95.0f);
  student.setResidentialStatus('D');
  student.setYearOfEngg(2);
  System.out.println("Student Id    :" + student.getStudentId());
  System.out.println("Qualifying Marks   :" + student.getQualifyingExamMarks());
  System.out.println("Qualifying Marks   :" + student.getResidentialStatus());
  System.out.println("Current Year of Engineering :" + student.getYearOfEngg());
 }
}

Assignment 3: Understanding Java architecture
Objective: Understand the creation of byte codes and .class files
Problem Description: Java programs are compiled to get the byte codes, which are verified
and executed by the JVM. Consider the Java program created in Assignment 1 of Day 2 to
explore and understand byte codes.
Perform the following steps:
Step 1: Go to the path where the Demo.java file is stored and answer the following questions:
a) How many class files do you see?
b) How many source files (.java) do you see?
c) Try opening the .class file using TextPad, What do you observe?
Step 2: Write the verification process for byte code
a) Open the .class file and make the modification (add a space after the last line) and
save it.
Note: The .class file can be opened in TextPad, the content of .class file is not in
readable format
b) Without compiling execute the same program. What do you observe?
c) Compile and execute the program what is difference you could observe?
Step 3: Remove the Student class written in Assignment 1 of Day 2 from Demo.java file and
place it into another file called Student.java. Compile Student.java and Demo.java
(containing the Demo class with the main method) separately and Execute Demo.java. Repeat
Step 1 and answer the questions. Is there any difference in the answers?

Assignment 4: Control structures and operators
Objective: Write programs to understand Control statements, Operators and boolean data type
Problem Description: The validation of the examination marks is to be done. The range of exam marks is between 65 and

100 (both inclusive). This is to be done by including a validate method in Student class.
Modify the code written in Student Class to implement selection statements using if-else and logical operators
Modified Student class diagram:
+validateExamMarks():boolean
Step 1: Modify the Student class created (Student.java) in Assignment No. 3 of Day 2 to include the method whose

prototype is given above
Step 2: The implementation of the method is as follows:
a. validateExamMarks()
i. If the qualifyingExamMarks is greater than or equal to 65 and less than or equal to 100, return true
ii. If not, return false
Step 3: Add the following statements to the existing main method written in Demo class (Demo.java)
a. Invoke the validateExamMarks() method
b. If the qualifyingExamMarks are valid,
a. Display “Valid Marks”
b. If not valid, display “Invalid marks, the range of the qualifying exam marks is between 65 and 100”
Step 4: Compile the program, fix the errors if any
Infosys Technologies Limited Object Oriented Programming using Java
ER/CORP/CRS/ LA1026 CONFIDENTIAL Version No. 2.0 15 of 77
Step 5: Execute the program and verify the output

Assignment 5: Control Structures

Objective: Creation of a Registration Class and instantiation of objects

Problem Description: Every student has to register and pay appropriate fees at the start of every academic year of engineering. The fees to be paid is calculated based on branch selected and discount based on qualification marks.

Implement the following class diagram using Java.

 

Registration

-registrationId:int

-fees:double

+set

+set

+get

+get

+calculateFee(int):double

Step 1: Create a file called Registration.java using TextPad

Step 2: Define the class Registration as per the class diagram specified

Step 3: Define all the getter and setter methods of Registration Class

Step 4: The implementation of calculateFees() method is done based on the marks which is passed as an argument to the method. The details are given in Table 1:

Table 1:

Range of marks

Discount in %

85-100

12

75-84

7

65-74

0

 

i. Declare a local variable with the name discount of type integer to store the discount identified as per the Table 1

ii. Calculate the fees(instance variable of Registration class) after giving the discount as follows:

fees = fees - (fees * (discount/100))

iii. Return the calculated fees

 

Step 4: Write a class called DemoReg in a file DemoReg.java with the main method

Step 5: Create a reference variable of Registration class with the name reg and instantiate the same

a. Invoke the corresponding setter method to set the value for the instance variable as follows:

RegistrationId

 

2001

 

 

b. The fees is based on the Branch Id. Create a local variable called branchId in the main method and initialize to 1002. The logic for calculation of fees as per Branch Id is given in Table 2

 

 

 

 

Table 2

branchId

Fees

1001

25575.0

1002

15500.0

1003

33750.0

1004

8350.0

1005

20500.0

 

Write the logic for identifying the fees based on Branch Id using a switch case. (In the assignments of Day 3 we will accept the branch Id as an argument).Using the appropriate setter method, set the fees.

c. Invoke the calculateFees() with 79 as an argument for marks

d. Using the corresponding getter methods, and display the details as follows:

 

Registration Id :___________________

Fees :___________________

Step 5: Compile the program and execute the program

Step 6: Notice that the value of the discount calculated is not proper. Why?

Assignment 6:Type Casting

Objective: Understand type casting

Problem Description: In Assignment 5 of Day 2, the discount was not getting calculated.

Step 1: Modify the formula written inside the calculateFees() as follows to get the right output:

fees = fees - (fees * ((double)discount/100))

Compile and execute the program and you will notice that the discount calculation is taking place correctly.

Note:

 The discount calculation did not happen in Assignment 5 because the calculation discount/100 resulted in an integer value (there is loss of the precision part) and hence did not reflect in the output. In Assignment 6 the discount is type casted to double explicitly and hence the desired output is obtained

 

Step 2: Try out the same program with the below mentioned changes to the formula in calculateFees() :

fees = fees - (fees * (discount/100.0))

Does it work? If so,why?

Assignment 7: Debugging - Type Casting

Objective: Understand type casting Infosys Technologies Limited Object Oriented Programming using Java ER/CORP/CRS/ LA1026 CONFIDENTIAL Version No. 2.0 18 of 77

Problem Description: Debug the program created in Assignment 4 of Day 2. Perform the following steps:

Step 1: Modify the invocation of the setter method setQualifyingMarks() to pass 68.0 as the argument to the method.

Step 2: Compile the program, do you notice a compilation error , Why? What is the difference between the earlier invocation and the modified invocation?

Step 3: Rectify the error by using explicit type casting with argument, compile and execute the program

 

Note:

 Widening conversions are allowed whereas narrowing conversions result in compilation error

 It is not possible to assign a double to a float value

 

Assignment 8: Loops

Objective: Understand loops

Problem Description: The code given below is written to display all the even numbers between 50 and 80 (both inclusive). Debug the program to get the correct output.

Step 1: Type the below program in a textpad, save the file as ForLoop.java, compile and execute.

public class ForLoop{

public static void main(String args[]){

for(int i=50;i<80;i++){

if(i%2==0){

System.out.println(i);

}

else{


break;

}

}

}

}

Step 2: Correct the logical error in the code, save, compile and execute the code

Step 3: Implement the same logic using while loop

 

 

Assignment 9: Debugging - this reference

Objective: Understand usage of this reference to resolve instance variable hiding

Problem Description: Debug the below given code to get the output shown below after the code.

class Registration{

private int registrationId;

public void setRegistrationId(int registrationId){

registrationId=registrationId;

}

public int getRegistrationId(){

return registrationId;

}

}

class Demo{

public static void main(String args[]){

Registration reg = new Registration();

reg.setRegistrationId(1001);

System.out.println("Registration Id:"+

     reg.getRegistrationId()); } }
Step 2: Correct the logical error in the code, save, compile and execute the code to get the below output
Output Expected:

Registration Id: 1001

Self Review
Predict the output of the following programs and analyze your answer by executing the code. If there is an error, find the reason for the error and debug the same. Also mention the inference drawn from each snippet provided in a notepad.
1. class TypeCasting{ public static void main(String args[]){ int intVal=10; double doubleVal= 20.0; 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"); } } }
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; val3++,val2--){ if(val1 > 2){ val1--; } if(val1 > 5){ System.out.print(val1 + “ “);

--val1; continue; } val1--; } } }
6. public class SwitchCase { public static void main(String args[]){ int val = (int)(4 * 3); switch (val) { case 0: System.out.println("Case 0"); break; case 1: System.out.println("Case 1"); break; case 2: System.out.println("Case 2"); break; default: System.out.println("Something is wrong "); } }

Folder name

File name(s)

Assignment 1

Demo.java

Assignment 2

Demo.java

Assignment 3

Demo.java, Student.java

Assignment 4

Demo.java, Student.java

Assignment 5

DemoReg.java, Registration.java

Assignment 6

DemoReg.java, Registration.java

Assignment 7

Demo.java, Student.java

Assignment 8

ForLoop.java, WhileLoop.java

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值