Programming in Java - Homework Answers: Classes, Strings, Inheritance
public class Date { private int month; private int day; private int year; public Date() { month = 1; day = 1; year = 1996; } public Date(int mon, int dy, int yr) { month = mon; day = dy; year = yr; } public Date nextDay() { Date next = new Date(); next.month = month; next.day = day; next.year = year; if (month == 2) if (day < 28) next.day++; else { next.day = 1; next.month = 3; } else if (month == 4 || month == 6 || month == 9 || month == 11) if (day < 30) next.day++; else { next.day = 1; next.month = month++; } else if (month == 12 && day == 31) { next.month = 1; next.day = 1; next.year++; } else if (day < 31) next.day++; else { next.day = 1; next.month = month++; } return next; } public boolean before(Date second) { if (second.year < year) return false; if (second.year > year) return true; if (second.month < month) return false; if (second.month > month) return true; if (second.day < day) return false; if (second.day > day) return true; return false; } public String toString() { String hold; hold = Integer.toString(month) + "/" + Integer.toString(day) + "/" + Integer.toString(year); return hold; } }
import java.io.PrintWriter; public class DateTest { public static void main(String[] args) { PrintWriter stdout = new PrintWriter(System.out,true); Date first = new Date(); Date next = new Date(10,26,99); Date later = next.nextDay().nextDay(); Date mine = new Date(11,29,99); if (first.before(mine)) stdout.println("Earlier"); else stdout.println("Later"); Date [] theDates = new Date[10]; theDates[0] = new Date(4,1,99); theDates[1] = new Date(5,1,99); theDates[2] = new Date(6,1,99); for (int i = 0 ; i < 3 ; i++) stdout.println(theDates[i]); } }
public class StringOps { public static int countChar(String str, char find) { int count = 0; for (int i = 0 ; i < str.length() ; i++) if (str.charAt(i) == find) count++; return count; } public static String removeChar(String str, char remove) { char hold; StringBuffer remstr = new StringBuffer(); for (int i = 0 ; i < str.length() ; i++) { hold = str.charAt(i); if (hold != remove) remstr.append(hold); } String retstr = new String(remstr); return retstr; } }
import java.io.PrintWriter; public class StringTest { public static void main(String[] args) { PrintWriter stdout = new PrintWriter(System.out,true); String road = "The road goes ever on and on"; int nume = StringOps.countChar(road,'e'); stdout.println("There are " + nume + "e's in the String."); String nors = StringOps.removeChar(road,'r'); stdout.println("After removing r's the String is: " + nors); } }
import java.io.PrintWriter; public class Person { protected String name; protected String address; public Person() { } public Person(String nn,String aa) { name = new String(nn); address = new String(aa); } public void print() { PrintWriter stdout = new PrintWriter(System.out,true); stdout.println("Name: " + name); stdout.println("Address: " + address + "\n"); } public class Employee extends Person { protected long SSN; protected int salary; public Employee() { } public Employee(String nn,String aa,long snum,int sal) { super(nn,aa); SSN = snum; salary = sal; } public void print() { PrintWriter stdout = new PrintWriter(System.out,true); stdout.println("Name: " + name); stdout.println("Address: " + address); stdout.println("SSN: " + SSN); stdout.println("Salary " + salary + "\n"); } public void raise(int percent) { salary = salary + (int)(salary * percent * .01); } }
1. valid 2. valid 3. valid 4. invalid, because you cannot have a subclass reference point to a superclass object
import java.io.PrintWriter; public class PerEmplTest { public static void main(String[] args) { Person [] list = new Person[4]; list[0] = new Person("Sam", "Bagshot Row"); list[1] = new Person("Saruman","Orthanc"); list[2] = new Employee("Merry","Buckland",111223333,62500); list[3] = new Employee("Pippin","Great Smials",222334444,64000); for (int i = 0 ; i < list.length ; i++) list[i].print(); } }
Name: Sam Address: Bagshot Row Name: Saruman Address: Orthanc Name: Merry Address: Buckland SSN: 111223333 Salary 62500 Name: Pippin Address: Great Smials SSN: 222334444 Salary 64000
Email Me | Office Hours | My Home Page | Department Home | MCC Home Page