2009年4月27日 星期一

Lab ADT

Define a Complex class and write an object oriented program to compute (2+3i)+(4+5i) in Java.
The methods should include an access and a mutator.

class程式碼
------------------------------------------------<程式碼開始>--------------------------------------
public class ADTClass {
private int realNum = 0, imaginaryNum = 0;
public ADTClass(int realNum, int imaginaryNum) {
this.realNum = realNum;
this.imaginaryNum = imaginaryNum;
}
public ADTClass add(ADTClass f) {
ADTClass ans = new ADTClass(1, 1);
ans.realNum = this.realNum + f.realNum;
ans.imaginaryNum = this.imaginaryNum + f.imaginaryNum;
return ans;
}
public String toString() {
return realNum + "+" + imaginaryNum + "i";
}
}
------------------------------------------------<程式碼結束>--------------------------------------
主程式碼
------------------------------------------------<程式碼開始>--------------------------------------
import java.util.*;
public class ADT {
    public static void main (String args[]) {
ADTClass f1, f2;
f1 = new ADTClass(2, 3);
f2 = new ADTClass(4, 5);
System.out.println("f1 = " + f1.toString());
System.out.println("f2 = " + f2.toString());
System.out.println("f1 + f2 = " + f1.add(f2).toString());
    }
}
------------------------------------------------<程式碼結束>--------------------------------------
附註:

Class definition 3

Do Display 4.7 (3rd, 2nd ed.) or 4.5 (1st ed.). Then use Display 4.8 to call 4.7.

Question
In Display 4.7, if the method setDate has the parameter as setDate(int month, int day, int year), what kind of changes should be made in its body of codes?


class程式碼
------------------------------------------------<程式碼開始>--------------------------------------
import java.util.Scanner;
public class DateFourthTry {
private int month;
private int day;
private int year;
public void setDate(int month, int day, int year) {
this.month = month;
this.day = day;
this.year = year;
}
public String toString() {
return (month + " " + day + ", " + year);
}
public int getDay() {
return day;
}
public int getYear() {
return year;
}
public int getMonth() {
return month;
}
public boolean equals(DateFourthTry otherDate) {
return( (month == otherDate.month) && (day == otherDate.day) && (year == otherDate.year) );
}
public boolean precedes(DateFourthTry otherDate) {
return ( (year < otherDate.year) || 
(year == otherDate.year && getMonth() < otherDate.getMonth() ) || 
(year == otherDate.year && month == otherDate.month && day < otherDate.day) );
}
}
------------------------------------------------<程式碼結束>--------------------------------------
主程式碼
------------------------------------------------<程式碼開始>--------------------------------------
import java.util.*;
public class Classdefinition3 {
    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.print(date1 + " does not equals " + date2);
date1.setDate(7, 28, 1750);
if(date1.precedes(date2))
System.out.println(date1 + " comes before " + date2);
else
System.out.print(date2 + " comes before or is equal to " + date1);
    }
}
------------------------------------------------<程式碼結束>--------------------------------------
附註:

Homework 4-13-2009 Fraction Multiplication

Write a program to implement a method that can multiply 2 fractions. You will implement a class called Fraction consisting of a numerator and a denominator. The multiplication of
2 fractions should be equal to a fraction.
Use 1/2 * 1/3 as the test.

Hints:
Fraction f1, f2;
f1.multiply(f2);


due date: 4/27/2009


class程式碼
------------------------------------------------<程式碼開始>--------------------------------------
public class Fractions {
private int numerator, denominator;
public Fractions(int numerator, int denominator) {
this.numerator = numerator;
this.denominator = denominator;
}
public Fractions add(Fractions f) {
int x, a, b;
Fractions ans = new Fractions(1, 1);
ans.numerator = f.denominator * this.numerator + f.numerator * this.denominator;
ans.denominator = f.denominator * this.denominator;
ans.numerator = ans.numerator / reduction (ans.numerator, ans.denominator);
ans.denominator = ans.denominator / reduction (ans.numerator, ans.denominator);
return ans;
}
public Fractions multiply(Fractions f) {
Fractions ans = new Fractions(1, 1);
ans.numerator = f.numerator * this.numerator;
ans.denominator = f.denominator * this.denominator;
ans.numerator = ans.numerator / reduction (ans.numerator, ans.denominator);
ans.denominator = ans.denominator / reduction (ans.numerator, ans.denominator);
return ans;
}
private int reduction(int a, int b) {
int x;
if(a<b) {
x = a;
a = b;
b = x;
}
while (b != 0) {
x = a % b;
a = b;
b = x;
}
return a;
}
public String toString()
{
return (numerator + "/" + denominator);
}
}
------------------------------------------------<程式碼結束>--------------------------------------
主程式碼
------------------------------------------------<程式碼開始>--------------------------------------
import java.util.*;
public class Homework04132009FM {
    public static void main (String args[]) {
Fractions f1, f2, f3, f4;
        f1 = new Fractions(4, 2);
f2 = new Fractions(1, 3);
System.out.println("f1 = " + f1.toString());
System.out.println("f2 = " + f2.toString());
System.out.println("f1 + f2 = " + f1.add(f2).toString());
System.out.println("f1 * f2 = " + f1.multiply(f2).toString() + "\n");
        f3 = new Fractions(5, 6);
f4 = new Fractions(2, 9);
System.out.println("f3 = " + f3.toString());
System.out.println("f4 = " + f4.toString());
System.out.println("f3 + f4 = " + f3.add(f4).toString());
System.out.println("f3 * f4 = " + f3.multiply(f4).toString() + "\n");
    }
}
------------------------------------------------<程式碼結束>--------------------------------------
附註:

