Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
140 views

Assignment 2 Answer

The document contains code for a MyArrayList class that implements basic array list functionality like adding and removing elements from the front and back. It also contains code for a Student class with attributes like name, student ID, and score. The main method prompts the user for input, creates Student objects, adds them to a MyArrayList, calculates statistics like total score, average, minimum and maximum scores, and displays the results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
140 views

Assignment 2 Answer

The document contains code for a MyArrayList class that implements basic array list functionality like adding and removing elements from the front and back. It also contains code for a Student class with attributes like name, student ID, and score. The main method prompts the user for input, creates Student objects, adds them to a MyArrayList, calculates statistics like total score, average, minimum and maximum scores, and displays the results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

MyArrayList

/**
* MyArrayList
*
* NURHAYATI BINTI MOHD HUSNI
* 28/9/2015
*/
//EXERCISE 1
public class MyArrayList
{
private static final int INITIAL_CAPACITY = 5;
private Object[] theData;
private int size = 0;
private int capacity = 0;
//default constructor
public MyArrayList()
{
theData = new Object [INITIAL_CAPACITY];
capacity = INITIAL_CAPACITY;
}
//EXERCISE 2
public boolean isEmpty()
{
return size == 0;
}
public boolean isFull()
{
return size == capacity;
}
public int size()
{
return size;
}
public Object get(int index)
{
return theData[index];
}

//EXERCISE 4
public void display()
{
for(int i=0; i<size; i++)
System.out.println(theData[i]);
}
//EXERCISE 3
public void insertAtFront(Object element)
{
if (isFull())
System.out.println("\nSorry, array is already full!");
else if (isEmpty())
{
theData[0] = element;
size++;
}
else
{
for (int i=size; i>0; i--)
theData[i] = theData[i-1];
theData[0] = element;
size++;
}

}
//}

public Object removeFromBack()


{
Object element;

if(isEmpty())
{
System.out.println("\nThe list is empty, cannot be
removed.");
element = null;
}
else

{
element = theData[size-1];
size--;

}
return element;
}
}

Student
import java.util.*;
public class Student
{
String nm;
int stuId;
double scor;
//default constructor
Student()
{
nm = null;
stuId = 0;
scor = 0;
}
//normal contructor
Student (String n, int id, double s)
{
nm = n;
stuId = id;
scor = s;
}
//accessor
public String getNm()
{
return nm;

}
public int getStuId()
{
return stuId;
}
public double getScor()
{
return scor;
}

//toString()
public String toString()
{
return ("\n Name : " + nm + "\n Student Id : " + stuId + "\n
Score : " + scor);
}
}

Exercise 14
import java.util.*;

public class ArrayListApp


{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
MyArrayList studList = new MyArrayList();
String nm;
int stuId;
double scor=0.0;
int num;
double total=0;
int elementToBeRemoved = 2;
System.out.println("How many students : ");
num = scanner.nextInt();

for (int i = 0; i<num; i++)


{
System.out.println("\nName : ");
nm = scanner.next();
System.out.println("Student ID : ");
stuId = scanner.nextInt();
System.out.println("Score : ");
scor = scanner.nextDouble();
if (scor>100.00)
{
System.out.println("Enter valid score again : ");
scor = scanner.nextDouble();
}

Student stud = new Student (nm, stuId, scor);


studList.insertAtFront(stud);

}
for (int x=0; x<elementToBeRemoved; x++)
{
studList.removeFromBack();
}
//studList.removeFromBack();

for (int k = 0; k<studList.size(); k++)


{
Student temp = (Student)studList.get(k);
total = total + temp.getScor();
}

double average = total/studList.size();


double max = 0.0;
for (int a = 0; a<1 ; a++)
{
Student temp = (Student)studList.get(a);
max = temp.getScor();
}

double min = 100;


for (int a = 1; a<studList.size(); a++)
{
Student temp = (Student)studList.get(a);
if (temp.getScor() > max)
max = temp.getScor();
else
max = max;
}
for (int a = 1; a<studList.size(); a++)
{
Student temp = (Student)studList.get(a);
if (temp.getScor() < min)
min = temp.getScor();
else
min = min;
}

System.out.println("\nStudent details : ");


studList.display();
System.out.println("\n\n\nTotal Score : " + total);
System.out.println("\nAverage : " + average);.out.println("\nMinimum
Score : " + min);
System.out.println("\nMaximum Score : " + max);
}
}

INPUT / OUTPUT

You might also like