2024年5月10日发(作者:)
-------------
16
25
5
4
3
2
1
-------------
-------------
2、编写程序将用户输入的一个0~100之间的分数,将其转化成1~5的5分计方法。
90~100: 5
80~90: 4
70~80: 3
-------------
-------------
60~70: 2
其他: 1
import r;
public class ScoreClass {
public static void main(String[] args) {
Scanner s=new Scanner();//如何接收一个值
("input a int(0-100):");
int score=t(),n=-1;
if(score>100){ //如何来分等级
n("Error");
}else if(score>=90){
n=5;
}else if(score>=80){
-------------
-------------
n=4;
}else if(score>=70){
n=3;
}else if(score>=60){
n=2;
}else if(score>=0){
n=1;
}else{
n("Error");
}
if(n!=-1){
n("n="+n);
}
-------------
-------------
}
}
3、使用类,生成100个0~99之间的随机数(整数),找出它们之
中的最大者及最小者,并统计大于50的整数个数
public class MaxMin {
public static void main(String[] args) {
double p,temp,max=-1,min=1000;
int i,j=0;
for(i=0;i<100;i++){
p=()*100;
temp=(p);//0~1
if(temp>max)
max=temp;
if(temp ------------- ------------- min=temp; if(temp>=50) j++; } n("max="+max); n("min="+min); n("total="+j); } } 1、编程实现矩形类Rectangle,Rectangle拥有私有属性长(length)和宽(width), 为Rectangle类提供至少两种构造方法,同时提供计算矩形周长(perimeter())和面积 (area())的方法。使用两种构造方法,至少各创建一个矩形对象实例,并分别计算两个 矩形的周长和面积。 public class Rectangle { private double width; ------------- ------------- private double length; public Rectangle(){ =5; =6; } public Rectangle(double w,double l){ =w; =l; } public double area(){ //求面积 return *; } public double perimeter(){ //求周长 ------------- ------------- return 2*(+); } public static void main(String[] args) { Rectangle r1=new Rectangle(); n("r1的周长:"+ter()); n("r1的面积:"+()); n(); Rectangle r2=new Rectangle(10,20); n("r2的周长:"+ter()); n("r2的面积:"+()); } public double getWidth() { return width; ------------- ------------- } public void setWidth(double width) { = width; } public double getLength() { return length; } public void setLength(double length) { = length; } } 2、使用矩形类InterRectangle,编程统计若干块矩形土地的相关信息。由用户输入 每块土地数目以及每块土地的长与宽,统计出所有土的面积和周长并显示出来。 import nPane; ------------- ------------- public class InterRectangle { private double width; private double length; public InterRectangle(){ =5; =6; } public InterRectangle(double w,double l){ =w; =l; } public double area(){ //求面积 return *; ------------- ------------- } public double perimeter(){ //求周长 return 2*(+); } public static void main(String[] args) { String strNo=putDialog(null,"请输入土地块数","输入对话框 ",ATION_MESSAGE); double total=ouble(strNo); //得到要统计的土地块数 String strW=null,strL=null; InterRectangle ir=null; double w,l; double totalArea=0,totalPer=0; for(int i=1;i<=total;i++){ strW=putDialog(null,"请输入第"+i+"块土地的宽度(米)"," ------------- ------------- 输入对话框",ATION_MESSAGE); strL=putDialog(null,"请输入第"+i+"块土地的长度(米)"," 输入对话框",ATION_MESSAGE); w=ouble(strW); //得到宽度 l=ouble(strL); //得到长度 ir=new InterRectangle(w,l); //根据不同的长宽得到不同的矩形对象 totalArea=totalArea+(); //求总面积 totalPer=totalPer+ter(); //求总周长 } n("总共有"+total+"块土地n土地总面积为:"+totalArea+"平米n 土地总周长为:"+totalPer+"米"); } } 3、为第一步实验中定义的矩形类Rectangle派生一个子类:正方体类Cube。若正方 体类的操作同样是求底面周长和面积,同时要求求出长方体体积,则这个子类除了从父类 ------------- ------------- 那里继承来的方法之外,还需要做哪些修改。编程检查、运行所编写的正方体类。使用两 种构造方法,至少各创建一个正方体对象实例,并分别计算两个正方体的底面周长、底面 面积和体积。 public class Cube extends Rectangle{ public Cube(){ super(10,10); } public Cube(double l){ super(l,l); } public double area(){ //求面积 return (); } public double perimeter(){ //求周长 ------------- ------------- return ter(); } public double getCube(){ //求tioj return ()*th(); } public static void main(String[] args){ Cube c=new Cube(); n("c底面周长:"+ter()); n("c底面面积:"+()); n("c体积:"+e()); n("n"); Cube c1=new Cube(5); n("c1底面周长:"+ter()); ------------- ------------- n("c1底面面积:"+()); n("c1体积:"+e()); } } 1、Point类描述“点”类,拥有两个成员变量x和y(分别代表点的横坐标和纵坐标), 给Point两个自定义的构造方法。Circle类描述“圆”类,是Point类的一个子类,并拥 有一个成员变量radius(代表圆的半径),给Circle两个自定义的构造方法。分别覆盖每 个类中的toString()方法,并在构造方法中借助toString()方法显示Point或Circle各自的 基本描述信息。在CircleTest中,分别至少调用Circle的两个构造方法各一次,构造两个 不同的圆,并查看显示信息。 public class Point { private double x; private double y; public Point(){ this(0,0); } ------------- ------------- public Point(double x,double y){ this.x=x; this.y=y; n("Point constructor:"+ng()); //注意,本例中在主方法中,当调用此处时是子类对象,所以用的是子类的toString() 方法 } public String toString(){ return "["+this.x+","+this.y+"]"; } } public class Circle extends Point{ private double radius; public Circle(){ ------------- ------------- this(10,0,0); } public Circle(double radius,double x,double y){ super(x,y); =radius; n("Circle constructor:"+ng()); } public String toString(){ return "Center="+ng()+"radius="+; } } public class CircleTest { public static void main(String[] args) { ------------- ------------- Circle cir1,cir2; n("第一个圆的信息:"); cir1=new Circle(); n("第二个圆的信息:"); cir2=new Circle(5,10,10); } } 2、接口Alarm(报警)拥有抽象方法void alarm(),接口Guard(防盗)用有抽象 方法void guard()和防盗年限10年,抽象类Door拥有抽象方法void open()和void close(),AlarmGuardDoor类继承了类Guard和实现了接口Alarm,在类InterfaceDemo 中完成类AlarmGuardDoor的实例化,并分别调用AlarmGuardDoor类中的所有方法查 看输出效果。 public interface Alarm { void alarm(); } ------------- ------------- public interface Guard { void guard(); } public abstract class Door { abstract void open(); abstract void close(); } public class AlarmGuardDoor extends Door implements Guard,Alarm{ public void alarm() { n("刮风下雨收衣服了"); } public void guard() { n("防盗效果好"); ------------- ------------- } void open() { n("芝麻开门"); } void close() { n("芝麻关门"); } } public class InterfaceDemo { public static void main(String[] args) { AlarmGuardDoor agd=new AlarmGuardDoor(); (); (); ------------- ------------- (); (); } } 3、定义抽象类Fruit,拥有抽象方法public void show()和成员protected String brand,定义Apple,Banane和Pear三个类,分别继承了抽象类Fruit,实现了方法show() 以及输出了水果的品牌(brand),在类DynamicBind中定义Fruit的一个引用,分别赋 予三个子类对象,并查看每次show()方法的区别。 public abstract class Fruit { protected String brand; public abstract void show(); } public class Apple extends Fruit{ public Apple(){ this("黄金苹果"); ------------- ------------- } public Apple(String brand){ =brand; } public void show() { n("Apple:"+brand); } } public class Pear extends Fruit{ public Pear(){ this("水晶梨"); } public Pear(String brand){ ------------- ------------- =brand; } public void show() { n("Pear:"+brand); } } public class Banane extends Fruit{ public Banane(){ this("千层蕉"); } public Banane(String brand){ =brand; } ------------- ------------- public void show() { n("Banane:"+brand); } } public class DynamicBind { public static void main(String[] args) { Fruit f=new Apple("红富士苹果"); (); f=new Pear("香梨"); (); f=new Banane("仙人蕉"); (); } ------------- ------------- } 1、类SimpleException中有方法 public static double Division(double x,double y) { if(y==0) throw new IllegalArgumentException("分母不能为0"); //手动抛出异常,对于双精 度而言,除数为0可以得到无穷大的值,本不会报异常错误,这里手动强制报 return x/y; } 主方法中有代码: double a=ouble(args[0]); double b=ouble(args[1]); n(Division(a,b)); 借助异常机制获所有可能出现的异常,并提示相关异常的出错信息。最后,无论程序 如何结束,保证程序都能输出语句“游戏结束!!!” ------------- ------------- public class SimpleException { public static double Division(double x,double y) { if(y==0) throw new IllegalArgumentException("分母不能为"); //手动抛出异常 return x/y; } public static void main(String[] args) { try{ double a=ouble(args[0]); double b=ouble(args[1]); n(Division(a,b)); }catch(ArrayIndexOutOfBoundsException e){ n("arg[0]和args[1]需要参数"); ------------- ------------- } catch(NumberFormatException e){ n("数据格式必须为double类型"); } catch(IllegalArgumentException e){ n("非法数据: "+ng()); } catch(Exception e){ n(e+" end"); } finally{ n("程序结束!"); } ------------- ------------- } } 2、自定义日期类异常, 1)、定义一个程序DateExceptionTest,该程序主方法中: a)、采用Scanner类的对象来接收三个整数 b)、对于非整数数据,能采用异常进行验证 c)、用三个整数借助自定义类DateException构建出一个日期对象,并以 “yyyy-mm-dd”的形式输出该日期对象。 d)、使用类 2)同时要求,自定义类DateException继承自Exception a)、拥有两个构造方法,分别是DateException(String s)、public DateException(int year,int month,int day) throws DateException和一个成员方法 Date getDate()。 b)、第二个构造方法能接收三个整数,拥有验证三个数据是否合法的能力,验证时, 对于非法数据格式,采用抛出异常DateException,抛出异常时,携带错误提示信息。 ------------- ------------- c)、DateException类中,借助方法getDate()获得该组合的日期对象 import xception; import DateFormat; import ; import ismatchException; import r; public class DateException extends Exception{ private int year; private int month; private int day; public DateException(String s){ super(s); } ------------- ------------- public DateException(int year,int month,int day) throws DateException{ super("构造日期"); if(year<1900 || year>2100) throw new DateException("年份必须在到之间"); //验证年份是否合法 if(month<1 || month>31) throw new DateException("月份必须在到之间"); //验证月份是否合法 if(day<1 || day>31) throw new DateException("日期必须在到之间"); //验证日是否合法 //在满足以上条件下,进一步验证特殊情况 switch(month){ case 2: if((year%4==0&&year%100!=0)||year%400==0){ if(day==30){ ------------- ------------- throw new DateException("该月没有号"); //闰年月没有号 } }else { if(day==29)throw new DateException("该月没有号"); //非闰年月没有号 } break; case 4: case 6: case 9: case 11: if(day==31)throw new DateException("该月不能出现天"); break; } ------------- ------------- =year; =month; =day; } public Date getDate()throws ParseException{ String strDate=+"-"++"-"+; SimpleDateFormat sdf=new SimpleDateFormat("yyyy-mm-dd"); Date date = (strDate); //根据提供参数构造一个日期对象 return date; } } import xception; import DateFormat; ------------- ------------- import ; import ismatchException; import r; public class DateExceptionTest { public static void main(String[] args) { DateException de; Date newdate; Scanner s=new Scanner(); int year,month,day; try{ n("请输入年份(1900到之间的整数):"); year=t(); n("请输入月份(1到之间的整数):"); ------------- ------------- month=t(); n("请输入日期(1到之间的整数):"); day=t(); de=new DateException(year,month,day); newdate=e(); SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-mm-dd"); n((newdate)); //格式化输出 } catch(InputMismatchException e){ //处理Scanner对象接收到非整形数据处理 n(ng()+":请输入整数"); } catch(DateException e){ n(ng()); ------------- ------------- } catch(ParseException e){ n(ng()+":数据错误,无法转换成日期"); } catch(Exception e){ tackTrace(); } } } 1、编写代码实现登录界面。 1)、简单登录界面框类SimpleLogin,含有两个标签lblName、lblPassword,标签 信息分别对应的是“用户名:”和“密码”,一个文本域jtfName用来接收用户名,一个密 码域jtfpassword用来接收密码,两个按钮btnOk、btnCancel,按钮信息分别是“登录” 和“取消”。 2)、登录按钮中实现验证“用户名”和“密码”的信息,本题中用户名和密码固定分 ------------- ------------- 别为“aa”和“123”。取消按钮中实现退出登录框功能。 import Layout; import ner; import Event; import Listener; import .*; public class SimpleLogin extends JFrame implements ActionListener{ private JLabel lblName,lblPassword; private JButton btnOk,btnCancel; private JTextField jtfName; private JPasswordField jtfpassword; private JPanel jpbtn,jpMain; public SimpleLogin(){ ------------- ------------- super("登录"); //按钮部分界面 jpbtn=new JPanel(); btnOk=new JButton("确定"); btnCancel=new JButton("取消"); (btnOk); (btnCancel); //登录信息部分界面 jpMain=new JPanel(); lblName=new JLabel("用户名:"); lblPassword=new JLabel("密 码:"); jtfName=new JTextField(15); jtfpassword=new JPasswordField(15); ------------- ------------- (lblName); (jtfName); (lblPassword); (jtfpassword); ionListener(this); ionListener(this); Container con=tentPane(); (jpbtn,); (jpMain,); e(250,150); ation(500,300); izable(false); ible(true); ------------- ------------- aultCloseOperation(_ON_CLOSE); } public static void main(String[] args) { SimpleLogin sl=new SimpleLogin(); } public void actionPerformed(ActionEvent e) { if(rce()==btnOk){ if(t().equals("aa") && String(sword()).equals("123")){ e(); ssageDialog(null,"登录成功"); }else{ ssageDialog(null,"登录失败"); t(""); ------------- new ------------- t(""); } } if(rce()==btnCancel){ (0); } } } 2、编写实现程序,在窗口North上布置三个按钮,并完成按钮点击事件,点击三个 按钮时,分别输出填充颜色的三角形,矩形和椭圆。 1、分别定义三个面板类Triangle、Rectangles和Ellipse,分别实现绘图三角形、矩 形和椭圆形; 2、框架类PaintFigure定义两个面板buttons和jMainPanel,分别装载控制按钮和 显示图形; 3、面板buttons装载PaintFigure的“北部”,在中有三个按钮btnTriangle、 ------------- ------------- btnRectangle和btnEllipse,点击三个按钮分别能在是面板jMainPanel上绘制出三角形、 矩形和椭圆形; 4、面板jMainPanel装载在PaintFigure的“中部”,其布局为三行三列的网格布局, 当点击面板buttons中按钮时,将在面板jMainPanel追加显示图形,最多可以显示九个 图形。 import .*; import .*; import .*; public class PaintFigure extends JFrame implements ActionListener{ private JPanel jpMain,jpbtn; private JButton jbtnT; private JLabel jlbl; private int i=0; private final int MaxPanel=16; class MouseHandler extends MouseAdapter{ //鼠标进入输出三角形 ------------- ------------- public void mouseEntered(MouseEvent e) { if(i (new Triangle()); t(); te(); i++; } } } public void actionPerformed(ActionEvent e) { //点击按钮输出三角形 if(rce()==jbtnT){ if(i (new Triangle()); ------------- ------------- t(); te(); i++; } } } public PaintFigure(){ jpMain=new JPanel(); jpbtn=new JPanel(); jbtnT=new JButton("三角形"); jlbl=new JLabel("三角形"); out(new GridLayout(4,4)); (jbtnT); ------------- ------------- (jlbl); ionListener(this);//添加事件 seListener(new MouseHandler()); //添加事件 Container con=tentPane(); (jpMain,); (jpbtn,); ible(true); nds(200,200,500,400); aultCloseOperation(_ON_CLOSE); } public static void main(String[] args){ PaintFigure pf=new PaintFigure(); } ------------- ------------- } import ; import cs; import n; import ; public class Triangle extends JPanel{ private int height; private int width; public void paintComponent(Graphics g){ omponent(g); height=e().height; width=e().width; Polygon p=new Polygon(); //定义一个多边形,改多边形具体有几条边,由下面添 加的点 ------------- ------------- nt(0, height); //定义多边形的顶点坐标 nt(width/2, 0); nt(width, height); or(); //绘图的颜色 lygon(p); //依据定义好的多边形对象p,绘制多边形 } } import .*; import .*; import .*; class Temperature extends JFrame //主面板 { Controler demo; ------------- ------------- public Temperature() { super("从摄氏温度转换为华氏温度"); demo = new Controler(); Container contentPane = getContentPane(); (demo); // lkit().getScreenSize().width;//显示器屏幕宽度 // lkit().getScreenSize().height;//显示器屏幕高度 e(300,200); //设置窗口显示在屏幕中心 int w=(int)(lkit().getScreenSize().width/th()/2); 幕宽度-窗口宽度)/2得到窗口起点横坐标 int h=(int)(lkit().getScreenSize().height/ght()/2); ation(w,h); ------------- //(屏 ------------- ible(true); aultCloseOperation(_ON_CLOSE); } public static void main(String args[]) { new Temperature(); } } class Controler extends JPanel implements ActionListener { JTextField input; JLabel Centigrade; JLabel Fahrenheit; ------------- //添加所有控件 ------------- JButton convent; String s1; public Controler() { input = new JTextField(); Centigrade = new JLabel("摄氏温度"); Fahrenheit = new JLabel("华氏温度"); convent = new JButton("转换"); s1 = t(); setLayout(new GridLayout(2,2)); add(input); add(Centigrade); add(convent); ------------- ------------- add(Fahrenheit); ionListener(this); } public void actionPerformed(ActionEvent ae) { //异常机制完成验证 try { if(nt(t()) < -273.15) { throw new Abnormity(); } else ------------- //处理温度不合法的情况 ------------- { t((nt(t())) + "华氏温度"); } } catch(Abnormity e) { t(sage()); } catch(NumberFormatException nf) { t("请规范你的输入"); } } ------------- ------------- //完成温度转换 double count(double condition) { double result; result = condition * 1.8 + 32; return result; } } class Abnormity extends Exception { public Abnormity() { super("你输入的温度值超界了"); ------------- //异常处理 ------------- } } 16 25 5 4 ------------- ------------- 3 2 1 -------------


发布评论