midterm 2 review 1. 2 object oriented programming write a date class. it should contain fields for...

19
Midterm 2 Review 1

Upload: gervais-gaines

Post on 13-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Midterm 2 Review 1. 2 Object Oriented Programming Write a Date class. It should contain fields for day, month, year, number of months per year, and number

Midterm 2 Review

1

Page 2: Midterm 2 Review 1. 2 Object Oriented Programming Write a Date class. It should contain fields for day, month, year, number of months per year, and number

2

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.

Page 3: Midterm 2 Review 1. 2 Object Oriented Programming Write a Date class. It should contain fields for day, month, year, number of months per year, and number

Solution

public class Date

{

public int day;

public int month;

public int year;

public static final int numMonthsPerYear = 12;

public static final int [] numDaysPerMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

public Date(int day, int month, int year)

{

this.day = day;

this.month = month;

this.year = year;

}

Page 4: Midterm 2 Review 1. 2 Object Oriented Programming Write a Date class. It should contain fields for day, month, year, number of months per year, and number

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;

}

}

}

Page 5: Midterm 2 Review 1. 2 Object Oriented Programming Write a Date class. It should contain fields for day, month, year, number of months per year, and number

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?

Page 6: Midterm 2 Review 1. 2 Object Oriented Programming Write a Date class. It should contain fields for day, month, year, number of months per year, and number

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();}}

}}

Page 7: Midterm 2 Review 1. 2 Object Oriented Programming Write a Date class. It should contain fields for day, month, year, number of months per year, and number

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();}

}}

Page 8: Midterm 2 Review 1. 2 Object Oriented Programming Write a Date class. It should contain fields for day, month, year, number of months per year, and number

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 two-dimensional 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

Page 9: Midterm 2 Review 1. 2 Object Oriented Programming Write a Date class. It should contain fields for day, month, year, number of months per year, and number

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;

}

Page 10: Midterm 2 Review 1. 2 Object Oriented Programming Write a Date class. It should contain fields for day, month, year, number of months per year, and number

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;

}

Page 11: Midterm 2 Review 1. 2 Object Oriented Programming Write a Date class. It should contain fields for day, month, year, number of months per year, and number

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;

}

Page 12: Midterm 2 Review 1. 2 Object Oriented Programming Write a Date class. It should contain fields for day, month, year, number of months per year, and number

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;

}

Page 13: Midterm 2 Review 1. 2 Object Oriented Programming Write a Date class. It should contain fields for day, month, year, number of months per year, and number

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.

Page 14: Midterm 2 Review 1. 2 Object Oriented Programming Write a Date class. It should contain fields for day, month, year, number of months per year, and number

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);

}

Page 15: Midterm 2 Review 1. 2 Object Oriented Programming Write a Date class. It should contain fields for day, month, year, number of months per year, and number

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();

}

Page 16: Midterm 2 Review 1. 2 Object Oriented Programming Write a Date class. It should contain fields for day, month, year, number of months per year, and number

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;

}}

Page 17: Midterm 2 Review 1. 2 Object Oriented Programming Write a Date class. It should contain fields for day, month, year, number of months per year, and number

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);

}

Page 18: Midterm 2 Review 1. 2 Object Oriented Programming Write a Date class. It should contain fields for day, month, year, number of months per year, and number

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();

}

Page 19: Midterm 2 Review 1. 2 Object Oriented Programming Write a Date class. It should contain fields for day, month, year, number of months per year, and number

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 decryptint num = letter - key;while(num > ‘z’) { num -= 26; }while(num < ‘a’) { num += 26; }return (char) num;

}}