In: Computer Science
Given an array, write code to scan the array for a particular purpose.
Given a class, write a getter and/or a setter.
Given a class with a list, write code that manages the list.
Since you have not mentioned the language of your preference, I am providing the code in Java.
1.
CODE
public class Main {
public static void main(String[] args) {
int arr[] = {34, 1, 56, 8, 19};
int sum = 0;
for (int i=0; i<arr.length; i++) {
sum += arr[i];
}
System.out.println("Sum of array elements = " + sum);
}
}
2.
class Person {
private String name;
private int age;
private char sec;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public char getSec() {
return sec;
}
public void setSec(char sec) {
this.sec = sec;
}
}
3.
import java.util.ArrayList;
class MyList {
private ArrayList<String> list;
public MyList() {
list = new ArrayList<>();
}
public void add(String x) {
list.add(x);
}
public void delete(String x) {
list.remove(x);
}
public boolean isEmpty() {
return list.isEmpty();
}
public void update(int i, String x) {
list.set(i, x);
}
}