文档库 最新最全的文档下载
当前位置:文档库 › java程序设计的编程

java程序设计的编程

java程序设计的编程
java程序设计的编程

面向对象程序设计上机复习

1、用for循环求1+3+…+99之和。

// Programme Name Sum.java

public class Sum {

public static void main(String[] args) {

int i,sum=0;

for(i=1;i<=50;i++)

sum=sum+2*i-1;

System.out.println(“1+3+…+99=”+sum);

}

}

****2、编写一个Java程序,用if-else语句判断某年份是否为闰年。

// Programme Name LeapYear.java

public class LeapYear{

public static void main(String args[]){

int year=2010;

if(args.length!=0)

year=Integer.parseInt(args[0]);

if((year%4==0 && year%100!=0)||(year%400==0))

System.out.println(year+" 年是闰年。");

else

System.out.println(year+" 年不是闰年。");

}

}//if-else语句

3、已知圆的周长计算公式为S = 2×π×r,圆的面积计算公式为A = π×r×r,设计一个Java类Circle,编写求解圆的周长和面积的函数。并求解当r=10时,圆的周长与面积。

// Programme Name Circle.java

public class Circle{

private double radius;

final double PI= 3.1415926;

public Circle(double r){

this.radius= r; }

public double getArea(){

return PI * this.radius * this.radius; }

public double getPerometer(){

return 2 * PI * this.radius; }

void display(){

System.out.printf("圆的半径是%f, 面积= %f, 周长= %f\n",

this.radius, this.getArea(), this.getPerometer()); }

public static void main(String[] args) {

System.out.println("*************** Circle *************");

Circle ci = new Circle(10);

ci.display();

}

}

4. 利用线程方法编写JApplet程序,实现在浏览器端实时动态显示本地系统时钟

// Programme Name Watch.java

import java.applet.Applet;

import java.awt.*;

import java.text.DateFormat;

import java.util.*;

public class Watch extends Applet {

public void paint(Graphics g) {

Date d= new Date();

DateFormat ldf = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG); //System.out.println("现在系统时间是(long):"+ ldf.format(d));

String time = ldf.format(d).toString();

g.drawString(time, 100, 100);

try {

Thread.sleep(1000);

} catch (InterruptedException e) { }

repaint();

}

}

JavaAppletDemo

//保存为Watch.html 文件

****5. (1)编写一个圆类Circle,该类拥有:

①一个成员变量

radius(私有,浮点型);// 存放圆的半径;

②两个构造方法

Circle( ) // 将半径设为0

Circle(double r ) //创建Circle对象时将半径初始化为r

③三个成员方法

double getArea( ) //获取圆的面积

double getPerimeter( ) //获取圆的周长

void show( ) //将圆的半径、周长、面积输出到屏幕(2)编写一个圆柱体类Cylinder,它继承于上面的Circle类。还拥有:

①一个成员变量

double hight(私有,浮点型);// 圆柱体的高;

②构造方法

Cylinder (double r, double h ) //创建Circle对象时将半径初始化为r

③成员方法

double getVolume( ) //获取圆柱体的体积

void showVolume( ) //将圆柱体的体积输出到屏幕

编写应用程序,创建类的对象,分别设置圆的半径、圆柱体的高,计算并分别显示圆半径、圆面积、圆周长,圆柱体的体积。

//Programme Name TestCylinder.java

class Circle { //定义父类--园类

private double radius; //成员变量--园半径

Circle() { //构造方法

radius=0.0;

}

Circle(double r) { //构造方法

radius=r;

}

double getPerimeter() { //成员方法--求园周长

return 2*Math.PI*radius;

}

double getArea() { //成员方法--求园面积

return Math.PI*radius*radius;

}

void disp() { //成员方法--显示园半径、周长、面积

System.out.println("园半径="+radius);

System.out.println("园周长="+getPerimeter());

System.out.println("园面积="+getArea());

}

}

class Cylinder extends Circle { //定义子类--圆柱类

private double hight; //成员变量--园柱高

Cylinder(double r,double h) { //构造方法

super(r);

hight=h;

}

public double getVol() { //成员方法--求园柱体积

return getArea()*hight;

}

public void dispV ol() { //成员方法--显示园柱体积

System.out.println("圆柱体积="+getV ol());

}

}

