1.随机产生20个50~100之间的整数,输出这20个数并找出最大数及最小数输出
public class Test1 {
public static void main(String[] args) {
int math[] = new int[20];
int max = 0;int min = 100;
for(int i = 0;i<20;i++){
math[i] = (int)(Math.random()*50+50);
System.out.print(math[i]+" ");
}
System.out.println();
if(maxif(min>math[i]) min=math[i]; }System.out.println("max:"+max);System.out.println("min:"+min); }}2.创建一个图书类,类中包含的属性有:书名、作者、出版社;包含的方法有:构造方法,设置书籍状态,查看书籍状态。书籍状态有在馆和外借两种。public class Test2 {public static void main(String[] args){Book book=new Book("Java程序设计","李伟","清华大学出版社");book.setzt(true);book.getzt();}}class Book{private String Bname;private String Aname;private String Baddress;Book(String Bname,StringAname,StringBaddress){this.Bname=Bname;this.Aname=Aname;this.Baddress=Baddress;}privatebooleanzt;public void setzt(booleanzt){this.zt=zt;}public void getzt(){if(zt==true)System.out.println("在馆");elseSystem.out.println("外借");}}3.设计一个Birthday类,其成员变量有:year,month,day;提供构造方法、输出Birthday对象值的方法和计算年龄的方法。编写程序测试这个类。public class Test3 {public static void main(String[] args){Birthday b=new Birthday(2010,6,8);b.printBirthDay();System.out.println(b.printAge()); } }class Birthday{privateint year;privateint month;privateint day;public Birthday(intyear,intmonth,int day){this.year=year;this.month=month;this.day=day;}public void printBirthDay(){System.out.println(year+"-"+month+"-"+day);}publicintprintAge(){return 2017-year;}}4. 编写一个类,描述汽车,其中用字符型描述车的牌号,用浮点型描述车的价格。编写一个测试类,其中有一个修改价格的方法,对汽车对象进行操作,根据折扣数修改汽车的价格,最后在main()方法中输出修改后的汽车信息public class Test4 {public static void main(String[] args){Car c=new Car("奔驰S6OO",50000);c.dismessage();}}class Car{String chePai;float price;float price1;Car(String chePai,float price){this.chePai=chePai;this.price1=price*4/5;this.price=price;}void dismessage(){System.out.println("这辆车的品牌是"+chePai+"原价是"+price+"打折后为"+price1);} }5.编写一个异常类MyException,再编写一个类Student,该类有一个产生异常的方法speak(int m)。要求参数m的值大于1000时,方法抛出一个MyException对象。最后编写主类,在主方法中创建Student 对象,让该对象调用speak()方法。classMyException extends Exception{privateint m;MyException(int m){this.m=m; }public String getMessage(){return "出现异常:参数"+this.m+"大于1000"; } }class Student{publicint speak(int m)throws MyException{if(m>1000) throw new MyException(m);return m; } }public class Test5 {public static void main(String[] args) {try{Student s=new Student();System.out.println("输出的结果是:"+s.speak(5));System.out.println("输出的结果是:"+s.speak(5000));}catch(MyException e){System.out.println(e.getMessage()); } } }6. 单击窗体的关闭按钮时,跳出如下对话框,选择“是”窗体关闭,选择“否”,窗体不关闭importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;class JFrame6{JFrame frame=new JFrame();JFrame15(){frame.setTitle("关闭窗体时,问一声");frame.setBounds(100,100,300,200);frame.setVisible(true);frame.addWindowListener(new myWindowListener()); } classmyWindowListener extends WindowAdapter{public void windowClosing(WindowEvent e){int result=JOptionPane.showConfirmDialog(frame,"你确定要关闭窗体?","确认对话框",JOptionPane.YES_NO_OPTION);if(result==JOptionPane.OK_OPTION){System.exit(0); }else if(result==JOptionPane.NO_OPTION){frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);} } } }public class Text6 {public static void main(String[] args) {new JFrame6();}}7. 创建一个File类的对象,首先判断此配置文件是否存在,如果不存在,则调用createNewFile方法创建一个文件,然后从键盘输入字符存入数组里,创建文件输出流,把数组里的字符写入到文件中,最终保存在“Example7.txt”文件中import java.io.*;public class Test7 {public static void main(String[] args) {File file=new File("D:\\","Example7.txt");byte[] b=new byte[1000];int n;try{if(!file.exists())file.createNewFile();FileOutputStreamfos=new FileOutputStream(file,true);n=System.in.read(b);fos.write(b,0,n);fos.close();}catch(Exception e){e.getMessage();}}}9. 编写MyThread线程类,在该类中实现九九乘法表的动态输出,每隔1秒输出乘法表中的一个运算结果。public class Test9 {public static void main(String[] args) {Thread t=new MyThread();t.start();}}classMyThread extends Thread{public void run(){inti,j;try{for(i=1;i<=9;i++){for(j=1;j<=i;j++){System.out.print(j+"*"+i+"="+i*j+"\t");sleep(1000);}System.out.println();}}catch(Exception e){e.toString();}}}9.编写类OverLoading,在该类中定义3个方法:一个info()方法是没有参数的,一个info()方法需要使用一个整形参数,一个info()方法需要使用一个String类型参数。在main方法中进行测试。运行结果如下:public class Test9 {public static void main(String[] args) {OverLoadingol=new OverLoading();https://www.360docs.net/doc/d718877213.html,();https://www.360docs.net/doc/d718877213.html,(5);https://www.360docs.net/doc/d718877213.html,("HelloWorld");}}classOverLoading{public void info(){System.out.println("您调用的是无参数的方法");}public void info(int n){System.out.println("您调用的是整形类型参数的方法,参数是:"+n);}public void info(String s){System.out.println("您调用的是String类型参数的方法,参数是:"+s);}}10. 编写类Shape,该类是一个抽象类。在该类中定义一个抽象方法:getArea()。编写类Circle,该类继承自Shape并实现了其抽象方法getArea()。在该类的构造方法中,获得了圆形的半径,以此在getArea()中计算面积。abstract class Shape{public abstract double getArea();}class Circle extends Shape{private double r;public Circle(double r){this.r=r;}public double getArea(){//return Math.PI*Math.pow(r,2);return 3.14*r*r;}}public class Test10 {public static void main(String[] args) {Circle c=new Circle(3);System.out.println(图形的面积是:"+c.getArea());}}
if(min>math[i]) min=math[i]; }
System.out.println("max:"+max);
System.out.println("min:"+min); }}
2.创建一个图书类,类中包含的属性有:书名、作者、出版社;包含的方法有:构造方法,设置书籍状态,查看书籍状态。书籍状态有在馆和外借两种。
public class Test2 {
public static void main(String[] args){
Book book=new Book("Java程序设计","李伟","清华大学出版社");
book.setzt(true);
book.getzt();}}
class Book{
private String Bname;
private String Aname;
private String Baddress;
Book(String Bname,StringAname,StringBaddress){
this.Bname=Bname;
this.Aname=Aname;
this.Baddress=Baddress;}
privatebooleanzt;
public void setzt(booleanzt){
this.zt=zt;}
public void getzt(){
if(zt==true)
System.out.println("在馆");
else
System.out.println("外借");}}
3.设计一个Birthday类,其成员变量有:year,month,day;提供构造方法、输出Birthday对象值的方法和计算年龄的方法。编写程序测试这个类。
public class Test3 {
Birthday b=new Birthday(2010,6,8);
b.printBirthDay();
System.out.println(b.printAge()); } }
class Birthday{
privateint year;
privateint month;
privateint day;
public Birthday(intyear,intmonth,int day){
this.year=year;
this.month=month;
this.day=day;}
public void printBirthDay(){
System.out.println(year+"-"+month+"-"+day);}
publicintprintAge(){
return 2017-year;}}
4. 编写一个类,描述汽车,其中用字符型描述车的牌号,用浮点型描述车的价格。编写一个测试类,其中有一个修改价格的方法,对汽车对象进行操作,根据折扣数修改汽车的价格,最后在main()方法中输出修改后的汽车信息
public class Test4 {
Car c=new Car("奔驰S6OO",50000);
c.dismessage();}}
class Car{
String chePai;
float price;
float price1;
Car(String chePai,float price){
this.chePai=chePai;
this.price1=price*4/5;
this.price=price;}
void dismessage(){
System.out.println("这辆车的品牌是"+chePai+"原价是"+price+"打折后为"+price1);} }
5.编写一个异常类MyException,再编写一个类Student,该类有一个产生异常的方法speak(int m)。要求参数m的值大于1000时,方法抛出一个MyException对象。最后编写主类,在主方法中创建Student 对象,让该对象调用speak()方法。
classMyException extends Exception{
privateint m;
MyException(int m){
this.m=m; }
public String getMessage(){
return "出现异常:参数"+this.m+"大于1000"; } }
class Student{
publicint speak(int m)throws MyException{
if(m>1000) throw new MyException(m);
return m; } }
public class Test5 {
try{
Student s=new Student();
System.out.println("输出的结果是:"+s.speak(5));
System.out.println("输出的结果是:"+s.speak(5000));
catch(MyException e){
System.out.println(e.getMessage()); } } }
6. 单击窗体的关闭按钮时,跳出如下对话框,选择“是”窗体关闭,选择“否”,窗体不关闭
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
class JFrame6{
JFrame frame=new JFrame();
JFrame15(){
frame.setTitle("关闭窗体时,问一声");
frame.setBounds(100,100,300,200);
frame.setVisible(true);
frame.addWindowListener(new myWindowListener()); } classmyWindowListener extends WindowAdapter{
public void windowClosing(WindowEvent e){
int result=JOptionPane.showConfirmDialog(frame,"你确定要关闭窗体?","确认对话框",JOptionPane.YES_NO_OPTION);
if(result==JOptionPane.OK_OPTION){
System.exit(0); }
else if(result==JOptionPane.NO_OPTION){
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
} } } }
public class Text6 {
new JFrame6();}}
7. 创建一个File类的对象,首先判断此配置文件是否存在,如果不存在,则调用createNewFile方法创建一个文件,然后从键盘输入字符存入数组里,创建文件输出流,把数组里的字符写入到文件中,最终保存在“Example7.txt”文件中
import java.io.*;
public class Test7 {
File file=new File("D:\\","Example7.txt");
byte[] b=new byte[1000];
int n;
if(!file.exists())
file.createNewFile();
FileOutputStreamfos=new FileOutputStream(file,true);
n=System.in.read(b);
fos.write(b,0,n);
fos.close();}
catch(Exception e){
e.getMessage();}}}
9. 编写MyThread线程类,在该类中实现九九乘法表的动态输出,每隔1秒输出乘法表中的一个运算结果。
public class Test9 {
Thread t=new MyThread();
t.start();}}
classMyThread extends Thread{
public void run(){
inti,j;
for(i=1;i<=9;i++){
for(j=1;j<=i;j++){
System.out.print(j+"*"+i+"="+i*j+"\t");
sleep(1000);}
System.out.println();}}
e.toString();}}}
9.编写类OverLoading,在该类中定义3个方法:一个info()方法是没有参数的,一个info()方法需要使用一个整形参数,一个info()方法需要使用一个String类型参数。在main方法中进行测试。运行结果如下:
OverLoadingol=new OverLoading();
https://www.360docs.net/doc/d718877213.html,();
https://www.360docs.net/doc/d718877213.html,(5);
https://www.360docs.net/doc/d718877213.html,("HelloWorld");
classOverLoading{
public void info(){
System.out.println("您调用的是无参数的方法");
public void info(int n){
System.out.println("您调用的是整形类型参数的方法,参数是:"+n);
public void info(String s){
System.out.println("您调用的是String类型参数的方法,参数是:"+s);
10. 编写类Shape,该类是一个抽象类。在该类中定义一个抽象方法:
getArea()。
编写类Circle,该类继承自Shape并实现了其抽象方法getArea()。在该类的构造方法中,
获得了圆形的半径,以此在getArea()中计算面积。
abstract class Shape{
public abstract double getArea();
class Circle extends Shape{
private double r;
public Circle(double r){
this.r=r;
public double getArea(){
//return Math.PI*Math.pow(r,2);
return 3.14*r*r;
public class Test10 {
Circle c=new Circle(3);
System.out.println(图形的面积是:"+c.getArea());
}}