Printing, Comparing and Sorting User defined Data stored in a Vector by Overriding methods

 

public class Car implements Comparable{

String name;
String model;
int year;
static String country = "India";

Car(String name, String model, int year) {
this.name = name;
this.model = model;
this.year = year;
}

@Override
public String toString() {
return (model + " " + name + " " + year + " " + country);
}

@Override
public boolean equals(Object obj) {
if (obj instanceof Car) {
Car c = (Car) obj; //typecasting Object obj to Car Object
return (
this.name.equals(c.name) &&
this.model.equals(c.model) &&
this.year == c.year
);
} else {
return false;
}
}
@Override
public int compareTo(Object obj) {
if(obj instanceof Car){
Car c = (Car)obj;
return(this.model.compareTo(c.model));
} else
return 0;
}
}

import java.util.Collections;
import java.util.Vector;

class UserDefined {

public static void main(String[] args) {
//Creating an empty vector
Vector<Car> vector = new Vector<>();
//Creating objects of class car
Car c1 = new Car("Maruti", "Alto", 2000);
Car c2 = new Car("Hundai", "i20", 2010);
Car c3 = new Car("Ford", "FreeStyle", 2020);
//adding element(userdefined) in our vector
vector.add(c1);
vector.add(c2);
vector.add(c3);

//printing the elements of Vector on console
//this will print only address of garbage value
//we have to override toString function to print the values
System.out.println(vector);

//creating object of class Car
Car c4 = new Car("Ford", "FreeStyle", 2020);
//Comparing c4 with vector elements
//will return false as address will be compared
//for true result, have to override equals() method
System.out.println(
"Is element matches with Vector's Element? " + vector.contains(c4));

//Sorting element by model
//sort method will throw exception
//have to override compareTo method
//have to implement Comparable Interface
Collections.sort(vector);
System.out.println("Sorted element by model ;");
System.out.println(vector);
}
}


Google Script for Data Entry Form in Google Spreadsheet

// function to validate the entry made by user in user form function validateEntry (){ // declare a variable and referernece of active goog...