public class TestCylinder { //定义主类

public static void main(String[] args) { //主程入口

Circle Ci=new Circle(10.0); // 生成园类实例

Ci.disp(); // 调用园类的方法

Cylinder Cyl=new Cylinder(5.0,10.0); //生成圆柱类实例

Cyl.disp(); //调用父类方法

Cyl.dispV ol(); //调用子类方法

}

}

****6. 应用FileInputStream类,编写应用程序,从磁盘上读取一个Java程序,并将源程序代码显示在屏幕上。(被读取的文件路径为:E:/test/Hello.java)

// Programme Name FISDemo.java

import java.io.*;

public class FISDemo {

public static void main(String args[]) {

byte[] buf=new byte[2056];

try{

FileInputStream fileIn=new FileInputStream("e:/test/Hello.java");

int bytes=fileIn.read(buf,0,2056);

String str=new String(buf,0,bytes);

System.out.println(str);

}catch(Exception e){

e.printStackTrace( );

}

}

}

*****7. 设计一个表示用户的类User,类中有用户名、口令(私有的)和记录用户数(静态)的成员变量。定义类的构造方法、设置和获取口令的方法及返回类对象信息的方法(包括用户名和口令)。编写应用程

序测试User类。

//Pragramme name UserExample;

class User {

private String name, password;

static int count=0;

public User() { count++;} // 无参构造方法

public User (String name) {https://www.wendangku.net/doc/5c1418452.html,=name; count++; } //一个参数构造方法

public User (String name,String password) {

https://www.wendangku.net/doc/5c1418452.html,=name; this.password=password; count++; } //两个参数构造方法

public void setPassword(String password){ this.password=password;}

public String getPassword(String name){ return password;}

public String message(){ return"Name="+name+",Password="+password;}

public static int getCount( ){ return count;}

}

public class UserExample{

public static void main(String[ ] args) { //主程序入口

User tom=new User("Tom");

tom.setPassword("tom123");

System.out.println(tom.message()+",Count="+User.getCount());

User Kate=new User("Kate","Kate666");

System.out.println(Kate.message()+",Count="+User.getCount());

}

}

8. 编写程序,在屏幕上显示带标题的窗口,并添加一个按钮。当用户单击按钮时,结束程序。// Programme Name ButtonEventDemo.java

import javax.swing.*;

import java.awt.event.*;

public class ButtonEventDemo extends JPanel implements ActionListener{

protected JButton b1; //声明一个按钮对象

public ButtonEventDemo() { //构造方法

ImageIcon ButtonIcon = new ImageIcon("images/green.png"); //创建按钮的图标对象

b1 = new JButton("退出按钮", ButtonIcon); //生成按钮对象

b1.setMnemonic(KeyEvent.VK_E); //设置b1的助记符是Alt+E

b1.setToolTipText("这是退出按钮。"); // 设置按钮提示条

this.add(b1); //往面板对象中加载按钮

b1.addActionListener(this); //本类对象注册为按钮的事件监听器

}

public void actionPerformed(ActionEvent e){ //按钮事件响应方法

System.exit(0); //按b1则退出主程序

}

private static void createGUI() { //创建窗体

JFrame.setDefaultLookAndFeelDecorated(true); //设置java隐含观感

JFrame frame = new JFrame("按钮测试"); //生成应用程序主窗体

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置关闭时隐含操作

ButtonEventDemo CPane = new ButtonEventDemo(); //生成主类对象--面板

CPane.setOpaque(true); //面板要求不透明

frame.setContentPane(CPane); //设置主类对象为主窗体的内容面板

frame.pack(); //主窗体紧缩显示

frame.setVisible(true); //设置主窗体可见

}

public static void main(String[] args) { //将createGUI()列入线程

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

createGUI();

}

});

}

}

9. 编程实现如下功能:利用字节文件流(FileOutputStream)在E:/TEST文件夹下建立一个文本文件info.txt(如不存在则创建之),并写入如下两行字符串信息:

“使用Java 字节流FileOutputStream写入文件”、

“利用Java 字符流FileReader和BufferedReader读取并显示文件信息”,

