How do I implement the toString() method for an ArrayStack? - java

I want to display a list of orders of type ArrayQueue <Order>
The class Order has an ArrayStack<String> as one of its attributes. I overrode the toString() method in the class Order, but how do I override it in the ArrayStack class? Because this is the output I get when I display:
OrderNumber Name Date ArrayStack#481adc30
What would I have to do to display the Strings in ArrayStack correctly? Do I make changes to class ArrayStack or change something in my Display method?
This is my Display method:
public void display(){
if (!isEmpty())
for (int i = 0; i < numberOfEntries; i++) {
System.out.println(queue[(frontIndex + i) % queue.length]);
}
else System.out.println("You don't have any orders");
}
ArrayStack Class:
public class ArrayStack < T > implements StackInterface < T >
{
private T [] stack; // array of stack entries
private int topIndex; // index of top entry
private static final int DEFAULT_INITIAL_CAPACITY = 50;
public ArrayStack ()
{
this (DEFAULT_INITIAL_CAPACITY);
} // end default constructor
public ArrayStack (int initialCapacity)
{
// the cast is safe because the new array contains null entries
# SuppressWarnings ("unchecked")
T [] tempStack = (T []) new Object [initialCapacity];
stack = tempStack;
topIndex = -1;
} // end constructor
/* Implementations of the stack operations */
Order Class:
import java.util.Date;
public class Order {
int orderNumber;
String customerName;
Date date;
StackInterface <String> items;
Order( int number, String name, Date datum, StackInterface<String> item){
orderNumber = number;
customerName= name;
date= datum;
items = item;
}
/Overriding toString() to Display a list of Orders as one String line.
public String toString(){
return orderNumber + " " + customerName + " " + date + " " + items;
}

You can override toString() method in ArrayStack as shown here. This will solve your problem.
public String toString() {
String result = "";
for (int scan = 0; scan < top; scan++)
result = result + stack[scan].toString() + "\n";
return result;
}

May be you should do this:
System.out.println(Arrays.toString(queue.toArray()));

Use this:
System.out.println(Arrays.toString(queue));

Related

How to access a member in a class with arraylist that adds its toString

I have a class, lets say CargoShip, which is a derived class of 'Starcraft', which implements the interface IStarcraft.
I have a function public static ArrayList<String> getSpacecraftDescriptionsByCommissionYear(ArrayList<ISpacecraft> fleet)
Question: The CargoShip has toString which prints name, commissionYear, etc..
I want to do two things: First, I want to use each Ship's toString (like the one in the CargoShip), and second I want it to be sorted by CommissionYear.
Problem: I don't know how to access the commissionYear field after I've added the toString to the arrayList.
ArrayList<String> strCommissions = new ArrayList<String>();
for(ISpacecraft flee : fleet)
{
strCommissions.add(flee.toString());
}
//Collections.sort(//What to write here??//);
return strCommissions;
}
Here is the CargoShip class if you need it:
package starfleet;
public class CargoShip extends Spacecraft{
private int numberOfSpaceCranes;
static int count = 0;
public CargoShip(String name, int commissionYear, float maximalSpeed,int cargoCapacity, int numberOfSpaceCranes)
{
this.name = name;
this.commissionYear = commissionYear;
if(MaximalSpeed())
this.maximalSpeed = maximalSpeed;
this.cargoCapacity = cargoCapacity;
this.numberOfSpaceCranes = numberOfSpaceCranes;
count++;
}
public int getNumberOfSpaceCranes ()
{
return this.numberOfSpaceCranes;
}
#Override
public String getName() {
return this.name;
}
#Override
public int getCommissionYear() {
return this.commissionYear;
}
#Override
public float getMaximalSpeed() {
if(MaximalSpeed())
return this.maximalSpeed;
else
return 0f;
}
#Override
public int getCargoCapacity() {
return this.cargoCapacity;
}
#Override
public int getFirePower() {
return this.firePower;
}
#Override
public int getAnnualMaintenanceCost() {
int cost = 0;
this.commissionYear = 2000;
cost += getCommissionYear();
cost += (500 * this.numberOfSpaceCranes);
cost += (2 * getCargoCapacity()); //To check: TotalCargoWeightCapacity?
// TODO Auto-generated method stub
return 0;
}
public String toString()
{
return
"Name = " + getName() + System.lineSeparator() +
"CommissionYear = " + getCommissionYear() + System.lineSeparator() +
"MaximalSpeed = " + getMaximalSpeed() + System.lineSeparator() +
"CargoCapacity = " + getCargoCapacity() + System.lineSeparator() +
"FirePower = " + getFirePower() + System.lineSeparator() +
"AnnualMaintenanceCost = " + getAnnualMaintenanceCost() + System.lineSeparator() +
"numberOfSpaceCranes = " + getNumberOfSpaceCranes() + System.lineSeparator();
}
}
First, you can copy the list fleet into a new one:
ArrayList<ISpacecraft> objects = new ArrayList<>(fleet);
then you can sort it by commission year:
Collections.sort(objects, Comparator.comparingInt(ISpacecraft::getCommissionYear));
then create the list of Strings:
ArrayList<String> strCommissions = new ArrayList<String>();
objects.forEach(o -> strCommissions.add(o.toString()));
thus your function becomes:
public static ArrayList<String> getSpacecraftDescriptionsByCommissionYear(ArrayList<ISpacecraft> fleet){
ArrayList<ISpacecraft> objects = new ArrayList<>(fleet);
Collections.sort(objects, Comparator.comparingInt(ISpacecraft::getCommissionYear));
ArrayList<String> strCommissions = new ArrayList<String>();
objects.forEach(o -> strCommissions.add(o.toString()));
return strCommissions;
}
reading:
Java 8 Lambda : Comparator example
You should implement Comparable and implement compareTo method or as mentioned above Comparator can be used.
public int compareTo(CargoShip otherCargoShip) {
int i = Name .compareTo(other.Name );
if (i != 0) return i;
i = CommissionYear.compareTo(other.CommissionYear);
if (i != 0) return i;
return Integer.compare(MaximalSpeed , other.MaximalSpeed );
}

Java printing an array of objects

I know there are a lot of pages about this question, but I cannot understand it in my case.
I need to print the array of objects. For example, I have an array of objects that hold objects from the "shape" class. Do I call the toString method for each object in the array, or do I code the toString method in ObjectList to print out the instance variables? If so, how do I do that?
public class Shape{
private String shapeName;
private int numSides;
public String toString(){
return shapeName + " has " + numSides + " sides.";
}
}
public class ObjectList{
private Object[] list = new Object[10];
private int numElement = 0;
public void add(Object next){
list[numElement] = next;
}
public String toString(){
// prints out the array of objects
// do I call the toString() method from the object?
// or do I print the instance variables? What am I printing?
// I'm guessing I do a for loop here
}
}
public class Driver{
public static void main(String[] args){
ObjectList list = new ObjectList();
Shape square = new Shape("square", 4);
Shape hex = new Shape("hexagon", 6);
list.add(square);
list.toString(); // prints out array of objects
}
I am aiming for it to print this:
square has 4 sides
hexagon has 6 sides
The simplest way to do this is use Arrays.toString:
Arrays.toString(myArray);
This will internally call the toString method of every element of your array.
So just override toString method in your Shape class and it should work fine.
To add further, override toString method in your class where you call Arrays.toString on your variable list :
public class ObjectList{
private Object[] list = new Object[10];
.............
public String toString(){
return Arrays.toString(list);
}
}
You can do this with bellowed code, make for loop in toString method to print each shape object.
class Shape{
private String shapeName;
private int numSides;
Shape(String shapeName, int numSides){
this.shapeName = shapeName;
this.numSides = numSides;
}
public String toString(){
return shapeName + " has " + numSides + " sides.";
}
}
class ObjectList{
private Object[] list = new Object[10];
private int numElement = 0;
public void add(Object next){
list[numElement] = next;
numElement++;
}
#Override
public String toString(){
String str="";
int i=0;
while(list[i] != null){
str += list[i]+"\n";
i++;
}
return str;
}
}
public class Driver{
public static void main(String[] args){
ObjectList list = new ObjectList();
Shape square = new Shape("square", 4);
Shape hex = new Shape("hexagon", 6);
list.add(hex);
list.add(square);
System.out.println(list);
}
}
Write a for-each statement in toString() of Object List and create a large String with '\n' characters and return it as a String . Or may be name displayListElement() will be semantically more correct in which you can simple print all the Objects in the list .
Indeed, you should call toString method for each of objects that you want to print and join them together. You can use StringBuilder to hold the string-in-the-making as follows:
public String toString() {
StringBuilder result = new StringBuilder();
for (int i = 0; i <= numElements; i++) {
result.append(list.toString() + "\n");
}
return result.toString();
}
Note that you need to increase numElements (e.g. numElements++) for each add operation as what pbabcdefp said in the comments. Also, you can use ArrayList class to manage "growing arrays".

ArrayList sort for String, int [duplicate]

This question already has answers here:
Sort ArrayList of custom Objects by property
(29 answers)
How to sort Arraylist of objects
(3 answers)
Closed 9 years ago.
I m looking to sort an ArrayList which is of the type <String,int>, according to int.
So, my variable is var<String,int>
India 2
Pakistan 3
USA 1
The output becomes:
USA 1
India 2
Pakistan 3
I am confused how does it works with int. Collections.sort(var) does not works with it.
You can't use ArrayList of type
<String, int>
You can't have primitives in ArrayList as ArrayList holds objects. So, the closest you can do is to store Integer objects.
ArrayList can be of only one type if you are parameterizing it.
If you want to hold String and int, you can create a class CountryInfo with fields name and rank. Then create
ArrayList<CountryInfo> list =new ArrayList<CountryInfo>();
Then you can use
Collections.sort(list, <Comparator>)
I have created an example where you can sort your ArrayList even if its with objects. You can read through it an see if it's helps.
I have made two classes and a test class:
First class is Country:
public class Country {
private String countryName;
private int number;
public Country(String countryName, int number){
this.countryName = countryName;
this.number = number;
}
public String getCountryName(){
return countryName;
}
public void setCountryName(String newCountryName){
countryName = newCountryName;
}
public int getNumber(){
return number;
}
public void setNumber(int newNumber){
number = newNumber;
}
public String toString(){
return getCountryName() + getNumber();
}
}
Next class is Methods:
public class Methods {
private Country country;
private ArrayList<Country> overview = new ArrayList<Country>();
private ArrayList<Country> overviewSorted = new ArrayList<Country>();
int [] test;
public void regCountry(String countryname, int numbers){
if(!(countryname == "" && numbers == 0)){
overview.add(new Country(countryname, numbers));
} else {
System.out.println("The input was null");
}
}
public void showRegisteredCountries(){
if(!(overview.size() < 0)){
for(int i = 0; i < overview.size(); i++){
System.out.println("The country: " + overview.get(i).getCountryName() + " has the number: " + overview.get(i).getNumber() + " registered");
}
} else {
System.out.println("There are no country registered");
}
}
public void numbersOverFromArrayList(){
if(!(overview.size() < 0)){
test = new int [overview.size()];
for(int i = 0; i < overview.size(); i++){
test[i] = overview.get(i).getNumber();
}
}
}
public void sortArrayAndCopyItBack(){
if(!(test.length < 0)){
java.util.Arrays.sort(test);
for(int i = 0; i < test.length; i ++){
for(int j = 0; j < overview.size(); j++){
if(test[i] == overview.get(j).getNumber()){
overviewSorted.add(new Country(overview.get(j).getCountryName(), overview.get(j).getNumber()));
}
}
}
}
}
public void showTableSorted(){
if(!(overviewSorted.size() < 0)){
for(int i = 0; i < overviewSorted.size(); i++){
System.out.println("Country name: " + overviewSorted.get(i).getCountryName() + " with number: " + overviewSorted.get(i).getNumber());
}
} else {
System.out.println("There are non countrys in table that is sorted");
}
}
}
Next is the test class:
public class test2 {
public static void main(String [] args){
Methods methodes = new Methods();
for(int i = 0; i < 4; i++){
String inCountry = JOptionPane.showInputDialog("Country:");
String inNumber = JOptionPane.showInputDialog("number:");
String country = inCountry;
int number = Integer.parseInt(inNumber);
methodes.regCountry(country, number);
}
methodes.showRegisteredCountries();
methodes.numbersOverFromArrayList();
methodes.sortArrayAndCopyItBack();
methodes.showTableSorted();
}
}
My output:
The country: Norway has the number: 5 registered
The country: Sweden has the number: 2 registered
The country: Denmark has the number: 9 registered
The country: Finland has the number: 7 registered
Country name: Sweden with number: 2
Country name: Norway with number: 5
Country name: Finland with number: 7
Country name: Denmark with number: 9
That is not an ArrayList. Use TreeMap in Stead.
Map<String, Integer> countryInfo = new TreeMap<String,Integer>();
This way it will be sorted automatically
You can sort
use Collections.sort(list,Comparator implementation)
in the implementation(here I have used anonymous implementation) override compare method
where you
get last character of each string convert to string and compare them
ArrayList<String> a=new ArrayList<String>();
a.add("India 2");
a.add("Pakistan 3");
a.add("USA 1");
Collections.sort(a, new Comparator<String>() {
#Override
public int compare(String o1, String o2) {
Integer i=Integer.valueOf(o1.substring((o1.length() -1),o1.length()));
Integer j=Integer.valueOf(o2.substring((o2.length() -1),o2.length()));
return i.compareTo(j);
}
});
You can optimist code
ArrayList is a collection of one type of object. It is not like maps that can take two inputs.
Therefore, there are three options:
1. Make use of a TreeMap that contains both a Key and a Map and is automatically sorted by key or
2. Make use of an unsorted map and sort with a comparator - see Sort a Map<Key, Value> by values (Java) or
3. Use an arraylist of a custom class with a comparator.
-
1) Using a TreeMap
Treemaps are an implementation of red-black trees. See: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/TreeMap.html
TreeMap<Integer,String> countries = new TreeMap<Integer,String>();
countries.put(2, "India");
countries.put(1, "USA");
countries.put(3, "Pakistan");
Iterator<Entry<Integer, String>> it = countries.entrySet().iterator();
Entry<Integer, String> entry;
while(it.hasNext())
{
entry = it.next();
System.out.println(entry.getValue() + " " + entry.getKey());
}
And this Produces:
USA 1
India 2
Pakistan 3
-
2) Make use of an unsorted map and sort with a comparator
See: Sort a Map<Key, Value> by values (Java) as the answer is very will written.
-
3) Using an ArrayList with Country Class
In order to support your example you would need to create a Country class.
You would need to do the following:
Implement Comparable within your country class and place the logic for the comparison within there.
Create a custom comparator that you will give to your Collection.sort invocation.
import java.util.ArrayList;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.Iterator;
public class CountrySortExample {
public static void main(String[] args) {
new CountrySortExample();
}
public ArrayList<Country> countries = new ArrayList<Country>();
public CountrySortExample()
{
countries.add(new Country("India",2));
countries.add(new Country("Pakistan",3));
countries.add(new Country("USA",1));
Collections.sort(countries);
Iterator<Country> it = countries.iterator();
Country count;
while(it.hasNext())
{
count = it.next();
System.out.println(count.CountryName + " " + count.CountryIndex);
}
}
class Country implements Comparable
{
public String CountryName;
public int CountryIndex;
public Country(String CountryName,int CountryIndex )
{
this.CountryName = CountryName;
this.CountryIndex = CountryIndex;
}
#Override
public int compareTo(Object o) {
if(! (o instanceof Country))
throw new InputMismatchException("Country is expected");
Country other = (Country)o;
if(other.CountryIndex > CountryIndex)
return -1;
else if(other.CountryIndex == CountryIndex)
return 0;
else return 1;
}
}
}
Further information is available at: http://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/
If you have an object that you want to sort in more than one way, you define a Comparator class for each type of sort you want to do.
Using the example that the OP gave, here's one way to define the object and Comparators.
Here's one test result:
CountryRating [name=India, rating=2]
CountryRating [name=Pakistan, rating=3]
CountryRating [name=USA, rating=1]
CountryRating [name=USA, rating=1]
CountryRating [name=India, rating=2]
CountryRating [name=Pakistan, rating=3]
And here's the example code:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class CountryRating {
private String name;
private int rating;
public CountryRating(String name, int rating) {
this.name = name;
this.rating = rating;
}
public String getName() {
return name;
}
public int getRating() {
return rating;
}
#Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("CountryRating [name=");
builder.append(name);
builder.append(", rating=");
builder.append(rating);
builder.append("]");
return builder.toString();
}
public static void main(String[] args) {
List<CountryRating> list = new ArrayList<CountryRating>();
CountryRating cr1 = new CountryRating("USA", 1);
CountryRating cr2 = new CountryRating("India", 2);
CountryRating cr3 = new CountryRating("Pakistan", 3);
list.add(cr1);
list.add(cr2);
list.add(cr3);
Collections.sort(list, new CountrySort());
printList(list);
System.out.println(" ");
Collections.sort(list, new RatingSort());
printList(list);
}
private static void printList(List<CountryRating> list) {
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
class CountrySort implements Comparator<CountryRating> {
#Override
public int compare(CountryRating cr1, CountryRating cr2) {
return cr1.getName().compareTo(cr2.getName());
}
}
class RatingSort implements Comparator<CountryRating> {
#Override
public int compare(CountryRating cr1, CountryRating cr2) {
return cr1.getRating() - cr2.getRating();
}
}

implementing a method from one class to another?

I am making a program for airplane seating arrangements for a class and i ended up making two toString methods but when I run the program the toString method in my airplane class is making something not work specifically:
str= str + seats[i][j].toString();
I believe that simply deleting the toString method in the seat class and somehow putting it back into the airplane class toString method would fix the problem or make it simpler. What's wrong?
Airplane class:
public class Airplane
{
private Seat [ ] [ ] seats;
public static final int FIRST_CLASS = 1;
public static final int ECONOMY = 2;
private static final int FC_ROWS = 5;
private static final int FC_COLS = 4;
private static final int ECONOMY_ROWS = 5;
private static final int ECONOMY_COLS = 6;
public Airplane()
{
seats = new Seat[FC_ROWS][ECONOMY_COLS];
}
public String toString()
{
String str = "";
for (int i=0; i<FC_ROWS; i++) {
for (int j=0; j<ECONOMY_COLS; j++)
{
str= str + seats[i][j].toString();
}
str = str + "\n";
}
return str;
}
}
Seat Class:
public class Seat
{
private int seatType;
private boolean isReserved;
public static final int WINDOW = 1;
public static final int AISLE = 2;
public static final int CENTER = 3;
public Seat(int inSeatType)
{
seatType = inSeatType;
isReserved = false;
}
public int getSeatType()
{
return seatType;
}
public void reserveSeat()
{
isReserved = true;
}
public boolean isAvailable()
{
if (!isReserved)
{
return true;
}
else return false;
}
public String toString()
{
if(isReserved == false)
{
return "*";
}
else return "";
}
}
In Seat.toString you should print a " " not "".
You're array is FC_ROWS by ECONOMY_COLS, so you're not creating all the seats. You should probably have two arrays (one for FC, one for Economy), since FC_ROWS != ECONOMY_ROWS.
You aren't actually creating Seats in your constructor. Use a nested loop to create them, otherwise you will get a NullPointerException. Creating an array doesn't create the objects contained in the array.
When you're creating the seats in the Airplane constructor, use if statements to figure out if the seat is supposed to be a Window, Aisle, etc.
seats seems to does not have Seat's instance.
Add this code :
for (int i=0; i<FC_ROWS; i++) {
for (int j=0; j<ECONOMY_COLS; j++)
{
seats[i][j] = new Seat();
}
}
below this :
seats = new Seat[FC_ROWS][ECONOMY_COLS];
I think that in Seat::toString, you mean to return " " (a space) if it isn't reserved.

trouble with array lists

I am doing a project and instead of using an array, I figured an array list would be better. I know I need to declare the array list and its methods, but I am not too sure where to go from there. Any suggestions? Here's code...
public class Student {
private String name;
private int[] tests;
public Student() {
this("");
}
public Student(String nm) {
this(nm, 3);
}
public Student(String nm, int n) {
name = nm;
tests = new int[n];
for (int i = 0; i < tests.length; i++) {
tests[i] = 0;
}
}
public Student(String nm, int[] t) {
tests = new int[t.length];
}
public Student(Student s) {
this(s.name, s.tests);
}
public int getNumberOfTests() {
return tests.length;
}
public void setName(String nm) {
name = nm;
}
public String getName() {
return name;
}
public void setScore(int i, int score) {
tests[i - 1] = score;
}
public int getScore(int i) {
return tests[i - 1];
}
public int getAverage() {
int sum = 0;
for (int score : tests) {
sum += score;
}
return sum / tests.length;
}
public int getHighScore() {
int highScore = 0;
for (int score : tests) {
highScore = Math.max(highScore, score);
}
return highScore;
}
public String toString() {
String str = "Name: " + name + "\n";
for (int i = 0; i < tests.length; i++) {
str += "test " + (i + 1) + ": " + tests[i] + "\n";
}
str += "Average: " + getAverage();
return str;
}
public String validateData() {
if (name.equals("")) {
return "SORRY: name required";
}
for (int score : tests) {
if (score < 0 || score > 100) {
String str = "SORRY: must have " + 0 + " <= test score <= " + 100;
return str;
}
}
return null;
}
}
I figured an array list would be better
Maybe. Maybe not. It depends. Does it look like you would get a benefit in using one based on the ArrayList API?
If your "list" never changes size, and you don't need to find things in it, then an array is just as good.
I know I need to declare the array list and its methods, but I am not
too sure where to go from there
You need to create a reference to an instance of an ArrayList. That's as simple as
List<Integer> myList = new ArrayList<Integer>();
in your class declaration. You don't need to "declare its methods". When you have a reference to an object, you can invoke its methods.
To use an ArrayList, you just need to declare and instantiate it:
// <SomeObject> tells Java what kind of things go in the ArrayList
ArrayList<SomeObject> aDescriptiveNameHere = new ArrayList<SomeObject>();
// This is also valid, since an ArrayList is also a List
List<SomeObject> list = new ArrayList<SomeObject>();
Then you can add things with the add() method:
// Please don't name your list "list"
list.add(new Thing(1));
list.add(new Thing(2));
You can get something by index (like you would with someArray[index]) as:
list.get(index);
// For example
Thing t = list.get(5);
You probably also need the size().
See the JavaDocs for more info.
All of the operations you're using are mirrored in the ArrayList API. One thing that's worth noting is that you cannot declare an ArrayList of primitive types, but for each of the primitive types there exists an Object that is the boxed version of the primative.
The boxed version of int is Integer, so you have
ArrayList<Integer> myList = new ArrayList<Integer>();
From there, you need to look up the methods you would need to use in order to manipulate the array. For example, if you want to add the number 42 to the end of the array, you would say
myList.add(42);
The ArrayList API is located here.
I think it could be better to use the stl vector instead make your own arrays
I tried to change the array to arraylist.
Reply if this doesn't compile correctly.
import java.util.ArrayList;
public class Student {
private String name;
// private int[] tests;
private ArrayList<Integer> tests;
public Student() {
this("");
}
public Student(String nm) {
// this(nm, 3);
name = nm;
}
/*
* public Student(String nm, int n) { name = nm; tests = new int[n]; for
* (int i = 0; i < tests.length; i++) { tests[i] = 0; } }
*/
/*
* public Student(String nm, int[] t) { tests = new int[t.length]; }
*/
public Student(Student s) {
this(s.name, s.tests);
}
public Student(String name2, ArrayList<Integer> tests2) {
name = name2;
tests = tests2;
}
public int getNumberOfTests() {
return tests.size();
}
public void setName(String nm) {
name = nm;
}
public String getName() {
return name;
}
// public void setScore(int i, int score) {
// tests[i - 1] = score;
public void setScore(int score) {
tests.add(score);
}
public int getScore(int i) {
// return tests[i - 1];
return tests.get(i - 1);
}
public int getAverage() {
int sum = 0;
for (int score : tests) {
sum += score;
}
// return sum / tests.length;
return sum / tests.size();
}
public int getHighScore() {
int highScore = 0;
for (int score : tests) {
highScore = Math.max(highScore, score);
}
return highScore;
}
public String toString() {
String str = "Name: " + name + "\n";
for (int i = 0; i < tests.size(); i++) {
str += "test " + (i + 1) + ": " + tests.get(i) + "\n";
}
str += "Average: " + getAverage();
return str;
}
public String validateData() {
if (name.equals("")) {
return "SORRY: name required";
}
for (int score : tests) {
if (score < 0 || score > 100) {
String str = "SORRY: must have " + 0 + " <= test score <= "
+ 100;
return str;
}
}
return null;
}
public ArrayList<Integer> getTests() {
return tests;
}
public void setTests(ArrayList<Integer> tests) {
this.tests = tests;
}
}

Categories