Need these simple coding and give output of these 2 simple questions IN JAVA
.
1)
2)
Expert Answer
public class ArrayTester {
public static void main(String[] args) {
Numbers nums=new Numbers(4);//it initialize the list with index+3
nums.print();//print the values of list
nums.update();//update the nums according to update method
nums.print();
}
}
class Numbers{
private int [] list;
public Numbers (int size){//initialize the list
list =new int [size];
for (int index =0;index<size;index++)
list[index]=index*3;// each element of list is index*3
}
public void print(){
for (int num:list)
System.out.println(num +””);
System.out.println();
}
public void update(){
for (int index=1;index<list.length;index++)
list[index]=list[index-1]+1;//current element will become the previous element+1
list[0]=list.length;//first element is equal to length of list
}
}
2)
Name class:
public class Name {
String first;
String last;
public Name(String first,String last) {
this.first=first;
this.last=last;//initialize the value
}
public String toString()
{
return first+” “+last;
}
}
Student class:
public class Student {
Name name;
double gpa;
public Student(String first,String last,double gpa) {
this.name=new Name(first, last);//initialize the value
this.gpa=gpa;
}
public String toString()
{
return name.toString()+” “+gpa;
}
public void updateGPA(double gpa)
{
this.gpa=gpa;//update the gpa depends upon value
}