这期可能是关于Java的最后一期更新了!期末考试即将来临,老师终于官方的发题目啦!这让我倍感涕零。下面二话不说就抛代码了~
题一(基本循环、选择语句)
编写Demo.java,输出10~100以内既能被2整除,又能被3整除的整数,要求每行只能打印5个数。
public class Demo {
public static void main(String[] agrs) {
int count=0;
String s="";
for(int i=10;i<=100;i++) {
if(i%2==0 && i%3==0) {
if(count<5) {
count++;
s+=Integer.toString(i);
s+=" ";
}
else {
count=1;
System.out.println(s);
s="";
s+=Integer.toString(i);
s+=" ";
}
}
}
System.out.println(s);
}
}
public class Demo {
public static void main(String[] agrs) {
int count=0;
String s="";
for(int i=10;i<=100;i++) {
if(i%2==0 && i%3==0) {
if(count<5) {
count++;
s+=Integer.toString(i);
s+=" ";
}
else {
count=1;
System.out.println(s);
s="";
s+=Integer.toString(i);
s+=" ";
}
}
}
System.out.println(s);
}
}
题二(类变量与类方法操作)
编写一汽车类Car,其具有以下属性:
品牌:brand,类型String;发动机排量:engineDisplacement, 类型:double;
速度:speed,类型double;状态:status,类型boolean;
最高时速:maxSpeed,类型double
Car的方法:
构造方法:Car(String brand, double engineDisplacement, double maxSpeed)
启动:start( ),该方法使得status值变为true;
加速:speedUp( ),当汽车处于启动状态时,每调用一次,速度增加5,但速度不高于最高时速;
减速:slowDown( ),当汽车处于启动状态时,每调用一次,速度减5,但速度不小于0;
熄火:stop( ),当speed为0时,将status值变为false。
每个方法除改变成员变量外,还要打印出方法执行后的状态和速度。
试完成Car类的实现,并且编写main方法,实例化一个Car对象,其中品牌为”宝马”,排量2.0,最高时速为160,依次执行启动该汽车,加速到120,再减速到0,最后熄火,要求显示各执行结果。
public class Car {
private String brand;
private double engineDisplacement;
private double speed=0;
private boolean status=false;
private double maxSpeed;
public Car(String brand, double engineDisplacement,double maxSpeed) {
this.brand=brand;
this.engineDisplacement=engineDisplacement;
this.maxSpeed=maxSpeed;
System.out.println("Car构造函数执行后的status:"+this.status+",speed:"+this.speed);
}
public void start() {
this.status=true;
System.out.println("start函数执行后的status:"+this.status+",speed:"+this.speed);
}
public void speedUp() {
if(this.status==true && this.speed+5<=this.maxSpeed)
this.speed+=5;
System.out.println("speedUp函数执行后的status:"+this.status+",speed:"+this.speed);
}
public void slowDown() {
if(this.status==true && this.speed-5>=0)
this.speed-=5;
System.out.println("slowDown函数执行后的status:"+this.status+",speed:"+this.speed);
}
public void stop() {
if(this.speed==0)
this.status=false;
System.out.println("stop函数执行后的status:"+this.status+",speed:"+this.speed);
}
public static void main(String[] args) {
Car c=new Car("宝马",2.0,160);
c.start();
for(int i=0;i<24;i++) {
c.speedUp();
}
for(int i=0;i<24;i++) {
c.slowDown();
}
c.stop();
}
}
public class Car {
private String brand;
private double engineDisplacement;
private double speed=0;
private boolean status=false;
private double maxSpeed;
public Car(String brand, double engineDisplacement,double maxSpeed) {
this.brand=brand;
this.engineDisplacement=engineDisplacement;
this.maxSpeed=maxSpeed;
System.out.println("Car构造函数执行后的status:"+this.status+",speed:"+this.speed);
}
public void start() {
this.status=true;
System.out.println("start函数执行后的status:"+this.status+",speed:"+this.speed);
}
public void speedUp() {
if(this.status==true && this.speed+5<=this.maxSpeed)
this.speed+=5;
System.out.println("speedUp函数执行后的status:"+this.status+",speed:"+this.speed);
}
public void slowDown() {
if(this.status==true && this.speed-5>=0)
this.speed-=5;
System.out.println("slowDown函数执行后的status:"+this.status+",speed:"+this.speed);
}
public void stop() {
if(this.speed==0)
this.status=false;
System.out.println("stop函数执行后的status:"+this.status+",speed:"+this.speed);
}
public static void main(String[] args) {
Car c=new Car("宝马",2.0,160);
c.start();
for(int i=0;i<24;i++) {
c.speedUp();
}
for(int i=0;i<24;i++) {
c.slowDown();
}
c.stop();
}
}
题三(异常处理)
编写一银行类Bank,其中存款余额:balance,要求:
(1)构造函数:Bank(int balance);
(2)存款操作:deposit( );
(3)取款操作:withdraw( );
(4)获取余额:getBalance( );
编写main函数,实例化Bank对象,初始给定存款100,存入50,打印余额,连续取款120,40,打印余额。要求用自定义异常处理方式解决余额不足的问题。
public class Bank {
private double balance;
public Bank(int balance) {
this.balance=balance;
}
public void deposit(double store) {
this.balance+=store;
}
public void withdraw(double draw) {
try {
if(this.balance-draw<0)
throw new Exception("余额不足!无法取出");
else
this.balance-=draw;
}
catch(Exception e){
e.printStackTrace();
}
}
public double getBalance() {
return this.balance;
}
public static void main(String[] args) {
Bank bk=new Bank(100);
System.out.println("原账户余额:");
System.out.println(bk.getBalance());
bk.deposit(50);
System.out.println("存入50元操作后余额:");
System.out.println(bk.getBalance());
bk.withdraw(120);
System.out.println("去除120元操作后余额:");
System.out.println(bk.getBalance());
bk.withdraw(40);
System.out.println("去除40元操作后余额:");
System.out.println(bk.getBalance());
}
}
public class Bank {
private double balance;
public Bank(int balance) {
this.balance=balance;
}
public void deposit(double store) {
this.balance+=store;
}
public void withdraw(double draw) {
try {
if(this.balance-draw<0)
throw new Exception("余额不足!无法取出");
else
this.balance-=draw;
}
catch(Exception e){
e.printStackTrace();
}
}
public double getBalance() {
return this.balance;
}
public static void main(String[] args) {
Bank bk=new Bank(100);
System.out.println("原账户余额:");
System.out.println(bk.getBalance());
bk.deposit(50);
System.out.println("存入50元操作后余额:");
System.out.println(bk.getBalance());
bk.withdraw(120);
System.out.println("去除120元操作后余额:");
System.out.println(bk.getBalance());
bk.withdraw(40);
System.out.println("去除40元操作后余额:");
System.out.println(bk.getBalance());
}
}
题四(GUI设计)
利用Swing组件编写一个界面大小400×300像素的简易文本编辑器MyTextEditor,要求:
(1)程序启动后,多行文本输入框JTextArea中显示当前目录下myText.txt文件中原有内容,如果该文件不存在,则新建文件;
(2)保存(按钮)功能:将编辑器界面输入的内容写入myText.txt文件中保存;
(3)取消(按钮)功能:将文本框中内容清空;
(4)退出(按钮)功能:退出程序。
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
public class MyTextEditor extends JFrame implements ActionListener{
//成员变量
JButton b1,b2,b3;
JTextArea area;
String text;
//构造函数
MyTextEditor(String xTitle){
super(xTitle); //设置标题
setSize(400,300); //设置窗口大小
Container c=getContentPane(); //获得容器
c.setLayout(new GridLayout(2,1)); //设置布局
JPanel panel1=new JPanel(); //设置输入框小容器
panel1.setLayout(new FlowLayout(FlowLayout.CENTER));
area=new JTextArea();
area.setBackground(Color.green);
area.setLineWrap(true);
panel1.add(area);
c.add(panel1);
JPanel panel2=new JPanel(); //设置保存,取消,退出按钮
panel2.setLayout(new FlowLayout(FlowLayout.CENTER));
b1=new JButton("保存");
b1.addActionListener(this); //设置事件监督
b2=new JButton("取消");
b2.addActionListener(this);
b3=new JButton("退出");
b3.addActionListener(this);
panel2.add(b1);
panel2.add(b2);
panel2.add(b3);
c.add(panel2); //小容器放入大容器中
setVisible(true); //显示许可
File f=new File("myText.txt"); //创建File变量
if(f.exists()==false) { //是否存在该目录,不存在创建
try{
f.createNewFile();
}catch(Exception e){
e.printStackTrace();
}
}
//读入txt中的文字
try {
FileReader fr=new FileReader("myText.txt");
BufferedReader br=new BufferedReader(fr);
String aline;
while(br.ready()) {
aline=br.readLine();
area.append(aline+"\r\n");
}
fr.close();
br.close();
}catch(IOException e) {
e.printStackTrace();
}
}
//编写事件的接口方法
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b1) { //若按下保存按钮
String store;
store=area.getText(); //获得目前文本框中内容
try { //实现写入txt功能
FileWriter fw=new FileWriter("myText.txt");
fw.write(store);
fw.close();
} catch(IOException t){
t.printStackTrace();
}
}
else if(e.getSource()==b2) { //按下取消按钮
area.setText(""); //清空文本框中内容
area.setEditable(true);
}
else {
System.exit(0); //按下退出按钮,退出
}
}
//测试main函数
public static void main(String[] agrs) {
MyTextEditor m=new MyTextEditor("MyTextEditor");
}
}