CSC 172 Intermediate Progamming

Similar documents
Defining Classes I. 4.1 Class Definitions Information Hiding and Encapsulation Overloading

Handout 7. Defining Classes part 1. Instance variables and instance methods.

CSC-140 Assignment 6

10/22/2018 Programming Data Structures

Lecture Notes: ESC 101

CIS 190: C/C++ Programming. Classes in C++

Abstract Data Types. Different Views of Data:

Common Mistakes with Functions. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

CS 101 Exam 2 Spring Id Name

Objects and Classes. Objects and Classes

Intermediate Programming

CSC 175 Intermediate Programming

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

Intermediate Programming

CMSC 132, Object-Oriented Programming II Summer Lecture 5:

Different kinds of resources provided by a package

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

CMSC 202H. Classes and Objects: Reusing Classes with Composition

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Page 1 of 5. *** person.cpp *** #include "person.h" #include <string> #include <iostream> using namespace std; *** person.h ***

Introduction to Algorithms and Data Structures

Topic 03 - Objects and Classes. I. Classes and Objects Casual Preview. Topic 03. VIII. Benefits of Encapsulation. XIII. Overloading methods, Signature

CSCI-1200 Data Structures Fall 2018 Lecture 3 Classes I

Sequential Search (Searching Supplement: 1-2)

Programming Language Concepts: Lecture 2

Ch 6. Structures and Classes

Programming Assignment #2

Programming Language Concepts: Lecture 2

Chapter 7 Classes & Objects, Part B

10/30/2010. Introduction to Control Statements. The if and if-else Statements (cont.) Principal forms: JAVA CONTROL STATEMENTS SELECTION STATEMENTS

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Data Types. Lecture2: Java Basics. Wrapper Class. Primitive data types. Bohyung Han CSE, POSTECH

EECS2030 Fall 2016 Preparation Exercise for Lab Test 2: A Birthday Book

Classes and Objects Miscellany: I/O, Statics, Wrappers & Packages. CMSC 202H (Honors Section) John Park

CSE 142, Autumn 2010 Final Exam Wednesday, December 15, Name:

Chapter 4 Defining Classes I

CSEN 202: Introduction to Computer Programming Spring term Final Exam

Introduction to Computer Programming

Chapter 4. Defining Classes I

Software and Programming 1

CS 302 Week 9. Jim Williams

A Founda4on for Programming

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

Condi(onals and Loops

Intermediate Programming. Runtime Errors

Course Content. Objectives of Lecture 18 Black box testing and planned debugging. Outline of Lecture 18

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

JavaScript by Vetri. Creating a Programmable Web Page

Chapter 6 Lab Classes and Objects

Chapter 6 Lab Classes and Objects

BCS THE CHARTERED INSTITUTE FOR IT. BCS HIGHER EDUCATION QUALIFICATIONS BCS Level 5 Diploma in IT OBJECT ORIENTED PROGRAMMING

Midterm Examination (MTA)

ECOM 2324 COMPUTER PROGRAMMING II

Exercise 1: Class Employee: public class Employee { private String firstname; private String lastname; private double monthlysalary;

OO Programming Concepts. Classes. Objects. Chapter 8 User-Defined Classes and ADTs

EXAMPLE ANSWERS. EXAM IN COURSE TDT4100 Object-Oriented Programming / IT1104 Programming, Advanced Course. Tuesday 29. Mai

CS212 Midterm. 1. Read the following code fragments and answer the questions.

CS141 Programming Assignment #8

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2009) Midterm Examination

Programming Logic and Design Sixth Edition

Chapter 9 Objects and Classes. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved.

CSE 21 Intro to Computing II. Post-Midterm Review

Introduction to Computer Programming

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited

Class design guidelines. Most of this material comes from Horstmann, Cay: Object-Oriented Design & Patterns (chapter 3)

CHAPTER 12 INHERITANCE AND CLASS HIERARCHIES

Practice Problems: Instance methods

Aerospace Software Engineering

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2013 CISC2200 Yanjun Li 1. Fall 2013 CISC2200 Yanjun Li 2

Object-Oriented Programming in Java

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2017 CISC2200 Yanjun Li 1. Fall 2017 CISC2200 Yanjun Li 2

Chapter 9 Objects and Classes. OO Programming Concepts. Classes. Objects. Motivations. Objectives. CS1: Java Programming Colorado State University

CONDITIONAL EXECUTION: PART 2

Jones & Bartlett Learning, LLC. NOT FOR SALE OR DISTRIBUTION

H212 Introduction to Software Systems Honors

What is the return type of getnameofmonth?

The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II Instructor: Dr. Bowen Hui. Tuesday, April 19, 2016

09/08/2017 CS2530 INTERMEDIATE COMPUTING 9/8/2017 FALL 2017 MICHAEL J. HOLMES UNIVERSITY OF NORTHERN IOWA TODAY S TOPIC: Exceptions and enumerations.

Chapter 4. Defining Classes I

public static boolean isoutside(int min, int max, int value)

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

Assignment 8B SOLUTIONS

11/19/2014. Objects. Chapter 4: Writing Classes. Classes. Writing Classes. Java Software Solutions for AP* Computer Science A 2nd Edition

SFWR ENG/COMP SCI 2S03 Principles of Programming SOLUTIONS

CSE 142, Autumn 2007 Midterm Exam, Friday, November 2, 2007

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

A Foundation for Programming

Principles of Object Oriented Programming. Lecture 4

Lab Exercise 1. Objectives: Part 1. Introduction

CS Programming I: Arrays

Arrays. What if you have a 1000 line file? Arrays

CS 101 Spring 2007 Midterm 2 Name: ID:

JOSE LUIS JUAREZ VIVEROS com) has a. non-transferable license to use this Student Guide

