can anybody help me with this? condiser the following class: public class Worker
ID: 3833069 • Letter: C
Question
can anybody help me with this?
condiser the following class:
public class Worker {
private String name;
private double rate;
private int age;
public Worker(String name, double rate, int age) {
this.name = name;
this.rate = rate;
this.age = age;
}
public String getName() {
return name;
}
public double getRate() {
return rate;
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setRate(double rate) {
this.rate = rate;
}
public void setAge(int age) {
this.age = age;
}
}
next write a Test class for the following tasks:
1- implment worker class. 2- declare an arraylist of Worker objects in other class call it TestClass.
3- Add 100 Worker objects to the Arraylist, usng the forumla
if/is odd number, name of i-th worker is "Amr_i"(eg.1st worker name is ("Amr_1")
if/is even number, name of i-th worker is "sam_i"(eg.2nd worker name is ("sam_2)
age of i-th worker is = 20+(i/10%)
rate of i-th worker is =25.0-i/100
I solved the first 3 taskes and i couldn't solve the rest
class TestClass {
public static void mian(String[] args) {
List<Worker> workerList = new ArrayList<>();
for (int i = 0; i < 1000; i++) {
String name;
int age;
double rate;
if (i % 2 == 0) {
name = "Amr_" + String.valueOf(i);
} else {
name = "sam_" + String.valueOf(i);
}
age = 20 + (i / 10);
rate = 25.0 - i / 100.0;
Worker w = new Worker(name, rate, age);
workerList.add(w);
}
}
so can u solve the rest for me
1-print the average age of all workers i having name "Sam_i" (i.e,the even workers)
2--print the average rate of all workers i having name "Amr" (i.e,the even workers)
3- remove all the worers from the the ArrayList whose age is greater than 30.
4- increase the rate of all workers in the arraylist whose rate is less than 10 by 1.
Explanation / Answer
Here is the update for you:
import java.util.*;
class WorkerTestClass {
public static void main(String[] args) {
List<Worker> workerList = new ArrayList<>();
for (int i = 0; i < 1000; i++) {
String name;
int age;
double rate;
if (i % 2 == 0) {
name = "Amr_" + String.valueOf(i);
} else {
name = "sam_" + String.valueOf(i);
}
age = 20 + (i / 10);
rate = 25.0 - i / 100.0;
Worker w = new Worker(name, rate, age);
workerList.add(w);
}
//1-print the average age of all workers i having name "Sam_i" (i.e,the even workers)
double avgAge = 0;
for(int i = 1; i < workerList.size(); i+= 2)
avgAge += workerList.get(i).getAge();
avgAge /= workerList.size();
System.out.println("The average age of odd workers is: " + avgAge);
//2--print the average rate of all workers i having name "Amr" (i.e,the even workers)
double avgRate = 0;
for(int i = 0; i < workerList.size(); i += 2)
avgRate += workerList.get(i).getRate();
avgRate /= workerList.size();
System.out.println("The average rate of even workers is: " + avgRate);
//3- remove all the worers from the the ArrayList whose age is greater than 30.
for(int i = 0; i < workerList.size(); i++)
if(workerList.get(i).getAge() > 30)
workerList.remove(i);
//4- increase the rate of all workers in the arraylist whose rate is less than 10 by 1.
for(int i = 0; i < workerList.size(); i++)
if(workerList.get(i).getRate() < 10)
workerList.get(i).setRate(workerList.get(i).getRate() + 1);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.