Skip to content

Instantly share code, notes, and snippets.

@NotInControl

NotInControl/Date

Last active Jan 4, 2016
Embed
What would you like to do?
2168-FRCJavaCourse2015-class3
package class3;
//class definition
public class Date {
//member variables
String month;
int day;
int year;
public void writeOutput()
{
System.out.println("Date :");
System.out.println(month + " " + day + ", " + year);
}
public String getMonth()
{
return month;
}
public int getDay()
{
return day;
}
public int getYear()
{
return year;
}
public void setMonth (String inputMonth)
{
month = inputMonth;
}
public void setDay (int inputDay)
{
if (inputDay >= 1 && inputDay <= 31)
day = inputDay;
else
System.out.println("Error: Invalid day parameter");
}
public void setYear (int inputYear)
{
if (inputYear >= 0)
year = inputYear;
else
System.out.println("Error: Invalid year parameter");
}
}
package class3;
//program
public class DateProgram {
public static void main(String[] args)
{
Date date1, date2;
Date date3 = new Date(); //instantiated object
Date notDate3;
date1 = new Date();
date2 = new Date();
//instance variables
date1.day = 24;
date1.year = 2014;
date1.month = "December";
date1.writeOutput(); //invoking the method
//instance variables
date2.day = 5;
date2.month = "July";
date2.year = 1999;
date2.writeOutput();
date2.month = "Hello Falcons";
date2.day = 26;
date2.year = -3;
date2.writeOutput();
date3.month = "Feburary";
System.out.println(date3.getMonth());
int dayValue = date2.getDay(); //save a value
System.out.println(dayValue);
System.out.println(date2.getDay());
date1.setDay(25);
date1.setMonth("March");
date1.setYear(2015);
date1.writeOutput();
date1.setDay(-3);
date1.setYear(-5);
date1.writeOutput();
}
}
public class Person {
//Member Variables
private String name;
private Date born;
private Date died;
//constructor
public Person()
{
name = "Doe";
born = new Date();
died = new Date();
born.setMonth("Jan");
born.setDay(1);
born.setYear(1);
died.setMonth("Jan");
died.setDay(1);
died.setYear(1);
}
//Methods
//Return the name of the person
public String getName()
{
return name;
}
//returns the date this person was born
public Date getBorn()
{
return born;
}
//returns the date this person passed
public Date getDied()
{
return died;
}
//sets the name of the person to inputName
public void setName(String inputName)
{
name = inputName;
}
//sets the birth date of the person
public void setBorn(String inputMonth, int inputDay, int inputYear)
{
Date inputBorn = new Date(); //instantiates a date object
inputBorn.setMonth(inputMonth);
inputBorn.setDay(inputDay);
inputBorn.setYear(inputYear);
born = inputBorn;
}
//sets the date of passing of the person
public void setDied(String inputMonth, int inputDay, int inputYear)
{
Date inputDied = new Date(); //instantiates a date object
inputDied.setMonth(inputMonth);
inputDied.setDay(inputDay);
inputDied.setYear(inputYear);
died = inputDied;
}
}
public class PersonProgram {
public static void main(String[] args)
{
//this declares the Person variable
Person student1;
Person student2;
Person student3 = new Person();
//the new operator is used to instantiate a new person to be stored into
//our variable
student1 = new Person();
student2 = new Person();
student1.setName("Kevin");
student1.setBorn("Dec",9,2015);
student1.setDied("Dec", 1, 1);
System.out.println(student1.getName());
student1.getBorn().writeOutput();
student2.setName("Harris");
student2.setBorn("Jul", 25, 2000);
System.out.println(student2.getName());
student2.getBorn().writeOutput();
int someNumber;
//System.out.println("The value of someNumber is: " + someNumber);
//some number does not have a default value
student3.getBorn().writeOutput();
student3.setBorn("Sep", 14, 2015);
student3.getBorn().writeOutput();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment