2011-09-24 02:50:09菇
物件導向程式設計 - R1 (更新inG)
【0923】 人物左右撞牆
《Girl》
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)/**
* Write a description of class baby here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class baby extends Actor
{
/**
* Act - do whatever the baby wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
private int step = 2;
private int count=0;
private label mylabel;
public baby(label l)
{
mylabel = l;
}
public void act()
{
// Add your action code here.
move();
if(yoo()) bounce();
}
public void move(){
setLocation(getX()+step, getY());
}
public void bounce(){
step=-step;
count++;
mylabel.setText("Score:" + count);
}
public boolean yoo(){
return (getX()==0) || (getX()==getWorld().getWidth() -1) ;
}
}
《Label》
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)/**
* Write a description of class label here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class label extends Actor
{
/**
* Act - do whatever the label wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
}
public label (){
GreenfootImage img = new GreenfootImage(100, 20);
img.drawString("Yooooooooooo", 20, 20);
setImage(img);
}
public label (String text){
GreenfootImage img = new GreenfootImage(text.length()*20, 20);
img.drawString(text, 20, 20);
setImage(img);
}
public void setText(String text)
{
GreenfootImage img = getImage();
img.clear();
img.drawString(text, 20,20);
}
}
《World》
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Worldmy here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Worldmy extends World
{
/**
* Constructor for objects of class Worldmy.
*
*/
public Worldmy()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(120, 80, 5);
label mylabel=new label("Count:0");
addObject(mylabel, 100,2);
baby mybaby=new baby(mylabel);
addObject(mybaby, 20,50);
}
}
【0930】 隨堂考:點擊青蛙亂跳&隨機無限增值青蛙
《frog》
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)/**
* Write a description of class frog here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class frog extends Actor
{
/**
* Act - do whatever the frog wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
if (Greenfoot.mouseClicked(this))
{
int x = Greenfoot.getRandomNumber(getWorld().getWidth()+1);
int y = Greenfoot.getRandomNumber(getWorld().getHeight()+1);
this.setLocation(x,y); //青蛙亂跳
World myworld = getWorld();
frog f = new frog();
myworld.addObject(f,x,y); //亂數增加一個青蛙到世界中
}
}
}
《myWorld》
『作業』女孩亂亂走
《Girl》
《Girl》
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class baby here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class baby extends Actor
{
/**
* Act - do whatever the car wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
private int movement=5;
private label mylabel;
private int count=0;
public baby(label l)
{
mylabel=l;
//
}
public void act()
{
move();
if (atEdge()) bounce();
}
public void move()
{
move(5);//
if(Greenfoot.getRandomNumber(100)<10)
{
turn(Greenfoot.getRandomNumber(90)-45);
}
if(getX()<=10||getX()>=getWorld().getWidth()-10)
{
turn(280);
}
if(getY()<=5||getY()>=getWorld().getHeight()-5)
{
turn(180);
}
}
public void bounce()
{
movement=-movement;
count++;
mylabel.setText("Fight:"+count);
}
public boolean atEdge()
{
return (getX()==0)||(getY()==getWorld().getWidth()-1);//
}
}
《World》import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class myWorld here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class brick extends World
{
/**
* Constructor for objects of class brick.
*
*/
public brick()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(600, 400, 1);
label mylabel=new label("Count:0");
addObject(mylabel, 100,2);//
baby mybaby=new baby(mylabel);
addObject(mybaby, 20,50);
}
}
《label》import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class label here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class label extends Actor
{
/**
* Act - do whatever the label wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public label()
{
GreenfootImage img=new GreenfootImage(110,90);
img.drawString("GOGO",30,30);
setImage(img);
}
public label(String text)
{
GreenfootImage img=new GreenfootImage(text.length()*20,20);
img.drawString(text,20,20);
setImage(img);
}
public void setText(String text)
{
GreenfootImage img = getImage();
img.clear();
img.drawString(text, 20,20);
}
}
【1007】射擊遊戲
《Rock》
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Rock here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Rock extends Actor
{
/**
* Act - do whatever the Rock wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
int x = this.getX()+10;
this.setLocation(x,this.getY());
if (getX() == this.getWorld().getWidth() -1)
{
getWorld().removeObject(this);
}
}
}
《Space》
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Space here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Space extends World
{
/**
* Constructor for objects of class Space.
*
*/
public Space()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(600, 400, 1);
Rocket r = new Rocket();
this.addObject(r,300,400);
}
public void act()
{
if(Greenfoot.getRandomNumber(100)<3)
this.addObject(new Rock(),0,20);
}
}
《Beeper》
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* A bullet to be shot at asteroids.
*
* The Shot moves to the top of the screen and then expires. If it hits an asteroid
* on the way, it destroys the asteroid, and then expires immediately.
*/
public class Beeper extends Actor
{
private Rocket myShip;
/**
* Constructor for a Shot. You must specify the ship the shot comes from.
*/
public Beeper(Rocket a)
{
this.myShip = a;
}
/**
* Act - do whatever the Shot wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
int ypos = getY();
if (ypos > 0) {
ypos = ypos - 5;
setLocation(getX(), ypos);
Actor Rock = getOneIntersectingObject(Rock.class);
if (Rock != null) {
// We've hit an asteroid!
hitAnAsteroid();
this.getWorld().removeObject(Rock);
this.getWorld().removeObject(this);
}
}
else {
// I reached the top of the screen
this.getWorld().removeObject(this);
}
}
/**
* This method gets called (from the act method, above) when the shot hits an
* asteroid. It needs to do only one thing: increase the score counter.
* (Everything else, such as removing the asteroid which was hit, is dealt
* with in the act method).
*/
private void hitAnAsteroid()
{
// What goes here????
// We want to call the "bumpCount" method from the Counter class -
//Counter c=((Space)this.getWorld()).getCounter();
//c.bumpCount(20);
// but how??!!
}
}
《Rocket》
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Rocket here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Rocket extends Actor
{
/**
* Act - do whatever the Rocket wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
if(Greenfoot.isKeyDown("space"))
{
this.getWorld().addObject(new Beeper(this),this.getX(),this.getY());
}
}
}
【1014】星星背景
《Space》
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.*;
/**
* Write a description of class Space here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Space extends World
{
/**
* Constructor for objects of class Space.
*
*/
public Space()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(600, 400, 1);
Rocket r = new Rocket();
this.addObject(r,300,400);
GreenfootImage bg = getBackground();
bg.setColor(Color.BLACK);
bg.fill();
createStars(2000);
}
public void act()
{
if(Greenfoot.getRandomNumber(100)<3)
this.addObject(new Rock(),0,20);
}
private void createStars(int numberOfStars)
{
GreenfootImage bg = getBackground();
for (int i = 0; i < numberOfStars; i++) {
bg.setColor(new Color(Greenfoot.getRandomNumber(255),Greenfoot.getRandomNumber(255),Greenfoot.getRandomNumber(255)));
int x = Greenfoot.getRandomNumber( getWidth() );
int y = Greenfoot.getRandomNumber( getHeight() );
bg.fillOval(x, y, 2, 2);
}
}
}
【1028】
《Hello》
public class Hello{
public static void main(String args[])
{
Student s=new Student();
System.out.println("學號為==>" + s.getStudentID() + "姓名為==>" + s.getStudentName());
Student s1=new Student("s001","望安安",18);
System.out.println("學號為==>" + s1.getStudentID() + "姓名為==>" + s1.getStudentName());
s1.setStudentID("s003");
System.out.println("學號為==>" + s1.getStudentID() + "姓名為==>" + s1.getStudentName() + "年齡:" + s1.getAge());
if (s1.isGrownUp())
System.out.println("大白目");
else
System.out.println("小屁孩");
}
}
class myFirst{
}
《Student》
public class Student{
String stu_id;
String stu_name;
int age;
public Student(){}
public Student(String i, String n, int a){
stu_id=i;
stu_name=n;
age = a;
}
public String getStudentID(){
return stu_id;
}
public void setStudentID(String id){
stu_id=id;
}
public String getStudentName() {
return stu_name;
}
public void setStudentName(String n) {
stu_name=n;
}
public int getAge(){
return age;
}
public void setAge(int a){
age = a;
}
public boolean isGrownUp(){
if(age >=18)
return true;
else
return false;
}
}
--
import java.io.*;
public class testTream {
public class testTream {
/**
* @param args
*/
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
BufferedReader buf;
String str;
/*buf=new BufferedReader(new InputStreamReader(System.in));
str1=buf.readLine();
while((str1=buf.readLine())!=null){
System.out.println("你剛剛輸入的:"+str1);*/
int num1 = Integer.parseInt(buf.readLine());
int num2 = Integer.parseInt(buf.readLine());
int sum = 0;
for (num1 ; num1 <=num2 ; num1++){
sum= num1+sum;
str = str + sum + ",";
}
System.out.println("您輸入:"+str);
System.out.println("總和為:"+sum);
}
* @param args
*/
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
BufferedReader buf;
String str;
/*buf=new BufferedReader(new InputStreamReader(System.in));
str1=buf.readLine();
while((str1=buf.readLine())!=null){
System.out.println("你剛剛輸入的:"+str1);*/
int num1 = Integer.parseInt(buf.readLine());
int num2 = Integer.parseInt(buf.readLine());
int sum = 0;
for (num1 ; num1 <=num2 ; num1++){
sum= num1+sum;
str = str + sum + ",";
}
System.out.println("您輸入:"+str);
System.out.println("總和為:"+sum);
}
}
--
--
public class testOperator {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
boolean x = false;
int y = 10;
if (x = (y < 12)){
System.out.println("進入if");
}
}
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
boolean x = false;
int y = 10;
if (x = (y < 12)){
System.out.println("進入if");
}
}
}
--
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.*;
public class qq {
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.*;
public class qq {
/**
* @param args
*/
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
String id = "s001";
String pwd = "123";
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("請輸入帳號:");
String your_id = buf.readLine();
System.out.println("你剛剛輸入的帳號:" + your_id);
System.out.println("請輸入密碼:");
String your_pwd = buf.readLine();
System.out.println("你剛剛輸入的密碼:" + your_pwd);
if (id.compareTo(your_id) == 0 && pwd.compareTo(your_pwd) == 0){
System.out.println("登入成功");
}
else{
System.out.println("帳號密碼錯誤");
}
}
}
--
* @param args
*/
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
String id = "s001";
String pwd = "123";
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("請輸入帳號:");
String your_id = buf.readLine();
System.out.println("你剛剛輸入的帳號:" + your_id);
System.out.println("請輸入密碼:");
String your_pwd = buf.readLine();
System.out.println("你剛剛輸入的密碼:" + your_pwd);
if (id.compareTo(your_id) == 0 && pwd.compareTo(your_pwd) == 0){
System.out.println("登入成功");
}
else{
System.out.println("帳號密碼錯誤");
}
}
}
--
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.*;
public class hh {
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.*;
public class hh {
/**
* @param args
*/
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
System.out.println("請輸入分數:");
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
String sc = buf.readLine();
int num = Integer.parseInt(sc);
System.out.println("你剛剛輸入的分數:" + num);
if (num < 60)
{
System.out.println("不及格");
}
else if (num >= 60 && num < 70)
{
System.out.println("丙");
}
else if (num >= 70 && num < 80)
{
System.out.println("乙");
}
else if (num >= 80 && num < 90)
{
System.out.println("甲");
}
else
{
System.out.println("優");
}
}
}
* @param args
*/
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
System.out.println("請輸入分數:");
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
String sc = buf.readLine();
int num = Integer.parseInt(sc);
System.out.println("你剛剛輸入的分數:" + num);
if (num < 60)
{
System.out.println("不及格");
}
else if (num >= 60 && num < 70)
{
System.out.println("丙");
}
else if (num >= 70 && num < 80)
{
System.out.println("乙");
}
else if (num >= 80 && num < 90)
{
System.out.println("甲");
}
else
{
System.out.println("優");
}
}
}
--