Classes. Classes as Code Libraries. Classes as Data Structures. Classes/Objects/Interfaces (Savitch, Various Chapters)

Patterns in Java Unfinished Symmetry

ITI Introduction to Computing II

Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION. describe the difference between direct addressing and indirect addressing

Transcription:

CSC 172 Intermediate Progamming Lecture 2 A Review of Programming With Classes What Is A Class? A class is similar to a data type, except that the data items that it contains, the data types of which they are declared and the operations performed by them are all declared by the programmer. An instance of a class (similar to a variable of a given type) is called an object. The data contained within an object is called a property. The actions (or operations) performed by an object is called a method. 1

Declaring Objects To declare objects of a given class we write: MyClass object1 = new MyClass(); If I write MyClass object2; Object2 is null (without value) until I write Object2 = new myobject(); A Simple Class -DateFirstTry.java public class DateFirstTry { public String month; public int day; public int year; // A four-digit number // writeoutput() - Write the date in a // proper format public void writeoutput() { System.out.println(month + " " + day + ", " + year); 2

TestFirstDate.java public class TestFirstDate { public static void main(string[] args) { DateFirstTry date1, date2; // Call the constructors - Now we can use // them date1 = new DateFirstTry(); date2 = new DateFirstTry(); //Initialize date1 date1.month = "December"; date1.day = 31; date1.year = 2006; System.out.println("date1:"); date1.writeoutput(); One instance of an object //Initialize date2 date2.month = "July"; date2.day = 4; date2.year = 1776; System.out.println("date2:"); date2.writeoutput(); Another instance of an object Output date1: December 31, 2006 date2: July 4, 1776 3

Invoking A Method Assume that we have these methods declared: public double mymethod(); public String yourmethod(); public void ourmethod() mymethod and yourmethod will always return a double and a String respectively and ourmethod will not return any value but all three will perform some action. Instances Of A Method When we invoke a method, a set of instructions are performed using the properties belonging to the object. object1.mymethod() uses the property values belonging to object1 and object2.mymethod()belonging to object2. 4

DateSecondTry.java import java.util.scanner; public class DateSecondTry { private String month; private int day; private int year; // A four-digit number // writeoutput() - Write the date in a proper // format public void writeoutput() { System.out.println(month + " " + day + ", " + year); // readinput() - Read a date from the keyboard public void readinput() { Scanner keyb = new Scanner(System.in); // Prompt the user for the month, // day and year and read them System.out.println("Month?"); month = keyb.next(); System.out.println("Day?"); day = keyb.nextint(); System.out.println("Year?"); year = keyb.nextint(); 5

// getday() - Allows you to get the data // property public int getday() { return day; // getyear() - Allows you to get the data // property public int getyear() { return year; // getmonth() - Returns the month as a number public int getmonth() { if (month.equals("january")) return 1; if (month.equals("february")) return 2; if (month.equals("march")) return 3; if (month.equals("april")) return 4; if (month.equals("may")) return 5; if (month.equals("june")) return 6; if (month.equals("july")) return 7; 6

if (month.equals("august")) return 8; if (month.equals("september")) return 9; if (month.equals("october")) return 10; if (month.equals("november")) return 11; if (month.equals("december")) return 12; else { System.out.println("Fatal error " + "not a valid month"); // Since it isn't valid, terminate the // program System.exit(0); // The compilers insists on this return return 0; 7

Local Variables Just the main method can have variables that are available for its exclusive use, so can other methods within a class. TestSecondDate.java public class TestSecondDate { public static void main(string[] args) { DateSecondTry date = new DateSecondTry(); //Initialize date1 date.readinput(); Invoking a void method Invoking an int method int daynumber = date.getday(); System.out.println("That is the " + daynumber + "th day of the month."); 8

DateThirdTry.java import java.util.scanner; public class DateThirdTry { private String month; private int day; private int year; // A four-digit number // setdate() Sets all three properties public void setdate(int newmonth, int newday, int newyear) { month = monthstring(newmonth); day = newday; year = newyear; Local variables (that are also formal parameters) // monthstring() - Returns the string for the // month specified in numeric // format public String monthstring(int monthnumber) { switch(monthnumber) { case 1: return "January"; case 2: return "February"; case 12: return "December"; default: System.out.println("Fatal error - not a + " valid month"); System.exit(0); return "Error"; public void writeoutput() { 9

public void readinput() { public int getday() { public int getyear() { public int getmonth() { // getyear() - Allows you to get the data property public int getyear() { return year; // getmonth() - Returns the month as a number public int getmonth() { 10

TestThirdData.java public class TestThirdDate { public static void main(string[] args) { Date date = new Date(); int year = 1882; date.setdate(6, 17, year); date.writeoutput(); Output June 17, 1882 The actual parameters whose values are copied over into setdate s formal parameters Ownership of Methods Every method, whether or not there is a parameter list, has some parameters passed to it subtly the properties belonging to that object. Writing today.writeoutput() is equivalent to writing System.out.println(today.month + " " + today.day + ", " + today.year); Because the references to month, day and year all refer to the properties of the object that s doing the calling. 11

this If you wish to make it clear which that the property or method being used belongs to the calling obect, use the reserved word this. this serves as a hidden parameter because it is always implied when no object s name is used before a property or method. this An Example // writeoutput() - Write the date in a proper // format public void writeoutput() { System.out.println(month + " " + day + ", " + year); is equivalent to: // writeoutput() - Write the date in a proper // format public void writeoutput() { System.out.println(this.month + " " + this.day + ", " + this.year); 12

Methods That May Be Expected There are certain methods that are found quite frequently and therefore are usually expected: tostring and equals are among them. DateFourthTry.java import java.util.scanner; public class DateFourthTry { private String month; private int day; private int year; // A four-digit number // tostring() - Converts the date into a string public String tostring() { return (month + " " + day + ", " + year); public void writeoutput() { 13

// equals() - Returns true if the parameter and // the object are the same data // Returns false otherwise public boolean equals(datefourthtry otherdate) { return( (month.equals(otherdate.month)) && (day == otherdate.day) && (year == otherdate.year)); // precedes() - Returns true if the object is // an earlier date than the // parameter // Returns false otherwise public boolean precedes(datefourthtry otherdate) { return( (year < otherdate.year) (year == otherdate.year && getmonth() < otherdate.getmonth()) (year == otherdate.year && month.equals(otherdate.month) && day < otherdate.day)); 14

public void setdate(int newmonth, int newday, int newyear) { public String monthstring(int monthnumber) { public void readinput() { public int getday() { public int getyear() { public int getmonth() { EqualsAndToStringDemo.java public class EqualsAndToStringDemo { public static void main(string[] args) { DateFourthTry date1 = new DateFourthTry(), date2 = new DateFourthTry(); date1.setdate(6, 17, 1882); date2.setdate(6, 17, 1882); if (date1.equals(date2)) System.out.println(date1 + " equals " + date2); else System.out.println(date1 + " does not equal " + date2); 15

date1.setdate(7, 28, 1750); if (date1.precedes(date2)) System.out.println(date1 + " comes before " + date2); else System.out.println(date2 + " comes before or is equal to " + date1); Output June 17, 1882 equals June 17, 1882 July 28, 1750 comes before June 17, 1882 Encapsulation Encapsulation means that data belonging to a class is hidden from the classes that use is methods. Public properties and methods may be used by objects of other classes. Private properties and methods may not be used by objects of other classes. 16

Accessors and Mutators Accessor methods make private data items available to other classes. Mutators methods allow private data values to be changed. Typically, accessor methods will have names with the prefix get and mutator methods will have names with the prefix set. DateFifthTry.java import java.util.scanner; public class DateFifthTry { private String month; private int day; private int year; // A four-digit number public void writeoutput() { 17

// readinput() - Read a date from the keyboard public void readinput() { boolean tryagain = true; Scanner keyb = new Scanner(System.in); // Keep asking until the user enters a // valid date while (tryagain) { System.out.println ("Enter month, day and year"); System.out.println("as three integers:"); System.out.println("do not use commas " + + " or other punctuations."); int monthinput = keyb.nextint(); int dayinput = keyb.nextint(); int yearinput = keyb.nextint(); New version of readinput ensures that the data is reasonable. if (dateok(monthinput, dayinput, yearinput)) { setdate(monthinput, dayinput, yearinput); tryagain = false; else System.out.println ("Illegal date. Reenter input."); 18

// setdate() - A mutator that expects the month // in numeric format public void setdate(int month, int day, int year) { if (dateok(month, day, year)) { this.month = monthstring(month); this.day = day; this.year = year; else { System.out.println ("Fatal error - invalid date"); System.exit(0); // setmonth() - A mutator for month that checks // whether it is a valid value for // month public void setmonth(int monthnumber) { if ((monthnumber <= 0)&&(monthNumber > 12)) { System.out.println ("Fatal error - Invalid month"); System.exit(0); else month = monthstring(monthnumber); 19

// setday() - A mutator for month that checks // whether it is a valid value for day public void setday(int day) { if ((day <= 0) && (day > 31)) { System.out.println ("Fatal error - Invalid day"); System.exit(0); else this.day = day; // setyear() - A mutator for month that checks // whether it is a valid value for // year public void setyear(int year) { if ((year < 1000) && (year > 9999)) { System.out.println ("Fatal error - Invalid year"); System.exit(0); else this.year = year; 20

// equals() - Returns true if the parameter and // the object are the same data // Returns false otherwise public boolean equals(datefifthtry otherdate) { return ((month.equalsignorecase(otherdate.month)) && (day == otherdate.day) && (year == otherdate.year)); // precedes() - Returns true if the object is an // earlier date than the parameter // Returns false otherwise public boolean precedes(datefifthtry otherdate) { return( (year < otherdate.year) (year == otherdate.year && getmonth() < otherdate.getmonth()) (year == otherdate.year && month.equals(otherdate.month) && day < otherdate.day)); 21

// getmonth() - Returns the month as a number public int getmonth() { if (month.equalsignorecase("january")) return 1; if (month.equalsignorecase("february")) return 2; if (month.equalsignorecase("december")) return 12; else { System.out.println ("Fatal error - not a valid month"); //Since it isn't valid, terminate the program System.exit(0); // The compilers insists on this return return 0; // getday() Accessor for the day public int getday() { return day; // getyear() Accessor for the year public int getyear() { return year // tostring() - Converts the date into a string public String tostring() { return (month + " " + day + ", " + year); 22

// dateok() - Returns true if the date is valid // Returns false if the date is // invalid // Assumes that the month is in numeric // format public boolean dateok(int monthint, int dayint, int yearint) { return( (monthint >= 1) && (monthint <= 12) && (dayint >= 1) && (dayint <= 31) && (yearint >= 1000) && (yearint <= 9999) ); // monthstring() - Returns the string for the // month specified in numeric // format public String monthstring(int monthnumber) { Preconditions and Postconditions A precondition states what is assumed to be true when the method is first called. A postcondition states what is assumed to be true when control is returned to the calling method. 23

Overloading Methods Frequently there can be more than one way of doing a task depending on what is available. Similarly there may be more than one method that shares a common name that do essentially the same job, but the details of the methods may differ; they must differ in parameter lists.this is called overloading a method. Rules For Overloading Although overloaded methods can have different return types, it is essential that they have different parameter lists. They can differ in the number of parameters or in the type of parameters. Their exact algorithms can even be different, although the assumption is usually made that they are different ways of doing the same jobs with different types of data. 24

DataSixthTry.java import java.util.scanner; public class DateSixthTry { private String month; private int day; private int year; // A four-digit number // setdate() - A mutator that expects the month // in numeric format public void setdate(int month, int day, int year) { if (dateok(month, day, year)) { this.month = monthstring(month); this.day = day; this.year = year; else { System.out.println ("Fatal error - invalid date"); System.exit(0); 25

// setdate() - A mutator that expects the month // in string format public void setdate(string monthstring, int day, int year) { if (dateok(monthstring, day, year)) { this.month = monthstring; this.day = day; this.year = year; else { System.out.println("Fatal error"); System.exit(0); //setdate() - A mutator that assumes that the // day is the first day of the // specified year public void setdate(int year) { setdate(1, 1, year); // dateok() - Returns true if the date is valid // Returns false if the date is // invalid // Assumes that the month is in // numeric format public boolean dateok(int monthint, int dayint, int yearint) { return( (monthint >= 1) && (monthint <= 12) && (dayint >= 1) && (dayint <= 31) && (yearint >= 1000) && (yearint <= 9999) ); 26

// dateok() - Returns true if the date is valid // Returns false if the date is // invalid // Assumes that the month is in // string format public boolean dateok(string monthstring, int dayint, int yearint) { return( monthok(monthstring) && (dayint >= 1) && (dayint <= 31) && (yearint >= 1000) && (yearint <= 9999) ); //monthok() - Returns true if the month is one // of the twelve valid strings // Returns false other if not public boolean monthok(string month) { return (month.equals("january") month.equals("december")); Constructors A constructor is a special type of method that is called when the object is created. When you write MyClass myobject = new MyClass(); you are calling the constructor. 27

Default Constructor A default constructor does not have any parameters; it assumes that some initial value must be supplied: // Date() - A default constructor for // the class public Date() { month = "January"; day = 1; year = 1000; Multiple Initializing Constructors An initialization constructor provides initial values for the object. Sometimes, there may be multiple versions, depending on the type of data supplied: // Date() - A constructor that accepts the // month in numeric format public Date(int monthint, int day, int year) { setdate(monthint, day, year); 28

// Date() - A constructor that accepts the // month in string format public Date(String monthstring, int day, int year) { setdate(monthstring, day, year); //Date() - A constructor that assumes that the // day is the first day of the // specified year public Date(int year) { setdate(1, 1, year); ConstructorsDemo.java public class ConstructorsDemo { public static void main(string[] arg) { Date date1 = new Date("December", 16, 1770), date2 = new Date(1, 27, 1756), date3 = new Date(1882), date4 = new Date(); System.out.println("Whose birthday is " + date1 + "?"); System.out.println("Whose birthday is " + date2 + "?"); System.out.println("Whose birthday is " + date3 + "?"); System.out.println("Whose birthday is " + date4 + "?"); 29

Output Whose birthday is December 16, 1770? Whose birthday is January 27, 1756? Whose birthday is January 1, 1882? Whose birthday is January 1, 1000? The Complete Date.java import java.util.scanner; public class Date { private String month; private int day; private int year; // A four-digit number // Date() - A default constructor for the class public Date() { month = "January"; day = 1; year = 1000; 30

// Date() - A constructor that accepts the // month in numeric format public Date(int monthint, int day, int year) { setdate(monthint, day, year); // Date() - A constructor that accepts the // month in string format public Date(String monthstring, int day, int year) { setdate(monthstring, day, year); //Date() - A constructor that assumes that the // day is the first day of the // specified year public Date(int year) { setdate(1, 1, year); // Date() - An copy constructor public Date (Date adate) { if (adate == null) { // Not a real date System.out.println("Fatal error."); System.exit(0); month = adate.month; day = adate.day; year = adate.year; 31

// dateok() - Returns true if the date is valid // Returns false if the date is // invalid // Assumes that the month is in // numeric format public boolean dateok(int monthint, int dayint, int yearint) { return( (monthint >= 1) && (monthint <= 12) && (dayint >= 1) && (dayint <= 31) && (yearint >= 1000) && (yearint <= 9999)); // dateok() - Returns true if the date is valid // Returns false if the date is // invalid // Assumes that the month is in // string format public boolean dateok(string monthstring, int dayint, int yearint) { return( monthok(monthstring) && (dayint >= 1) && (dayint <= 31) && (yearint >= 1000) && (yearint <= 9999)); 32

//monthok() - Returns true if the month is one // of the twelve valid strings // Returns false other if not public boolean monthok(string month) { return (month.equals("january") month.equals("february") month.equals("march") month.equals("april") month.equals("may") month.equals("june") month.equals("july") month.equals("august") month.equals("september") month.equals("october") month.equals("november") month.equals("december")); // monthstring() - Returns the string for the month // specified in numeric format public String monthstring(int monthnumber) { switch(monthnumber) { case 1: return "January"; case 2: return "February"; case 3: return "March"; case 4: return "April"; case 5: return "May"; case 6: return "June"; case 7: return "July"; case 8: return "August"; case 9: return "September"; case 10: return "October"; case 11: return "November"; case 12: return "December"; default: System.out.println ("Fatal error - not a valid month"); System.exit(0); return "Error"; 33

// tostring() - Converts the date into a string public String tostring() { return (month + " " + day + ", " + year); // equals() - Returns true if the parameter and // the object are the same data // Returns false otherwise public boolean equals(date otherdate) { return( (month.equals(otherdate.month)) && (day == otherdate.day) && (year == otherdate.year)); // precedes() - Returns true if the object is // an earlier date than the // parameter // Returns false otherwise public boolean precedes(date otherdate) { return((year < otherdate.year) (year == otherdate.year && getmonth() < otherdate.getmonth()) (year == otherdate.year && month.equals(otherdate.month) && day < otherdate.day)); // writeoutput() - Write the date in a proper // format public void writeoutput() { System.out.println(month + " " + day + ", " + year); 34

// readinput() - Read a date from the keyboard public void readinput() { boolean tryagain = true; Scanner keyb = new Scanner(System.in); // Keep asking until the user enters a valid // date while (tryagain) { System.out.println ("Enter month, day and year."); System.out.println("Do not use a comma."); String monthinput = keyb.next(); int dayinput = keyb.nextint(); int yearinput = keyb.nextint(); if (dateok (monthinput, dayinput, yearinput)) { setdate(monthinput, dayinput, yearinput); tryagain = false; else System.out.println ("Illegal date. Reenter input."); // getday() - Accessor for day public int getday() { return day; // getyear() Accessor for year public int getyear() { return year; 35

// getmonth() - Returns the month as a number public int getmonth() { if (month.equalsignorecase("january")) return 1; if (month.equalsignorecase("february")) return 2; if (month.equalsignorecase("march")) return 3; if (month.equalsignorecase("april")) return 4; if (month.equalsignorecase("may")) return 5; if (month.equalsignorecase("june")) return 6; if (month.equalsignorecase("july")) return 7; if (month.equalsignorecase("august")) return 8; if (month.equalsignorecase("september")) return 9; if (month.equalsignorecase("october")) return 10; if (month.equalsignorecase("november")) return 11; if (month.equalsignorecase("december")) return 12; else { System.out.println ("Fatal error - not a valid month"); //Since it isn't valid, terminate the program System.exit(0); // The compilers insists on this return return 0; // setdate() - A mutator that expects the month // in numeric format public void setdate(int month, int day, int year) { if (dateok(month, day, year)) { this.month = monthstring(month); this.day = day; this.year = year; else { System.out.println ("Fatal error - invalid date"); System.exit(0); 36

// setdate() - A mutator that expects the month // in string format public void setdate(string monthstring, int day, int year) { if (dateok(monthstring, day, year)) { this.month = monthstring; this.day = day; this.year = year; else { System.out.println("Fatal error"); System.exit(0); //setdate() - A mutator that assumes that the // day is the first day of the // specified year public void setdate(int year) { setdate(1, 1, year); // setmonth() - A mutator for month that checks // whether it is a valid value for // month public void setmonth(int monthnumber) { if (monthnumber <= 0 && monthnumber > 12) { System.out.println ("Fatal error - Invalid month"); System.exit(0); else month = monthstring(monthnumber); 37

// setday() - A mutator for day that checks // whether it is a valid value for // day public void setday(int day) { if ((day <= 0) && (day > 31)) { System.out.println ("Fatal error - Invalid day"); System.exit(0); else this.day = day; // setyear() - A mutator for year that checks // whether it is a valid value for year public void setyear(int year) { if ((year < 1000) && (year > 9999)) { System.out.println("Fatal error - Invalid year"); System.exit(0); else this.year = year; 38