Lab Exercise - Array in JAVA
Lab Exercise - Array in JAVA
Answers:
class Exercise1{
int total = 0;
for(int i = 0; i< exe.length; i++){
System.out.println(exe[i]);
total = total + exe[i];
}
System.out.println("The average is: " + total/exe.length);
}
newExe[j] = exe[i];
}
return newExe;
}
System.out.println("Average: ");
average(exe1);
Use keyboard to key-in the input based on the attribute above. You can create any method
that suitable to get the data and display the data.
Bonus : apply passing array as parameter in method.
Answers:
import java.util.Scanner;
class Exercise2{
int z = 3;
int[] id = new int[z];
String[] name = new String[z];
int[] age = new int[z];
double[] salary = new double[z];
}
for(int i = 0;i<name.length;i++){
System.out.println(" "i);
System.out.println("Employer no " + i);
System.out.println("Id: " + id[i]);
System.out.println("Name: " + name[i]);
System.out.println("Age: " + age[i]);
System.out.println("Salary: " + salary[i]);
}
}
}
Answers:
class Exercise3{
title = t;
author = a;
yearPublished = year;
ISBN = IS;
price = p;
}
title = t;
}
author = a;
}
ISBN = IS;
}
yearPublished = year;
}
return title;
}
return author;
}
return ISBN;
}
return yearPublished;
}
return price;
}
obj[i].display();
}
Answers:
import java.util.Scanner;
class Teacher{
Teacher(){ }
name = n;
hoursWorked = h;
}
name = n;
}
hoursWorked = h;
}
return name;
}
return hoursWorked;
}
System.out.println(" ");
System.out.println("Name: " + t.getName());
System.out.println("Hours Worked: " + t.getHoursWorked());
System.out.println("Salary obtained: RM" + t.computePay(t));
}
}
obj[i].displaySalary(obj[i]);
}
}
}