2009年4月13日 星期一

LAB


public class Fraction {
private int numerator;
private int denominator;

public void number( int numerator, int denominator)
{
this.numerator = numerator; //把這method的區域變數numerator給予這個類別內宣告的numerator
this.denominator = denominator; //this:代表使用中的類別,也可以使用在變數名稱有重複或易於混淆時
}
public String outputstring() {

return (numerator + "/" + denominator);  //分數的表示形式
}

public Fraction add(Fraction Fraction2) //add中有2個Fraction相加,Fraction Fraction2
{                                       //為Fraction.add(Fraction)表示方式並為區隔而寫為Fraction2
Fraction countOfTemp = new Fraction();
countOfTemp.numerator = numerator*Fraction2.denominator + denominator*Fraction2.numerator;
countOfTemp.denominator = denominator*Fraction2.denominator;
//交叉相乘
return countOfTemp; 
}


public static void main(String[] args) 
{
Fraction f1 = new Fraction();
Fraction f2 = new Fraction();
f1.number(1, 2);
f2.number(1, 3);
System.out.println("f1 = " + f1.outputstring() + "\nf2 = " + f2.outputstring());
System.out.println("f1 + f2 = "+ f1.add(f2).outputstring());
 
}


}


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


2009年4月12日 星期日

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


HOMEWORK

/*
* Counter.java
*
* Created on 2009年4月12日, 下午 11:01
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package javaapplication16;

/**
*
* @author KFN
*/
public class Counter {
private int counter;

void reset()
{
counter = 0;
}

void inc()
{
++counter;
}

void dec()
{
--counter;
}

int getcounter()
{
return counter;
}

void output()
{
System.out.println(counter);
}
/** Creates a new instance of Counter */
public Counter() {
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Counter counter = new Counter();

counter.reset();
counter.inc();
counter.inc();
counter.inc(); //作為測試多做了2個加
counter.inc();
counter.dec();
counter.output();
// TODO code application logic here
}

}

2009年3月30日 星期一

MEMO

import java.util.Scanner;

public class Ch4lab2 {
private String month;
private int day;
private int year;
public void readInput()
{
System.out.println(month + " " + day + ", " + year);
}
public void setDate(int newMonth, int newDay, int newYear)
{
month = monthString(newMonth);
day = newDay;
year = newYear;
}
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");
   System.exit(0);
   return "Error";
}
}

}
-----

public class DateThirdTryDemo {
public static void main(String[] args)
{
Ch4lab2 date = new Ch4lab2();
int year = 1882;
date.setDate(6, 17, year);
date.readInput();
System.out.println(date.newDay());
System.out.println(date.newMonth());
System.out.println(date.newYear());
}

}


MEMO

public class DateFirstTry {
public String month;
public int day;
public int year;
public void makeItNewYears()
{
month = "January";
day = 1;
}
public void writeOutput()
{
System.out.println(month + " " + day + ", " + year);
}

}
------------


public class ch4 {
public static void main (String[] args)
{
DateFirstTry date1, date2;
date1 = new DateFirstTry();
date2 = new DateFirstTry();
date1.month = "January";
date1.day = 1;
date1.year = 2007;
System.out.println("date1:");
date1.writeOutput();
date2.month = "July";
date2.day = 4;
date2.year = 1776;
System.out.println("date2:");
date2.writeOutput();
System.out.println("Make It In New Years:");
date2.makeItNewYears();
date2.writeOutput();
}

}

lab class definition

Study Display 4.1 and then do Self-Test Exercise 1.