Java Language only please 1- Q4: [5 Points] What is call by value? Explain it wi
ID: 3766910 • Letter: J
Question
Java Language only please
1-
Q4: [5 Points]
What is call by value? Explain it with an example of swapping between two numbers.
2-
Q5: [5 Points]
Scenario:
Design a class to represent Employees in a small company according to the following requirements:
1- An employee has three attributes: name , salary and jobTitle
2- Add one constructor without parameters. In initialization of objects, Name is set to the value “NEW” and salary value is unspecified but it has to be an arbitrary number between 100 and 20000.
3- Add the following methods to the class:
getSalary- Returns employee’s salary.
getName- Returns employee’s name.
getJobTitle- Returns employee’s job title.
setName- Sets the employee’s name to a given value. Precondition: the number of characters in the given name is not less than 3 characters.
setSalary - Sets the employee’s salary to a given value. Precondition: the given salary value should be greater than 100.
4- All employees in this company have the same permanent job title: “Accountant” and it can’t be changed.
Explanation / Answer
1. What is call by value? Explain it with an example of swapping between two numbers.
Answer:
In call by value method,the value of actual argument will pass to the formal argument.And any changes made inside the formal argument,that does not reflect back to the actual argument.
The call by value method is useful when the value of actual arguments are not to be modified.
Any change made on formal arguments it does not effect on the actual arguments because the foemal argument are the photo copies of actual argument.
public class Swapping{
static void swap(int i,int j){
int temp=i;
i=j;
j=temp;
System.out.println("After swapping i = " + i + " j = " + j);
}
public static void main(String[] args){
int i=1;
int j=2;
System.out.prinln("Before swapping i="+i+" j="+j);
swap(i,j);
}
}
Design a class to represent Employees in a small company according to the following requirements:
1- An employee has three attributes: name , salary and jobTitle
2- Add one constructor without parameters. In initialization of objects, Name is set to the value “NEW” and salary value is unspecified but it has to be an arbitrary number between 100 and 20000.
3- Add the following methods to the class:
getSalary- Returns employee’s salary.
getName- Returns employee’s name.
getJobTitle- Returns employee’s job title.
setName- Sets the employee’s name to a given value. Precondition: the number of characters in the given name is not less than 3 characters.
setSalary - Sets the employee’s salary to a given value. Precondition: the given salary value should be greater than 100.
4- All employees in this company have the same permanent job title: “Accountant” and it can’t be changed.
public class Employees {
String name;
double salary;
String jobTitle;
public Employees()
{
name = "NEW";
salary = 100;
jobTitle = "Accountant";
}
public String getName() {
return name;
}
public void setName(String name) {
if(name.length()>=3)
{
this.name = name;
}
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
if(salary>100)
{
this.salary = salary;
}
}
public String getJobTitle() {
return jobTitle;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.