Skip to content

Instantly share code, notes, and snippets.

@Sadin
Created February 16, 2016 23:21
Show Gist options
  • Save Sadin/84de19a2ee0c706e77f0 to your computer and use it in GitHub Desktop.
Save Sadin/84de19a2ee0c706e77f0 to your computer and use it in GitHub Desktop.
// Writing and Utilizing Methods & Classes Part 1: Student Class
// Zach Snyder
public class Student
{
// instance variable
private String name;
private String soc;
private double gpa;
// Constructor
public Student()
{
name = "unknown";
}
public Student(String name,
String socNum,
double gpa )
{
setName(name);
setSoc(socNum);
setGpa(gpa);
}
// Mutator
public void setName(String newName) // set name
{
name = newName;
}
public void setSoc(String newSoc)
{
soc = newSoc;
}
public void setGpa(double newGpa)
{
gpa = newGpa;
}
// Accessor
public String getName()
{
return name;
}
public double getGpa()
{
return gpa;
}
public String getSoc()
{
return soc;
}
// toString This returns a String object
@Override
public String toString()
{
return "Student Name: " + name
+ "Student Social: " + soc
+ " Student GPA: " + gpa;
}
@Override
public boolean equals(Object o)
{
if ( ! ( o instanceof Student ) )
return false;
else
{
Student s = ( Student ) o;
if ( name.equals( s.name )
&& soc == s.soc )
return true;
else
return false;
}
}
}
// Writing and Utilizing Methods & Classes Part 1: Student Client
// Zach Snyder
public class StudentTest {
public static void main(String[] args) {
Student student = new Student("Mike", "170578234", 4.0);
Student student2 = new Student("Joe", "170823465", 2.8);
System.out.println("The First Student is: "
+ student.getName()
+ ", Their Social is: "
+ student.getSoc()
+ ", They are holding a GPA of: "
+ student.getGpa());
System.out.println("The First Student is: "
+ student2.getName()
+ ", Their Social is: "
+ student2.getSoc()
+ ", They are holding a GPA of: "
+ student2.getGpa());
System.out.println("\nTesting the toString method " + student.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment