This weeks lab involved working with 3 separate classes. I'm excited about this lab because it worked out perfectly for me in the end and the lab itself was fun. The goal of this project was to create a program that would ask for input for 3 different pets that included the name, weight, length, and date born then print all of the results. The main class referred to the pet class to set the name, weight, and length. The pet class in turn refers to the date class to set the date of birth.
//Christopher Smith
//CSC 15 Lab 12 Classes
//MainClass
import java.util.Scanner;
public class MainClass {
public static void main(String[] args) {
PetClass pet1 = new PetClass();
PetClass pet2 = new PetClass();
PetClass pet3 = new PetClass();
//take input referring to sub class EnterInfo in both petclass and dateclass
System.out.println("Enter your pets information");
pet1.EnterInfo();
System.out.println();
System.out.println("Enter your second pets information");
pet2.EnterInfo();
System.out.println();
System.out.println("Enter your third pets information");
pet3.EnterInfo();
System.out.println();
//printing the entered info back out
pet1.PrintInfo();
System.out.println();
pet2.PrintInfo();
System.out.println();
pet3.PrintInfo();
System.out.println();
}
}
---------------------------------------------------------------------------------------------------------
//Christopher Smith
//CSC 15 Lab12 Classes
//PetClass
import java.util.Scanner;
public class PetClass {
private String name,weight,length; //private string variables for use in this class
private DateClass birthdate = new DateClass(); //calls up date class and defines as birthdate
//accessor (makes private variables into a public available variable)
public String GetName() {return name;}
public String GetWeight() {return weight;}
public String GetLength() {return length;}
//mutator (uses private variables to modify the data stored in them)
public void SetName (String name_given){name=name_given;}
public void SetWeight (String weight_given){weight=weight_given;}
public void SetLength (String length_given){length=length_given;}//variable length_given used from keyboard input
/*
It's almost like with accessors and mutators everythings backwards. Your keyboard input goes into
the mutator which feeds into the accessor.
*/
//sub class used for displaying everything in petClass and dateClass
public void PrintInfo() {
System.out.println("Name is "+GetName());
System.out.println("Weight is "+GetWeight());
System.out.println("Length is "+GetLength());
System.out.print("Birthdate ");
birthdate.PrintInfo();//pulls birthdate info from my date class
}
//keyboard input used by mutator
public void EnterInfo() {
Scanner kb = new Scanner (System.in); //keyboard input defined as kb
System.out.print("Enter Name: ");
SetName(kb.nextLine()); //next string entry before enter used for kb input, sets name.
System.out.print("Enter Weight: ");
SetWeight(kb.nextLine());//next string sets pet weight
System.out.print("Enter Length: ");
SetLength(kb.nextLine());//next string sets pet length
System.out.print("Enter Birthdate: ");
birthdate.EnterInfo();//writing to dateclass enterinfo
}
}
----------------------------------------------------------------------------------------------------
//Christopher Smith
//CSC 15 lab 12 Classes
//DateClass
import java.util.Scanner;
public class DateClass { //public so can be called upon by other classes
private int mm, dd, yy; //private variables for this class only; used to store integers for date
//accessor used to create public accessible variables for use in other classes
public int GetMM() {return mm;}
public int GetDD() {return dd;}
public int GetYY() {return yy;}
//mutators, calls upon keyboard input to modify the variables
public void SetMM (int mm_given){mm=mm_given;}
public void SetDD (int dd_given){dd=dd_given;}
public void SetYY (int yy_given){yy=yy_given;}
//information display
public void PrintInfo() {
System.out.println("Date is: "+GetMM()+"/"+GetDD()+"/"+GetYY());
}
//for keyboard input to be used by mutator
public void EnterInfo() {
Scanner kb = new Scanner (System.in); //keyboard input defined as kb
System.out.print("Enter Month: ");
SetMM(kb.nextInt());
System.out.print("Enter Day: ");
SetDD(kb.nextInt());
System.out.print("Enter Year: ");
SetYY(kb.nextInt());
}
}
No comments:
Post a Comment