2010年4月30日 星期五

99/4/30 Java-型態轉換加總











import java.io.*;
import java.lang.Byte;
class Javaftirty
{
public static void main(String[] args)
{
int a,b,total;
System.out.println("Ming");
File mingfile = new File("mf.txt");
//Byte b = new Byte("1");
a=Integer.parseInt(args[0]);
b=Integer.parseInt(args[1]);
total=a+b;

try
{

FileOutputStream lym = new FileOutputStream(mingfile);
mingfile.createNewFile();
PrintStream lop = new PrintStream(lym);
lop.print(a +"+" + b + "=" + total);
//lym.write(b);
/*BufferedOutputStream yml = new BufferedOutputStream(lym);
PrintStream ps = new PrintStream(yml);
ps.println("12345");
ps.close();
lym.close();
yml.close();*/
}
catch (Exception e)
{
System.out.println("Ming");
}
}
}


資料來源
http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html
http://www.tcrc.edu.tw/question/30.pdf
http://java.sun.com/j2se/1.4.2/docs/api/java/io/FileOutputStream.html
http://java.sun.com/j2se/1.4.2/docs/api/java/io/BufferedOutputStream.html
http://java.sun.com/j2se/1.4.2/docs/api/java/io/PrintStream.html
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Byte.html

2010年4月16日 星期五

99/04/16 Java 期中考



















import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TriangleMethod extends JFrame
implements ActionListener {

private JButton button;
private JPanel panel;

public static int b1,b2;
public static void main(String[] args) {

int a1,a2;

a1 = Integer.parseInt(args[0]);
a2 = Integer.parseInt(args[1]);
b1 = a1;
b2 = a2;

TriangleMethod frame = new TriangleMethod();
frame.setSize(350, 400);
frame.createGUI();

frame.setVisible(true);

}



private void createGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new FlowLayout() );

panel = new JPanel();
panel.setPreferredSize(new Dimension(300, 300));
panel.setBackground(Color.white);
window.add(panel);

button = new JButton("Press me");
window.add(button);
button.addActionListener(this);
}

public void actionPerformed(ActionEvent event) {
Graphics paper = panel.getGraphics();
drawLogo(paper, 10, 20);
//drawLogo(paper, 100, 100);
//drawTriangle(paper, 100, 10, 40, 40);
drawTriangle(paper, 10, 100, 20, 60);
}

private void drawLogo(Graphics drawingArea,
int xPos, int yPos) {
drawingArea.drawRect(xPos, yPos, b1, b2);//drawingArea.drawRect(xPos, yPos, 40, 40);
//drawingArea.drawRect(xPos, yPos, 20, 20);
}

private void drawTriangle(Graphics drawingArea,
int xPlace,
int yPlace,
int width,
int height) {
drawingArea.drawLine(xPlace, yPlace,
xPlace, yPlace + b1);
drawingArea.drawLine(xPlace, yPlace + b1,
xPlace + b2, yPlace + b1);
drawingArea.drawLine(xPlace, yPlace,
xPlace + b2, yPlace + b1);
}
}



注意事項
1.首先必須先把值改程可以用輸入的。
2.最直接的方法就是放外層共用。
3.並不是直接將值傳入,改程讓程式自動取值。

2010年4月8日 星期四

Java 矩形與三角形面積99/04/09









class Javafn
{
public static void main(String[] args)
{
int a,z,u;
String a1,a2;
a1=args[0];
a2=args[1];

Triangle ming1 = new Triangle();
u = ming1.Triangle(a1,a2);
System.out.println(u);

Rectangle ming2 = new Rectangle();
z = ming2.Rectangle (a1,a2);
System.out.println(z);

}
}

class Triangle
{
Triangle()
{
System.out.println("Triangle Area");
}

static int Triangle(String a1,String a2)
{
int a,b,c;
a=Integer.parseInt(a1);
b=Integer.parseInt(a2);
c=a*b/2;
return c;
}

}

class Rectangle
{
Rectangle()
{
System.out.println("Rectangle Area");
}
static int Rectangle(String a1,String a2)
{
int a,b,c;
a=Integer.parseInt(a1);
b=Integer.parseInt(a2);
c=a*b;
return c;
}

}

2010年4月1日 星期四

Java 99/04/02





class Javaft
{
public static void main(String[] args)
{
int a,z,u,b1,b2;
String a1,a2;
b1 = 3;
b2 = 4;
a1=args[0];
a2=args[1];
u=sum(b1,b2);
z = add (a1,a2);
System.out.println(u);
}
static int add(String a1,String a2)
{
int x,y,z;
x = Integer.parseInt(a1);
y = Integer.parseInt(a2);
z = x + y;
System.out.println(z);
return z;
}
static int sum(int b1,int b2)
{
int c;
c = b1 + b2;
return c;
}
}

注意事項:
1.不要重複宣告。
2.需要運算的變數一定要給值。
3.編譯後有讀值的程式一定要給值。
4.變數資料傳送注意型態的問題。