2010年2月22日 星期一

98-1 計概 作業二 複習

#include
#include
void main(void){
double i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12;
i1 = 3.5 / 1.2;
i2 = 3.6 / 1.2;
i3 = 3.7 / 1.2;
i3 = 3.5 / 1.2;
i4 = 3.6 / 1.2;
i5 = 3.7 / 1.2;
i6 = 3.6 + 2;
i7 = 3.6 + 2;
i8 = 3.6 + 2.0;
i9 = (1 + 2 * 3 / 4 % 5 + 2);
i10 = (1 + 2 * 3 / 4 % 5 + 2);
//i11 = (1.0 + 2.0 * 3.0 / (4.0 % 5.0) + 2.0);
i12 = (1.0 + 2.0 * 3.0 / (4 % 5) + 2.0);
printf("i1=%f\n",i1);
printf("i2=%f\n",i2);
printf("i3=%f\n",i3);
printf("i4=%f\n",i4);
printf("i5=%f\n",i5);
printf("i6=%f\n",i6);
printf("i7=%f\n",i7);
printf("i8=%f\n",i8);
printf("i9=%f\n",i9);
printf("i10=%f\n",i10);
//printf("i11=%f",i11);
printf("i12=%f\n",i12);
_getch();
}

98-1 計概 作業一 複習

#include
#include

void main(void)
{
int a,b,c,d;
printf("Enter the first number: ");
scanf("%d",&a); //%d是整數,%f是允許小數點 &a存取
printf("Enter the second number: ");
scanf("%d",&b);
printf("Enter the third number: ");
scanf("%d",&c);
d=a+b+c;
printf("Total=%d",d); //=後面出現的是d //%d代表的是輸入整數
_getch(); //停留於這個畫面
}

2009年6月23日 星期二

我有話要說

我在大一時對於c++並沒有學的很好,而大二時修資料結構更是修的十分痛苦,因為概念懂,但是程式碼就是背不起來,也看不懂(就算期中得了滿分還是死在期末考)。但是這學期修了java讓我這個程式苦手能夠稍微寫一些程式,或是看懂別人程式在做什麼,他的想法如何對我而言已經是十分大的進步了(有如阿公那輩的人學會用電腦那種感慨),而讓我覺得java最不一樣的地方在於他很直接,我缺什麼就補什麼,如a+bi我只要把相加的式子寫出來就完成一半了,我也可以偷懶不想一直打a+"+"+b+"i"而建構一個String去幫我偷懶。
除了看課本和聽老師講解之外,我室友也幫了我不少忙,像是之前要小考以及小考0分時,他很有耐性的教導我解決我的疑惑或是我不懂的地方,我認為,進部的相反並不是退步,而是什麼都不去做,我沒有因為不懂就放他去,也遇到願意幫我課後教學的人,我認為這是我除了學會java的初步課程之外最大的收穫。

2009年6月19日 星期五

http://sofree.cc/intro-wordpress-2-8/
http://cjhtech.blogspot.com/2007/02/blogger.html

2009年6月15日 星期一

Lab Hanoi Tower

The pseudocode for Hanoi Tower is as follows:

solve(N, Src, Aux, Dst)
if N is 0 return
solve(N-1, Src, Dst, Aux)
Move N from Src to Dst
solve(N-1, Aux, Src, Dst)


Write the Java program based on the pseudocode in the above

.


Lab Recursive method

Write a recursive method to compute Fibonacci series.

Hint:

1.
fib(n)=fib(n-1)+fib(n-2)

2.
public static long fib(int n)




fib(1)


fib(5)


fib(10)


Lab Factorial

Write a Java program that computes N! where N is a positive integer.

Hint:

public static long factorial(int n)




0!的情形


1!的情形


10!的情形

2009年6月8日 星期一

Homework 6-1-2009 Modular Sorting

Write a sort method which takes a double array as parameter
and returns the sorted array. Call this method in a main program.

Hint: The lab is a rewriting of Lab Sorting
to make the sorting procedure a reusable method.



-----.





----
package javaapplication8;

/**
*
* @author KFN
*/
public class Sort
{

/** Creates a new instance of Sort */
public Sort()
{
}

public static void sort( double[] score ) {
int index = 0;
double temp = 0.0;
for( index = score.length-1; index >-1 ; --index)
{
for (int j = 0; j < index; j++)
{
if(score[index] > score[j])
{
temp = score[j];
score[j] = score[index];
score[index] = temp;
}
}
}


}

}

---
package javaapplication8;