然后利用字符文件流(FileReader和BufferedReader)读取显示该文件的内容。

// Programme Name StreamDemo.java

import java.io.BufferedReader;

import java.io.File;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.IOException;

public class StreamDemo {

static String filename = "E:/test/info.txt";

static File file = new File(filename);

private static void FileOutStreamDemo(){

StringBuffer sb= new StringBuffer("使用Java 字节流FileOutputStream写入文件");

sb.append("\n利用Java 字符流FileReader和BufferedReader读取并显示文件信息");

System.out.println(sb);

try{

if (!file.exists() | (!file.isFile())) {

System.out.printf("指定的文件:%s 不存在!\n", filename);

File d= file.getParentFile();

if (!d.exists()| !d.isDirectory()){

d.mkdir();

System.out.printf("%s 已创建成功!\n", d.getPath()); }

if (file.createNewFile()) {

System.out.printf("%s 已创建成功!\n", filename); } }

System.out.println(file.getAbsolutePath());

byte[] b = sb.toString().getBytes();

FileOutputStream fos = new FileOutputStream(file);

fos.write(b);//直接覆盖原文件

fos.close();

//System.out.println(b.hashCode());

}catch(IOException ie){

System.out.println(ie.getLocalizedMessage()); } }

private static void FileReaderDemo() {

try {

/*

FileReader fr = new FileReader(file);

int in;

while ((in = fr.read()) != -1) {

System.out.print((char)in); }

fr.close();//g关闭之前会先flush

*/

BufferedReader br = new BufferedReader(new FileReader(file));//更简单一点

String s;

do{

s= br.readLine();

if(s==null)

break;

System.out.println(s);

}while(s!=null);

} catch (IOException e) {

System.out.println(e.getLocalizedMessage());

e.printStackTrace();} }

public static void main(String []args){

FileOutStreamDemo();

System.out.printf("%s文件的大小是:%d bytes\n", filename, file.length());

FileReaderDemo(); }}

******10、定义一个表示学生信息的类Student,要求如下:

(1)类Student的成员变量:

sNO表示学号;sName表示姓名;sSex表示性别;sAge表示年龄;sJava:表示Java课程成绩。

(2)类Student带参数的构造方法:

在构造方法中通过形参完成对成员变量的赋值操作。

(3)类Student的方法成员:

getNo():获得学号;

getName():获得姓名;

getSex():获得性别;

getAge()获得年龄;

getJava():获得Java 课程成绩

(4)根据类Student的定义,创建五个该类的对象,输出每个学生的信息,计算并输出这五个学生Java 语言成绩的平均值,以及计算并输出他们Java语言成绩的最大值和最小值。

//Pragramme name Student;

public class Student {

String sNO,sName,sSex;

int sAge,sJava;

public Student(String XH,String XM,String XB,int NL,int XF) {

super();

sNO=XH;

sName=XM;

sSex=XB;

sAge=NL;

sJava=XF;

}

public String getNO() {

return sNO;

}

public String getName() {

return sName;

}

public String getSex() {

return sSex;

}

public int getAge() {

return sAge;

}

public int getJava() {

return sJava;

}

public static void main(String[] args) {

Student[] st=new Student[5];

st[0]=new Student("09zc01","张三","男",19,94);

st[1]=new Student("09zc02","李四","男",20,85);

st[2]=new Student("09zc03","王五","女",18,96);

st[3]=new Student("09zc04","赵六","男",17,90);

st[4]=new Student("09zc05","杨七","女",21,88);

int max=0,min=100,sum=0;

System.out.println(" 学生信息:");

for (int i=0;i

i f (st[i].sJava < min)

min=st[i].sJava;

i f (st[i].sJava > max)

max=st[i].sJava;

s um=sum+st[i].sJava;

S ystem.out.println("学生编号:"+st[i].getNO()+",姓名:"+st[i].getName()+",性别:"+st[i].getSex()+",年龄:"+st[i].getAge()+", Java课学分:"+st[i].getJava());

}

System.out.println();

System.out.println(" 共有学生:"+st.length+",平均成绩:"+sum/st.length);

System.out.println(" 最小学分:"+min+",最大学分:"+max);

}

}

相关文档