Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Midterm 2 Review
1
Object Oriented Programming
Write a Date class. It should contain fields for day,
month, year, number of months per year, and
number of days per month.
Write a constructor to initialize the instance fields
Write a compareTo(Date other) method that
determines which of two Date objects is later in time.
compareTo should return 1 if the “other” Date is
later, -1 if the “this” Date is later, and 0 if they are
the same date.
2
Solution
public class Date
{
public int day;
public int month;
public int year;
public static final int
numMonthsPerYear = 12;
public static final int []
30, 31, 31, 30, 31, 30, 31};
numDaysPerMonth
public Date(int day, int month, int year)
{
this.day = day;
this.month = month;
this.year = year;
}
= {31, 28, 31, 30, 31,
Solution, continued
public int compareTo(Date other)
{
if(other.year>this.year) {
return 1;
} else if(other.year<this.year) {
return -1;
} else if(other.month > this.month) {
return 1;
} else if(other.month < this.month) {
return -1;
} else if(other.day > this.day) {
return 1;
} else if(other.day < this.day) {
return -1;
} else {
return 0;
}
}
}
OOP, continued
Write a Calendar class that holds a separate
Date object for every day in November.
In the main method, have it print out every
date in a sequence, 7 dates per line.
If we modify the fields in the Date to be
private, how can we change Date and
Calendar so that the main method can still
print out the dates?
Solution 1
public class Calendar
{
public Date [] november2009;
public Calendar()
{
november2009 = new Date[Date.numDaysPerMonth[10]];
for(int i=0; i<november2009.length; i++) {
november2009[i] = new Date(i+1,11,2009);
}
}
private static void printDate(Date d)
{
System.out.print(d.month + "/" + d.day + "/" + d.year);
}
public static void main(String [] args)
{
for(int i=0; i<november2009.length; i++) {
printDate(november2009[i]);
if(i%7==6) {
System.out.println();
}
}
}
}
Solution 2
Change Date instance fields to private
Add a toString() method to Date:
public String toString() {
return this.month + “/” + this.day + “/” + this.year;
}
Get rid of printDate(), and change main() in Calendar to use toString:
public static void main(String [] args)
{
for(int i=0; i<november2009.length; i++) {
System.out.print(november2009[i].toString());
if(i%7==6) {
System.out.println();
}
}
}
Array Programming
Write a method that takes two ints as arguments: size
and value. It should return an array with the given size,
whose elements all have the given value.
Write a method that returns the average value of a twodimensional double array.
Write a method that takes an int Num as an argument,
and returns an array containing the first Num numbers in
the Fibonacci sequence.
Do the same with an ArrayList instead
Solutions
public static int [] createConstantArray(
int size, int value)
{
int [] result = new int[size];
for(int i=0; i<result.length; i++) {
result[i] = value;
}
return result;
}
Solutions, continued
public static double average2D(double [][] array)
{
double sum = 0;
int count = 0;
for(int i=0; i<array.length; i++) {
for(int j=0; j<array[i].length; j++) {
sum += array[i][j];
count++;
}
}
return sum / count;
}
Solutions, continued
public static int [] fibonacciSequence(int n)
{
if(n<=0) { return null; }
int [] result = new int[n];
if(n>=1) {
result[0] = 1;
}
if(n>=2) {
result[1] = 1;
}
for(int i=2; i<result.length; i++) {
result[i] = result[i-1] + result[i-2];
}
return result;
}
Solutions, continued
public static ArrayList<Integer> fibonacciSequence(int n)
{
ArrayList<Integer> result = new ArrayList<Integer>();
if(n>=1) {
result.add(1);
}
if(n>=2) {
result.add(1);
}
for(int i=2; i<n; i++) {
int prev = result.get(result.size()-1);
int prev2 = result.get(result.size()-2);
result.add(prev+prev2);
}
return result;
}
File Programming
Create a “FileEncrypter” class that reads in
every word of a file (containing all lower case
alphabetic words), and shifts the letters by some
number x. For instance, if x=2, then
FileEncrypter would change “zebra” to “bgdtc”
(shifting every letter by 2). Store the encrypted
file into a new file.
Write a “FileDecrypter” class that reads in an
encrypted file, and decrypts it. The
FileDecrypter needs to know the number x that
the File Encrypter used, to reverse the process.
Solution: FileEncrypter
import java.util.*;
import java.io.*;
public class FileEncrypter
{
public static void main(String [] args)
throws FileNotFoundException
{
if(args.length<3) {
System.out.println(
“Usage:
java FileEncrypter <infile> <outfile> <encrypt-key>”);
}
int key = Integer.parseInt(args[2]);
encrypt(args[0], args[1], key);
}
Solution: FileEncrypter, continued
public static void encrypt(String infile, String outfile, int
key)
throws FileNotFoundException
{
Scanner in = new Scanner(new File(infile));
PrintWriter pw = new PrintWriter(new File(outfile));
while(in.hasNext()) {
String word = in.next();
String jumble = encryptWord(word, key);
pw.println(jumble);
}
in.close();
pw.close();
}
Solution: FileEncrypter, continued
public static String encryptWord(String word, int key)
{
String result = “”;
for(int i=0; i<word.length(); i++) {
result += encryptLetter(word.charAt(i), key);
}
return result;
}
public static char encryptLetter(char letter, int key)
{
int num = letter + key;
while(num > ‘z’) { num -= 26; }
while(num < ‘a’) { num += 26; }
return (char) num;
}
}
Solution: FileDecrypter
import java.util.*;
import java.io.*;
public class FileDecrypter
{
public static void main(String [] args)
throws FileNotFoundException
{
if(args.length<3) {
System.out.println(
“Usage:
java FileDecrypter <infile> <outfile> <encrypt-key>”);
}
int key = Integer.parseInt(args[2]);
decrypt(args[0], args[1], key);
}
Solution: FileDecrypter, continued
public static void decrypt(String infile, String outfile, int
key)
throws FileNotFoundException
{
Scanner in = new Scanner(new File(infile));
PrintWriter pw = new PrintWriter(new File(outfile));
while(in.hasNext()) {
String word = in.next();
String jumble = decryptWord(word, key);
pw.println(jumble);
}
in.close();
pw.close();
}
Solution: FileDecrypter, continued
public static String decryptWord(String word, int key)
{
String result = “”;
for(int i=0; i<word.length(); i++) {
result += decryptLetter(word.charAt(i), key);
}
return result;
}
public static char decryptLetter(char letter, int key)
{
// add key to encrypt, subtract to decrypt
int num = letter - key;
while(num > ‘z’) { num -= 26; }
while(num < ‘a’) { num += 26; }
return (char) num;
}
}