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

Assignment 01

The document discusses the differences between instance variables and local variables in Java. Instance variables are defined in a class but outside methods and can be accessed throughout the class. They are initialized when an object is created and destroyed when the object is. Local variables are declared within methods/blocks and can only be accessed within that method/block. They are initialized and destroyed within that method/block. The document also discusses some math methods in Java like min(), max(), avg(), trigonometric functions that can be used to perform numeric calculations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Assignment 01

The document discusses the differences between instance variables and local variables in Java. Instance variables are defined in a class but outside methods and can be accessed throughout the class. They are initialized when an object is created and destroyed when the object is. Local variables are declared within methods/blocks and can only be accessed within that method/block. They are initialized and destroyed within that method/block. The document also discusses some math methods in Java like min(), max(), avg(), trigonometric functions that can be used to perform numeric calculations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Instance Variable Local Variable

They are defined as a type of variable


They are defined in class but outside
declared within programming blocks or
the body of methods.
subroutines.

These variables are created when an These variables are created when a block,
object is instantiated and are method or constructor is started and the
accessible to all constructors, methods, variable will be destroyed once it exits the
or blocks in class. block, method, or constructor.

These variables are destroyed when These variables are destroyed when the
the object is destroyed. constructor or method is exited.

It can be accessed throughout the Its access is limited to the method in which it
class. is declared.

These variables do not always have some


These variables are given a default
value, so there must be a value assigned by
value if it is not assigned by code.
code.

It is not compulsory to initialize It is important to initialize local variables


instance variables before use. before use.

They are used to reserving memory for They are used to decreasing dependencies
data that the class needs and that too for between components I.e., the complexity of
the lifetime of the object. code is decreased.

It includes access modifiers such as It does not include any access modifiers such
private, public, protected, etc. as private, public, protected, etc.

2. Java Math Class in Java contains certain methods to perform different numeric operations, for
instance, exponential, square root, logarithmic, trigonometric functions.
To perform different numeric calculations in Java, math class has provided several methods
which you will study further.
Some of the methods are min(), max(), avg(), sin(), cos() etc.
3.
/**
*
*/
package Assignment1_E2046333;

import java.util.Scanner;

/**
* @author Acer
*
*/
public class Assignment1_E2046333 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String name;
String EmpID;
int jobtype;
int workex;
int rate;
int workinghr;
int overtime;
int monthlySal;

Scanner scan=new Scanner(System.in);

System.out.println("Enter your name: ");


name=scan.nextLine();

System.out.println("Enter your Employee ID: ");


EmpID=scan.nextLine();

System.out.println("Select your job type: ");


System.out.println("1.Full-time");
System.out.println("2.Part-time");
jobtype=scan.nextInt();

if(jobtype==1){
System.out.println("Full-time Employee company
experience");
System.out.println("1.Less than 1 year");
System.out.println("2.1-2 years");
System.out.println("3.2-3 years");
System.out.println("4.More than 3 years");
System.out.println("Enter your option: ");
workex=scan.nextInt();

if (workex==1) {
System.out.println("Your name is: "+name);
System.out.println("Your Employee ID is:
"+EmpID);
System.out.println("Your salary is: Rs.
30,000");
}else if(workex==2) {
System.out.println("Your name is: "+name);
System.out.println("Your Employee ID is:
"+EmpID);
System.out.println("Your salary is: Rs.
45,000");
}else if(workex==3) {
System.out.println("Your name is: "+name);
System.out.println("Your Employee ID is:
"+EmpID);
System.out.println("Your salary is: Rs.
60,000");
}else if(workex==4) {
System.out.println("Your name is: "+name);
System.out.println("Your Employee ID is:
"+EmpID);
System.out.println("Your salary is: Rs.
70,000");
}
else if (jobtype==2) {

System.out.println("Enter the Rate per hour:


");
rate=scan.nextInt();

System.out.println("Enter the number of


working hours: ");
workinghr=scan.nextInt();

System.out.println("Enter the overtime hours:


");
overtime=scan.nextInt();

else if(!(workex==1 && workex==2 && workex==3 &&


workex==4)){
System.out.println("Invalid option, Enter your
option again");
}
}
}

You might also like