can anybody help me with this? condiser the following class: public class Worker
ID: 3833063 • 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
import java.io.*;
import java.util.*;
class Worker {
private String name;
private double rate;
private int age;
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;
}
}
public class TestClass{
public static void main(String[] args) {
List<Worker> workerList = new ArrayList<Worker>();
TestClass ob = new TestClass();
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);
}
ob.printOddWorkerAvgRate(workerList);
ob.printEvenWorkerAvgAge(workerList);
ob.removeWorker(workerList, 30);
ob.increaseRate(workerList, 1);
}
public void printOddWorkerAvgRate(List<Worker> workerList){
int sum = 0, j = 0;
float avg;
for(Worker element : workerList){
if(element.getName().startsWith("Amr")){
sum += element.getRate();
j++;
}
}
avg = (float)sum / j;
System.out.println("The avg rate of worker with name starting with Amr is " + avg);
}
public void printEvenWorkerAvgAge(List<Worker> workerList){
int sum = 0, j = 0;
float avg;
for(Worker element : workerList){
if(element.getName().startsWith("Sam")){
sum += element.getAge();
j++;
}
}
avg = (float)sum / j;
System.out.println("The avg age of worker with name starting with Sam is " + avg);
}
public void removeWorker(List<Worker> workerList, int age){
int i;
for(Worker element : workerList){
if(element.getAge() > age)
//workerList.remove(element);
i = 3;
}
}
public void increaseRate(List<Worker> workerList, int rate){
for(Worker element : workerList){
if(element.getRate() < rate)
element.setRate(element.getRate()+1);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.