/**
*
* @author KFN
*
*/
import java.util.Scanner;
public class ArrayOfScores
{

/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
double[] score = new double[5];
int index;
double max, temp;

System.out.println("Enter 5 Scores");
score[0] = keyboard.nextDouble();
max = score[0];
for(index = 1; index < score.length; ++index)
score[index] = keyboard.nextDouble();

Sort.sort( score );
/*
Sort a = new Sort();
a.sort( score );
*/
System.out.println("The scores are" );
for(index = 0; index< score.length; index++)
System.out.println(score[index]);

2009年6月1日 星期一

"Lab Array"

Study Display 6.1, and then write a program that can sort numbers in ascending order.


-----
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
double[] score = new double[5];
int index;
double max, temp;
System.out.println("Enter 5 Scores");
score[0] = keyboard.nextDouble();
max = score[0];
for(index = 1; index <>
score[index] = keyboard.nextDouble();
for( index = score.length-1; index >-1 ; --index){
for (int j = 0; j <>
if(score[index] > score[j]){
temp = score[j];
score[j] = score[index];
score[index] = temp;
}
}
}
System.out.println("The scores are" );
for(index = 0; index<>
System.out.println(score[index]);
}

Lab: Static Method II

Define a Complex class with a static method for computing complex addition. Use (2+3i)*(4+5i) in your test.



public class ADT {
private int real, image ;
public ADT(){
}
public ADT(int real , int image) {
this.real = real;
this.image = image;
}
public ADT mult(ADT ADT2){
ADT ans = new ADT(0,0) ;
ans.real = this.real * ADT2.real + this.image * ADT2.image;
ans.image = this.image * ADT2.real + this.real * ADT2.image;
return ans;
}
public String toString(){
return real + "+" + image + "i";
}
}

-----

public class StaticMethodII {
public static void main(String[] args) {
ADT f1, f2;
f1 = new ADT(2,3);
f2 = new ADT(4,5);
System.out.println("f1 = " + f1.toString());
System.out.println("f2 = " + f2.toString());
System.out.println("f1 x f2 = " + f1.mult(f2).toString());
}



2009年5月25日 星期一

Lab Magic Parking Tower

A parking tower is out of order someday. If you park a Benz, you will end up with a Torben. Write a program to simulate this scenario. First create a class called CarParked which has a static method called outOfOrder. Name an object called yourCar, which happens to be a Benz. Your program should contain a class called CarParked and a test program called CarParkedDemo which test the method by CarParked.outOfOrder(yourCar).

Hint: You may study Display 5.14 to get some ideas.



-----


public class CarParked {
private String car;  //p275 3 name改car
public CarParked( String car )
{
this.car = car;
}
public CarParked()
{
car = " No car yet. ";
}
void setCar(String car)
{
this.car = car;
}
public static CarParked outOfOrder( CarParked car )
{
car.setCar( "Torben" ) ;
return car ;
}
public String toString()
{
return car;
}

}

----

public class CarParkedDemo {
public static void main (String args[]) {
CarParked car = new CarParked( "Benz" ) ;
 
System.out.println("進入停車場的車為" + car);
 
CarParked.outOfOrder( car );
 
System.out.println("離開時的車為" + car);
}

}

小考成績

全錯

連我覺得應該有分的也沒拿到

也不是沒看也不是沒念

但仍然會有點沮喪

仿佛在浪費時間似的

就像微積分

題庫全做完了

那又如何

還不是被當了

說實話還蠻DOWN的

2009年5月11日 星期一

Lab Static Method

Define a Complex class with a static method for computing complex addition. Use (2+3i)+(4+5i) in your test.




-------------
public class Complex {
int real;
int image;
public Complex()
{}
public Complex( int real , int image)
{
this.real = real;
this.image = image;
}
public static Complex add(Complex a, Complex b) {
Complex ans = new Complex();
ans.real = a.real + b.real;
ans.image = a.image + b.image;
return ans;
}
public String toString() {
return real + "+" + image + "i";
}
}

---------------
public class LabStaticMethod {
        public static void main (String args[]) {
 
 Complex f1, f2;
 f1 = new Complex(2, 3);
 f2 = new Complex(4, 5);
 
 System.out.println("f1 = " + f1.toString());
 System.out.println("f2 = " + f2.toString());
 System.out.println("f1 + f2 = " + Complex.add(f1, f2).toString());
   }
}

Lab Math methods

Compute the following mathematical functions.

Math.round(3.2)
Math.round(3.6)
Math.floor(3.2)
Math.floor(3.6)
Math.ceil(3.2)
Math.ceil(3.6)



----
public class LabMath {

/**
* @param args
*/
public static void main(String args[]) {
System.out.println("Math.round(3.2) = " + Math.round(3.2) );
System.out.println("Math.round(3.6) = " + Math.round(3.6) );
System.out.println("Math.floor(3.2) = " + Math.floor(3.2) );
System.out.println("Math.floor(3.6) = " + Math.floor(3.6) );
System.out.println("Math.ceil(3.2) = " + Math.ceil(3.2) );
System.out.println("Math.ceil(3.2) = " + Math.ceil(3.6) );
// TODO 自動產生方法 Stub

}

}

Lab Finding the max of three numbers

Write a static method that computes the maximum of three float numbers.

--------

import java.util.Scanner;
public class Maximum {
public static void main (String args[]) {
Scanner scan = new Scanner(System.in);
System.out.println("請輸入三個數字比大小");
float a = scan.nextFloat();
float b = scan.nextFloat();
float c = scan.nextFloat();
System.out.println("最大者為"+Maximum.Max(a, b, c));
}
public static float Max( float a,float b,float c )  
{
if( a > b && a > c)
return a;
else if( b > c && b >a )
return b;
else
return c;
}

}

Lab Finding the max of three numbers

Write a static method that computes the maximum of three float numbers.





HOMEWORK2

/*
* Tempurate.java
*
* Created on 2009年5月11日, 下午 2:41
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package homework;

/**
*
* @author KFN
*/
public class Temperature {
float degree ;
String scale ;
/** Creates a new instance of Tempurate */
public Temperature() {
degree = 0 ;
scale = "C";
}
public Temperature( float degree ) {
this.degree = degree;
scale = "C";
}
public Temperature( String scale ) {
degree = 0 ;
this.scale = scale ;
}
public Temperature( float degree, String scale ) {
this.degree = degree;
this.scale = scale ;
}
public float Change( String type ){
if ( this.scale.equals( type ) )
return degree; // 兩個種類相同的話直接傳入
else {
if ( type.equals( "F" ) )
return ( ( degree / 5 ) * 9 ) + 32 ;
else if( type.equals( "C" ) )
return 5 * ( degree - 32 ) / 9 ;
else
return 0;
}
}
public String toString(){
return this.degree + " degree " + this.scale ;
}
public boolean Equals( Temperature other ) {
if ( this.scale.equals( other.scale ) ) {
if ( this.degree == other.degree )
return true;
else
return false;
} else {
if ( this.degree == other.Change(this.scale) )
return true;
else
return false;
}
}
public boolean Greater( Temperature other ) {
if ( this.scale.equals( other.scale ) ) {
if ( this.degree > other.degree )
return true;
else
return false;
} else {
if ( this.degree > other.Change( this.scale ) )
return true;
else
return false;
}
}
public boolean Less( Temperature other ) {
if ( this.scale.equals( other.scale ) ) {
if ( this.degree <>
return true;
else
return false;
} else {
if ( this.degree <>
return true;
else
return false;
}
}
}


------------------------
/*
* Main.java
*
* Created on 2009年5月11日, 上午 11:40
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package homework;

/**
*
* @author KFN
*/
import java.util.Scanner;
public class Main {
/** Creates a new instance of Main */
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
/*
Scanner scan =new Scanner(System.in);
System.out.println("請輸入溫度");
float n = scan.nextFloat();
System.out.println("請選擇攝氏'C'或是華氏'F'");
String scale = scan.next();
if(scale.equals("C"))
{
float changeF = ((n/5)*9)+32;
System.out.println("您輸入攝氏"+n+"度");
System.out.println("轉換為華氏為"+changeF+"度");
}
else if(scale.equals("F"))
{
float changeC = 5*(n-32)/9 ;
System.out.println("您輸入華氏"+n+"度");
System.out.println("轉換為攝氏為"+changeC+"度");
}
else
{
System.out.println("請選擇攝氏'C'或是華氏'F'");
}
*/
Temperature t1 = new Temperature( 100, "C");
Temperature t2 = new Temperature( 212, "F" );
Temperature t3 = new Temperature( "C" );
Temperature t4 = new Temperature( "F" );
// question 1.
System.out.println("t1 " + t1.toString() );
System.out.println("t2 " + t2.toString() );
System.out.println("t3 " + t3.toString() );
System.out.println("t4 " + t4.toString() );
System.out.println("");
//question 2.
if ( t1.Equals( t2 ) )
System.out.println("t1 is equals to t2 " );
else
System.out.println("t1 is not equals t2 " );
if ( t2.Greater( t4 ) )
System.out.println("t2 is greater than t4 " );
else
System.out.println("t2 is not greater than t4 " );
if ( t2.Less( t4 ) )
System.out.println("t2 is Less than t4 " );
else
System.out.println("t2 is not Less than t4 " );
// TODO code application logic here
}
}

Homework 5-4-2009

Do Project 7 of Chapter 4.

1.
2.
3.4.




---



2009年5月4日 星期一

Lab Method Overloading

依據Class definition 3,修改程式使其接受三種setDate

date1.setDate(1,2,2008);
date2.setDate("February",2, 2008);
date3.setDate(2008);



Lab Java Constructor

Write constructors in the lab Fraction Addition.





------程式碼------


public class FractionAddtion {

public static void main(String[] args)
{
Fraction f1 = new Fraction(1 ,2);
Fraction f2 = new Fraction(1 ,3);

System.out.println("f1 = " + f1.outputstring() + "\nf2 = " + f2.outputstring());
System.out.println("f1 + f2 = "+ f1.add(f2).outputstring());

}

}

---------------------------

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

public void number( int numerator, int denominator)
{
this.numerator = numerator;
this.denominator = denominator;
}
public String outputstring() {

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

public Fraction(int x, int y) {
this.numerator = x;
this.denominator = y;
}

public Fraction add(Fraction Fraction2)
{
Fraction countOfTemp = new Fraction(numerator ,denominator);
countOfTemp.numerator = numerator*Fraction2.denominator + denominator*Fraction2.numerator;
countOfTemp.denominator = denominator*Fraction2.denominator;
//交叉相乘
return countOfTemp;
}





}