2009年4月13日 星期一

lab Fraction equality test

Write a program to implement a method that can check whether 2 fractions are equal. You will implement a class called Fraction consisting of a numerator and a denominator. The equality test of 2 fractions should return a boolean value.

Use the following as the tests.

* 1/2, 2/4
* 5/6, 6/7


Hints:
Fraction f1, f2;
f1.equals(f2);


class程式碼
------------------------------------------------<程式碼開始>--------------------------------------
public class Fraction {
private int numerator, denominator;
public Fraction(int numerator, int denominator) {
this.numerator = numerator;
this.denominator = denominator;
}
boolean equals(Fraction f) {
if(f.denominator * this.numerator - f.numerator * this.denominator == 0)
return true;
else
return false;
}
public String toString()
{
return (numerator + "/" + denominator);
}
}
------------------------------------------------<程式碼結束>--------------------------------------
主程式碼
------------------------------------------------<程式碼開始>--------------------------------------
import java.util.*;
public class Fractionequalitytest {
    public static void main (String args[]) {
        Fraction f1, f2;
        f1 = new Fraction(1, 2);
f2 = new Fraction(2, 4);
System.out.println("f1 = " + f1.toString());
System.out.println("f2 = " + f2.toString());
if(f1.equals(f2))
System.out.println("f1 = f2\n");
else
System.out.println("f1 != f2\n");
f1 = new Fraction(5, 6);
f2 = new Fraction(6, 7);
System.out.println("f1 = " + f1.toString());
System.out.println("f2 = " + f2.toString());
if(f1.equals(f2))
System.out.println("f1 = f2\n");
else
System.out.println("f1 != f2\n");
    }
}
------------------------------------------------<程式碼結束>--------------------------------------
附註:

lab Fraction Addition

Write a program to implement a method that can do additions of 2 fractions. You will implement a class called Fraction consisting of a numerator and a denominator. The additions of
2 fractions should be equal to a fraction.
Use 1/2+1/3 as the test.

Hints:
Fraction f1, f2;
f1.add(f2);




class程式碼
------------------------------------------------<程式碼開始>--------------------------------------
public class Fractions {
public int numerator, denominator;
Fractions(int numerator, int denominator) {
this.numerator = numerator;
this.denominator = denominator;
}
public void add(Fractions f) {
int numerators, denominators;
numerators = f.denominator * this.numerator + f.numerator * this.denominator;
denominators = f.denominator * this.denominator;
System.out.println(numerators + "/" + denominators);
}
}

------------------------------------------------<程式碼結束>--------------------------------------
主程式碼
------------------------------------------------<程式碼開始>--------------------------------------
import java.util.*;
public class FractionAddition {
    public static void main (String args[]) {
Fractions f1, f2;
        f1 = new Fractions(1, 2);
f2 = new Fractions(1, 3);
f1.add(f2);
    }
}
------------------------------------------------<程式碼結束>--------------------------------------
附註:本次lab的寫法只能滿足本題目的用法,對於需要持續相加時會有問題,解決用法請參考下個lab。

Homework 3-30-2009: counter

Define a class called Counter whose objects count things. An object of this class records a count that is a nonnegative integer. Include methods to set the counter to 0, to increase the count by 1, and to decrease the count by 1. Include an accessor method that returns the current count value and a method that outputs the count to the screen. Write a program to test


counter.reset();
counter.inc();
counter.inc();
counter.dec();
counter.output();



class程式碼
------------------------------------------------<程式碼開始>--------------------------------------
public class Counter
{
public int count = 0;
public void reset()
{
count = 0;
}
public void inc()
{
count++;
}
public void dec()
{
count--;
}
public void output()
{
System.out.println(count);
}
}
------------------------------------------------<程式碼結束>--------------------------------------
主程式碼
------------------------------------------------<程式碼開始>--------------------------------------
import java.util.*;

public class Homework03302009Counter {
    public static void main (String args[]) {
        Counter counter = new Counter();
counter.reset();
counter.inc();
counter.inc();
counter.dec();
counter.output();
    }
}
------------------------------------------------<程式碼結束>--------------------------------------
附註: