Skip to content

Instantly share code, notes, and snippets.

@mangkoran
Last active April 18, 2021 08:34
Show Gist options
  • Save mangkoran/1cc6683eceecb26d1c4f8f48907b6448 to your computer and use it in GitHub Desktop.
Save mangkoran/1cc6683eceecb26d1c4f8f48907b6448 to your computer and use it in GitHub Desktop.
OOP Java Array Tutorial
public class Product {
public static void main(String[] args) {
calculate(1);
calculate(1, 2);
calculate(1, 2, 3);
calculate(1, 2, 3, 4);
}
public static void calculate(int ... i) {
int sum = 0;
System.out.printf("Number of arguments: %d", i.length);
for (int j : i) {
sum += j;
}
System.out.printf(", Sum: %d%n", sum);
}
}
public class TestTeacher {
public static void main(String[] args) {
Teacher teachers[] = new Teacher[10];
teachers[0] = new Teacher("Shigetora", 72.7);
teachers[1] = new Teacher("Teacher 1", 10);
teachers[2] = new Teacher("Teacher 2", 20);
teachers[3] = new Teacher("Teacher 3", 30);
teachers[4] = new Teacher("Teacher 4", 40);
teachers[5] = new Teacher("Teacher 5", 50);
teachers[6] = new Teacher("Teacher 6", 60);
teachers[7] = new Teacher("Teacher 7", 70);
teachers[8] = new Teacher("Teacher 8", 80);
teachers[9] = new Teacher("Teacher 9", 90);
for (Teacher teacher : teachers) {
displaySalary(teacher);
}
// displaySalary(teachers[0]);
}
public static void displaySalary(Teacher teacher) {
System.out.printf("Name: \t\t%s%n" +
"Hours Worked: \t%.2f%n" +
"Salary: \t%.2f%n", teacher.getName(), teacher.getHoursWorked(), teacher.computePay());
System.out.printf("%n");
}
}
public class Teacher {
private String name;
private double hoursWorked;
static final int RATE = 30;
public Teacher() {
}
public Teacher(String name, double hoursWorked) {
this.name = name;
this.hoursWorked = hoursWorked;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getHoursWorked() {
return hoursWorked;
}
public void setHoursWorked(double hoursWorked) {
this.hoursWorked = hoursWorked;
}
public double computePay() {
return (hoursWorked*RATE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment