Skip to content

Commit e53f4bc

Browse files
committed
6.1 Method with return value
In this program simple method overtime() is created to calculate extra hours worked by the employee and returned the extra work time.
1 parent bb4a41f commit e53f4bc

6 files changed

Lines changed: 74 additions & 11 deletions

PrintStringTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ class PrintStringTest
1515
public static void main(String args[])
1616
{
1717

18-
PrintString p = new PrintString();
19-
p.displayText();
18+
PrintString p = new PrintString(); // Creating Object of class PrintString
19+
p.displayText(); // Calling non static method displayText()
2020

2121
}
2222
}

PrintStringTestStat.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
class PrintString
33
{
44

5-
static void displayText()
5+
static void displayText() // Defining static method
66
{
77
System.out.println("Hello, World :) ");
88
}
@@ -15,7 +15,7 @@ class PrintStringTestStat
1515
public static void main(String args[])
1616
{
1717

18-
PrintString.displayText();
18+
PrintString.displayText(); // Calling static method from object name
1919

2020
}
2121
}

TestEmployeeCmd.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@ public static void main(String args[])
2727
{
2828

2929
Employee e = new Employee();
30-
e.setData(args);
30+
e.setData(args); // Passing command line input to setData method
3131
e.getData();
3232
}
33-
}
33+
}
34+
35+
/*
36+
to compile
37+
/path/of/filedirectory>javac TestEmployeeCmd.java
38+
/path/of/filedirectory>java TestEmployeeCmd Rupesh 56 90000.50 true
39+
*/

TestEmployeeObject.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import java.util.Scanner;
2+
class Employee
3+
{
4+
String name;
5+
int e_id;
6+
float salary;
7+
boolean status;
8+
9+
Scanner sc = new Scanner(System.in);
10+
void setData()
11+
{
12+
13+
System.out.println("Enter Name (String) of Employee: " );
14+
name=sc.nextLine();
15+
System.out.println("Enter Employee ID (Integer) of Employee: " );
16+
e_id=sc.nextInt();
17+
System.out.println("Enter Salary (Float) of Employee: " );
18+
salary=sc.nextFloat();
19+
System.out.println("Enter Working Satus (Boolean) of Employee: " );
20+
status=sc.nextBoolean();
21+
22+
}
23+
void getData()
24+
{
25+
System.out.println("Name:"+name+"\nEmp Id:"+e_id+"\nSalary:"+salary+"\nStatus:"+status);
26+
}
27+
Employee copyEmployee(Employee e)
28+
{
29+
Employee de = new Employee();
30+
de.name =e.name;
31+
de.e_id = e.e_id;
32+
de.salary = e.salary;
33+
de.status = e.status;
34+
return de;
35+
}
36+
}
37+
38+
/*** Main Class***/
39+
40+
class TestEmployeeObject
41+
{
42+
public static void main(String args[])
43+
{
44+
45+
Employee e = new Employee();
46+
e.setData();
47+
System.out.println("\n\n Details of Employee : "+e.name );
48+
e.getData();
49+
Employee ecopy = e.copyEmployee(e);
50+
System.out.println("\n\n Details of copy of Employee : "+e.name );
51+
ecopy.getData();
52+
53+
}
54+
}

TestEmployeeOvertime.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ void getData()
2626
{
2727
System.out.println("Name:"+name+"\nEmp Id:"+e_id+"\nSalary:"+salary+"\nStatus:"+status);
2828
}
29-
float overtime()
29+
float overtime(float time)
3030
{
31-
float ot = work_hrs-40;
31+
float ot = work_hrs-time;
3232
return ot;
3333
}
3434
}
@@ -43,7 +43,10 @@ public static void main(String args[])
4343
Employee e = new Employee();
4444
e.setData();
4545
e.getData();
46-
float ot = e.overtime();
46+
System.out.println("Enter working hour for the Month: ");
47+
Scanner sc = new Scanner(System.in);
48+
float time = sc.nextFloat();
49+
float ot = e.overtime(float time);
4750
if(ot > 0)
4851
{
4952
System.out.println(e.name +" worked "+ot+" Hrs Extra");

TestEmployeeScan.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ class Employee
55
int e_id;
66
float salary;
77
boolean status;
8-
Scanner sc = new Scanner(System.in); //Creating Object for the call Scanner
8+
Scanner sc = new Scanner(System.in); //Creating Object for the Scanner class
99
void setData()
1010
{
1111

1212
System.out.println("Enter Name (String) of Employee: " );
13-
name=sc.nextLine(); // Methood to accept String from user
13+
name=sc.nextLine(); // Method to accept String from user
1414
System.out.println("Enter Employee ID (Integer) of Employee: " );
1515
e_id=sc.nextInt();
1616
System.out.println("Enter Salary (Float) of Employee: " );

0 commit comments

Comments
 (0)