/home/caleb/ASDV-Java/Semester 2/Exams/Exam2-Practice_CalebFontenot/src/main/java/com/calebfontenot/exam2/practice_calebfontenot/CountryOfOrigin.java |
nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt
nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java
package com.calebfontenot.exam2.practice_calebfontenot;
import java.util.Arrays;
@author
public class CountryOfOrigin implements Cloneable, Comparable<CountryOfOrigin> {
private char[] country = "USA".toCharArray();
public CountryOfOrigin() {}
public CountryOfOrigin(char[] country)
{
this.country = country;
}
public String getCountry()
{
return String.copyValueOf(this.country);
}
public void setCountry(char[] country)
{
this.country = country;
}
public boolean changeOneLetter(int index, char newLetter)
{
if (index >= this.country.length || index < 0) {
return false;
}
this.country[index] = newLetter;
return true;
}
@Override
public boolean equals(Object obj)
{
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final CountryOfOrigin other = (CountryOfOrigin) obj;
return Arrays.equals(this.country, other.country);
}
@Override
public String toString()
{
return String.copyValueOf(this.country);
}
@Override
public int compareTo(CountryOfOrigin o)
{
return String.copyValueOf(this.country).compareTo(String.copyValueOf(o.country));
}
@Override
public Object clone() {
return new CountryOfOrigin(new String(this.country).toCharArray());
}
public static void main(String[] args) throws CloneNotSupportedException
{
CountryOfOrigin c1 = new CountryOfOrigin();
CountryOfOrigin c2 = new CountryOfOrigin("GERMANY".toCharArray());
CountryOfOrigin c3 = (CountryOfOrigin) c2.clone();
System.out.println("c1 is: " + c1);
System.out.println("c2 is: " + c2);
System.out.println("c3 cloned of c2 is: " + c3);
c3.changeOneLetter(0, 'J');
c3.changeOneLetter(1, 'A');
c3.changeOneLetter(2, 'P');
System.out.println("c3 after changeOneLetter is: " + c3);
System.out.println("if this displays GERMANY and not JAPMANY\nyour clone is deep: " + c2);
System.out.println(c1.compareTo(c2));
System.out.println(c1.compareTo(c3));
System.out.println(c1.equals(c2));
System.out.println(c1.equals(c3));
System.out.println(c2.getCountry());
c2.setCountry("ITALY".toCharArray());
System.out.println(c2.getCountry());
System.out.println(c1.getCountry());
}
}