Last active
August 12, 2018 18:02
-
-
Save kurtkaiser/86df79e9bad9e8ea637910b5c25a2c8f to your computer and use it in GitHub Desktop.
An assignment for an advance java course at my local community college. The assignment asked for four separate files, as shown below. Periodic needed to be abstract and PeriodArray had contain both metals and nonmetals elements.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Kurt Kaiser | |
CTIM-168 E40 | |
07.11.2018 | |
*/ | |
import java.util.Objects; | |
public abstract class Periodic | |
{ | |
// Instance Variables | |
private String symbol; | |
private int atomicNumber; | |
private double atomicWeight; | |
// Constructors | |
public Periodic() | |
{ | |
symbol = "none"; | |
atomicNumber = 0; | |
atomicWeight = 0; | |
} | |
public Periodic(String newSymbol, int newAtomicNumber, double newAtomicWeight) | |
{ | |
symbol = newSymbol; | |
atomicNumber = newAtomicNumber; | |
atomicWeight = newAtomicWeight; | |
} | |
// Getters | |
public String getSymbol() { | |
return symbol; | |
} | |
public int getAtomicNumber() { | |
return atomicNumber; | |
} | |
public double getAtomicWeight() { | |
return atomicWeight; | |
} | |
// Setters | |
public void setAtomicNumber(int atomicNumber) { | |
this.atomicNumber = atomicNumber; | |
} | |
public void setAtomicWeight(double atomicWeight) { | |
this.atomicWeight = atomicWeight; | |
} | |
public void setSymbol(String symbol) { | |
this.symbol = symbol; | |
} | |
public String toString() { | |
return "Symbol:" + symbol + | |
"\nAtomic Number: " + atomicNumber + | |
"\nAtomicWeight: " + atomicWeight; | |
} | |
@Override | |
public boolean equals(Object otherObject) { | |
if (this == otherObject) return true; | |
if (otherObject == null || getClass() != otherObject.getClass()) return false; | |
Periodic periodic = (Periodic) otherObject; | |
return atomicNumber == periodic.atomicNumber && | |
Double.compare(periodic.atomicWeight, atomicWeight) == 0 && | |
Objects.equals(symbol, periodic.symbol); | |
} | |
// Abstract method | |
public abstract void describeElement(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Kurt Kaiser | |
CTIM-168 E40 | |
07.11.2018 | |
*/ | |
public class PeriodArray { | |
public static void metals(PeriodicMetal[] metalsArray) { | |
metalsArray[0] = new PeriodicMetal("Na", 11, 22.9897); | |
metalsArray[1] = new PeriodicMetal("Ti", 22, 47.867); | |
metalsArray[2] = new PeriodicMetal("Li", 3, 6.941); | |
} | |
public static void nonMetals(PeriodicNonMetal[] nonMetalsArray) { | |
nonMetalsArray[0] = new PeriodicNonMetal("H", 1, 1.0079); | |
nonMetalsArray[1] = new PeriodicNonMetal("He", 2, 4.0026); | |
nonMetalsArray[2] = new PeriodicNonMetal("Se", 34, 78.96 ); | |
} | |
public static void main(String[] args) { | |
PeriodicMetal [] metalsArray = new PeriodicMetal[3]; | |
metals(metalsArray); | |
int sum = 1; | |
for (int i = 0; i < 3; i++){ | |
System.out.println("--- Metal " + sum + " ---\n" + metalsArray[i].toString()); | |
metalsArray[i].describeElement(); | |
sum++; | |
} | |
PeriodicNonMetal [] nonMetalsArray = new PeriodicNonMetal[3]; | |
nonMetals(nonMetalsArray); | |
sum = 1; | |
for (int c = 0; c < 3; c++){ | |
System.out.println("--- Non-Metal " + sum + " ---\n" + nonMetalsArray[c].toString()); | |
nonMetalsArray[c].describeElement(); | |
sum++; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Kurt Kaiser | |
CTIM-168 E40 | |
07.11.2018 | |
*/ | |
public class PeriodicMetal extends Periodic | |
{ | |
public PeriodicMetal(){ | |
super(); | |
} | |
public PeriodicMetal(String newSymbol, int newAtomicNumber, double newAtomicWeight){ | |
super(newSymbol, newAtomicNumber, newAtomicWeight); | |
} | |
public void describeElement(){ | |
System.out.println("Metals are good conductors."); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Kurt Kaiser | |
CTIM-168 E40 | |
07.11.2018 | |
*/ | |
public class PeriodicNonMetal extends Periodic | |
{ | |
public PeriodicNonMetal(){ | |
super(); | |
} | |
public PeriodicNonMetal(String newSymbol, int newAtomicNumber, double newAtomicWeight){ | |
super(newSymbol, newAtomicNumber, newAtomicWeight); | |
} | |
public void describeElement(){ | |
System.out.println("Non-metals are not good conductors."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment