- 73
- 0
- 约4.67千字
- 约 6页
- 2020-08-01 发布于上海
- 举报
《Java程序设计》作业三
教师:李家琦
学生:2013级本科、电子13001-13004班
13.7
设计一个名为Colorable的接口,其中有名为howToColor()的void方法。可着色对象的每个类必须实现Colorable接口。设计一个扩展GeometricObject类并实现Colorable接口的名为Square的类。实现howToColor方法,显示消息“Colorall four sides”。?画出包括Colorable、Square和GeometricObject的UML图。编写一个测试程序,创建有五个GeometricObject对象的数组。对于数组中的每个对象而言,如果对象是可着色的,那就调用howToColor方法。
Colorable接口
public interface Colorable {
void howToColor();
}
GeometricObject类
public class GeometricObject{
}
Square类
public class Square extends GeometricObject implements Colorable{
public void howToColor() {
System.out.println(Colorall four sides);
}
}
场景类Main
public class Main {
public static void main(String args[]){
GeometricObject[] gs = {new GeometricObject(),new GeometricObject(),new Square(),new GeometricObject(),new GeometricObject(),};
for (GeometricObject geometricObject : gs) {
if(geometricObject instanceof Square){
Square s =?(Square)geometricObject;
s.howToColor();
}
}
}
}
14.6
请写一个程序显示一个象棋棋盘,其中每个黑白单元格都是一个填充了黑色或者白色的Rectangle
import java.awt.Color;
import java.awt.Point;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Ch1_6
{
public static void main(String[] args)
{
JFrame f=new JFrame("国际象棋棋盘"); //创建窗口
//窗口设置大小
f.setSize(168,195); //边框的长和宽
//窗口设置位置
Point point=new Point(350,200);
f.setLocation(point);
int grids=8; //行数和列数
int gridsize=20; //单元格的高和宽
for(int i=0; igrids; i++) //外循环控制行
{
for(int j=0; jgrids; j++) //内循环控制列
{
JLabel l = new JLabel(); //生成标签实例
l.setSize(gridsize,gridsize); //设置标签大小
l.setLocation(i*gridsize,j*gridsize); //设置标签位置
原创力文档

文档评论(0)