1. 按下列要求编写程序。
(1)定义一个Student类,其中含有学号(stuNo)、姓名(stuName)、成绩(stuMark)三个成员变量。
(2)为Student类添加一个构造函数,初始化所有的成员变量。
(3)为Student类添加四个成员方法,分别用于获得学号(getStuNo)、获得姓名(getStuName)、获得成绩(getStuMark)、修改成绩(modiStuMark)。
(4)在上述程序段的基础上,创建类Student的一个对象st1,并编写完整的程序使用Student类中的每个方法。(单独创建执行类或者把Student类作为执行类)
//定义Student类,此时文件名为“Student.java”
public class Student {
public int stuNo;
public String stuName;
public int stuMark;
//含有学号(stuNo)、姓名(stuName)、成绩(stuMark)三个成员变量
public Student(int stuNo,String stuName,int stuMark){
this.stuNo=stuNo;
this.stuName=new String(stuName);//string字符串初始化
this.stuMark=stuMark;
}//添加一个构造函数(函数名与类名相同),初始化所有的成员变量
int getStuNo(){
return stuNo;
}
String getStuName(){
return stuName;
}
int getStuMark(){
return stuMark;
}//以上三个均是无参返回
int modiStuMark(int stuMark){
return this.stuMark=stuMark;//含参返回
}//添加四个成员方法,
//分别用于获得学号(getStuNo)获得姓名(getStuName)获得成绩(getStuMark)修改成绩(modiStuMark)
public static void main(String[] args) {
Student stl=new Student(5132,"lmw",99);
stl.getStuMark();
stl.getStuName();
stl.getStuMark();//调用三个方法
System.out.println(stl.stuName+" "+stl.stuNo+" "+stl.stuMark);
stl.modiStuMark(100);
System.out.println(stl.stuName+" "+stl.stuNo+" "+stl.stuMark);
//Student作为执行类
}
}
2. 按下列要求编写程序。
(1)设计一个商品数据类PClass,其中包含两个成员变量:商品编号(int pNumber)、单价(double pPrice);包含两个成员方法:显示编号showNumber()和显示单价showPrice();及两个构造函数PClass()和PClass(int number,double price)。
(2)设计一个完整的Java程序使用上题的类PClass,建立一个Pclass类的对象p,即PClass p=new PClass(1,1000.0)。然后使用showNumber()方法将商品编号显示出来,使用showPrice()方法将单价显示出来。(单独创建执行类或者把PClass类作为执行类)
//定义商品数据类PClass,此时文件名为“PClass.java”
public class PClass {
int pNumber;
double pPrice;
//两个成员变量:商品编号(int pNumber)单价(double pPrice)
void showNumber(){
System.out.println("商品的编号是:"+pNumber);
}
void showPrice(){
System.out.println("商品的单价是:"+pPrice);
}
//两个成员方法:显示编号showNumber()和显示单价showPrice()
PClass(){}//空的构造函数
PClass(int number,double price){
pNumber=number;
pPrice=price;
}//初始化赋值
public static void main(String[] args) {
PClass p=new PClass(1,1000.0);//对象必须被用,否则会报错
p.showNumber();
p.showPrice();
}//把PClass类作为执行类
}
3. 创建一个类,在其中定义一个方法print(int a)打印一个给定的整数,然后重载print方法,依次完成下列要求,然后编写完整的程序。
(1)打印一个给定的double类型的数。
(2)打印两个给定的整数。
(3)打印一个字符串。
设计一个完整的Java程序使用上述各个方法。(单独创建执行类或者把本类作为执行类)
//定义类print,此时文件名为“print.java”
public class print {
void print(int a){
System.out.println("a="+a);
}
void print(double b){
System.out.println("double b="+b);
}
void print(int c,int d){
System.out.println("int c="+c);
System.out.println("int d="+d);
}
void print(String e){
System.out.println("String e="+e);
}
//重载print方法
public static void main(String[] args) {
print printf=new print();
printf.print(1);
printf.print(2.0);
printf.print(3,4);
printf.print("lmw");
}//把本类作为执行类
}
4. 按下列要求编写程序段。
(1)设计一个类WClass继承第2题中的商品数据类PClass,在其中增加成员变量商品等级(int pStage)和成员方法showStage(),两个构造函数WClass()和WClass(int number,double price,int stage)。
(2)设计一个完整的Java程序使用类WClass,建立一个WClass类的对象w,即WClass w=new WClass(1,1000.0,2)。然后分别使用showNumber()、showPrice()、showStage()方法将商品编号、单价和等级显示出来。(单独创建执行类或者把本类作为执行类)
//定义商品数据类PClass,此时文件名为“lmw.java”
class PClass {
int pNumber;
double pPrice;
// 两个成员变量:商品编号(int pNumber)单价(double pPrice)
void showNumber() {
System.out.println("商品的编号是:" + pNumber);
}
void showPrice() {
System.out.println("商品的单价是:" + pPrice);
}
// 两个成员方法:显示编号showNumber()和显示单价showPrice()
PClass(){
}// 空的构造函数
PClass(int number, double price) {
pNumber = number;
pPrice = price;
}// 初始化赋值
}
class WClass extends PClass {//定义一个类WClass继承商品数据类PClass
int pStage;//增加成员变量商品等级(int pStage)
void showStage(){//增加成员方法showStage()
System.out.println("商品等级是:"+pStage);
}
//两个构造函数WClass()和WClass(int number,double price,int stage)
WClass() {}
WClass(int number,double price,int stage){
pNumber=number;
pPrice=price;
pStage=stage;
}
}
public class lmw{
public static void main(String[] args){
WClass w=new WClass(1,1000.0,2);//对象必须被用,否则会报错
w.showNumber();
w.showPrice();
w.showStage();
}
}
5. 按下列要求编写程序段,然后编写完整的Java程序实现。
(1)设计一个银行账户类BankAccount,其中包含一个double类型的成员变量mRate,表示银行存款利率,并定义setRate(double rate)方法设置并打印存款利率。
(2)创建类BankAccount的子类LoanAccount表示贷款账户,在子类中隐藏父类中的成员变量mRate,并覆盖父类中的成员方法setRate(double rate),以设置并打印贷款利率。
(3)在子类LoanAccount中定义message()方法,分别调用父类和子类中的setRate方法。
单独创建执行类或者把本类作为执行类。
//文件名为“lmw.java”
class BankAccount{
double mRate;
void setRate(double rate){
mRate=rate;
System.out.println("父类利润:"+mRate);
}
}
class LoanAccount extends BankAccount{
double mRate;//只要子类中声明的变量和父类同名,子类就隐藏了父类的成员变量
void setRate(double rate){
mRate=rate;
System.out.println("子类利润:"+mRate);
}//子类通过重写覆盖父类的成员方法,即子类方法名,参数个数及类型和父类完全相同
void message(double f,double s){
setRate(f);
super.setRate(s);
}
}
public class lmw{
public static void main(String[]args){
LoanAccount lmw= new LoanAccount();
lmw.message(3.14,5.28);
}
}
6. 若有一个接口A说明一个面积方法area(),
interface A {
public double area();
}
请设计一个三角形类GCLass实现接口A中的area()方法,GClass构造函数的参数为三边长s1、s2、s3,均为整数。
class GCLass implements A{
public void area(int s1,int s2,int s3){
double p,s;
p=(s1+s2+s3)/2;
s=Math.sqrt(p*(p-s1)*(p-s2)*(p-s3));
System.out.println("area="+s);
}
}
interface A {
public double area();
}
public class lmw{
public static void main(String[]args){
GCLass s=new GCLass();
s.area(3,4,5);
}
}
7. 包mycolor中有一个类Color,其中含有一个方法printColor(String color)能够输出参数指定的字符串。编写一个Java程序,引入mycolor包,并使用printColor方法输出参数指定的字符串。
package mycolor;
public class color {
public color(){
}
public void printcolor(String color){
System.out.println(color);
}
}
import mycolor.color;
public class lmw {
public static void main(String[] args) {
color mine=new color();
mine.printcolor("red");
}
}
8.使用BufferedReader类从键盘接收一个整数和一个实数,计算它们的差,然后在屏幕上输出。
import java.io.*;
public class lmw {
public static void main(String args[]) throws IOException {
int i;
float f, r;
BufferedReader din = new BufferedReader(
new InputStreamReader(System.in));
System.out.print("input i: ");
i = Integer.parseInt(din.readLine());
System.out.print("input f: ");
f = Float.parseFloat(din.readLine());
r = i - f;
System.out.println(i + "-" + f + "=" + r);
}
}
9.编程创建一个 Point 类在其中定义两个变量表示一个点的坐标值再定义构造函数初始化为坐标原点然后定义一个方法实现点的移动再定一个方法打印当前点的坐标。并创建一个对象验证。
class Point {
int x, y;
void move(int newX, int newY) {
x = newX;
y = newY;
}
void print() {
System.out.println("x=" + x + " y=" + y);
}
}
public class lmw {
public static void main(String args[]) {
Point p2;
p2 = new Point();
p2.print();
p2.move(50, 50);
System.out.println("**after moving**");
p2.print();
}
}
10.创建一个顺序文件,向其中写入部分用户键盘输入的字符,并回显在屏幕上。
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Scanner;
public class lmw {
private OutputStream outStream;
private File f;
public OutputStream getOStream(File f) {
try {
outStream = new FileOutputStream(f);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("文件未找到");
return null;
}
return outStream;
}
public static void main(String[] args) {
lmw t = new lmw();
File f = new File("c:/a.txt");
OutputStream ostream = t.getOStream(f);
Scanner sc = new Scanner(System.in);
while (true) {
try {
String tmpString = sc.nextLine();
if (tmpString != null) {
System.out.println(tmpString);
ostream.write(tmpString.getBytes());
}
if (tmpString.equals("exit")) {
ostream.close();
System.exit(0);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
11
1.编程创建一个Box类,在其中定义三个变量表示一个立方体的长、宽和高,再定义一个方法setDemo对这三个变量进行初始化,然后定义一个方法求立方体的体积。创建一个对象,求给定尺寸的立方体的体积。
2.将上题的方法setDemo改用构造函数实现初始化。
public class lmw {
public static void main(String[] args) {
Box l=new Box();
l.setDemo(1,2,3);
System.out.println(l.get());
}
}
class Box{
private double length,width,height;
public void setDemo(double lenth,double width,double height)
{
this.length=lenth;
this.width=width;
this.height=height;
}
public double get(){
return length*width*height;
}
}
public class lmw{
public static void main(String[] args) {
Box l;
l=new Box(1,2,3);
System.out.println(l.get());
}
}
class Box{
private double length,width,height;
public Box(double lenth,double width,double height)
{
this.length=lenth;
this.width=width;
this.height=height;
}
public double get(){
return length*width*height;
}
}
12.使用Java Aplication的命令行参数读入两个数据,计算它们的差,然后将差输出。如果参数的数目不足,显示相应提示信息并退出程序的执行。
public class lmw {
public static void main(String[] args) {
int i = 0, j = 0;
if (args.length == 2) {
i = Integer.valueOf(args[0]);
j = Integer.valueOf(args[1]);
System.out.println(i + "+" + j + "=" + (i + j));
} else
System.out.println("参数不足");
}
}
13.编程包含两个按钮和一个标签,将发生单击事件的按钮上的文本信息显示在标签中。
import java.awt.*;
import java.awt.event.*;
public class lmwjava implements ActionListener
{
Frame fr;
Panel pa;
Button bt1,bt2;
Label la;
lmwjava()
{
fr=new Frame();
pa=new Panel();
bt1=new Button("Button 1");
bt1.addActionListener(this);
bt2=new Button("Button 2");
bt2.addActionListener(this);
la=new Label("显示文本信息:");
pa.add(bt1);
pa.add(bt2);
fr.add(pa,"North");
fr.add(la,"Center");
fr.setVisible(true);
fr.setSize(400,400);
}
public void actionPerformed(ActionEvent e)
{
Object s=e.getSource();
if(s==bt1)
la.setText("Button 1");
if(s==bt2)
la.setText("Button 2");
}
public static void main(String[] args) {
new lmwjava();
}
}
14.编程包含一个标签和一个按钮,单击按钮时,标签的内容在“你好”和“再见”之间切换。
import java.awt.*;
import java.awt.event.*;
public class lmwjava extends Frame implements ActionListener
//承Java的JFrame类,JFrame 是Java的窗体类,继承它可以重写它的一些方法达到更方便编程的作用
//implements ActionListener 是实现 ActionListener 接口,为动作监听接口,是Java swing 监听窗体动作的一个接口
{
Panel cards;//Panel类也是一个容器,但是她不能单独存在只能存在于其他容器中,一个Panel对象代表了一个长方形区域这个区域可以容纳其他组件,在程序中通常会使用Panel来实现一个特殊的布局
CardLayout Clayout = new CardLayout();// 定义卡片对象
public lmwjava()
{
Panel p = new Panel();
Button bt = new Button("切换");
bt.addActionListener(this);//这里必须有事件监听对象,并且this指本身这个对象,这个类会实现监听器这个接口
p.add(bt);
add("South", p);//把切换按钮添加到界面的南方
cards = new Panel();
cards.setLayout(Clayout);//设置当前页面布局
Panel p1 = new Panel();
p1.add(new Label("你好"));
Panel p2 = new Panel();
p2.add(new Label("再见"));
cards.add("Panel with Label", p1);
cards.add("Panel with Label", p2);
add(cards);//把切换按钮添加到界面
}
public void actionPerformed(ActionEvent e)
//当特定于组件的动作(比如被按下)发生时,由组件(比如 Button)生成此高级别事件。事件被传递给每一个 ActionListener 对象,这些对象是使用组件的 addActionListener 方法注册的,用以接收这类事件。
{
Clayout.next(cards);
}
public static void main(String args[]) {
lmwjava window = new lmwjava();
window.setTitle("转换");//添加界面主题
window.pack();//依据放置的组件设定窗口的大小,使之正好能容纳放置的所有组件
window.setVisible(true);//可以运行开始画图了
}
}
14.编程包含一个文本框和一个文本区域,文本框内容改变时,将文本框中的内容显示在文本区域中;在文本框中按回车键时,清空文本区域的内容。
import java.awt.*;
import java.awt.event.*;
import java.util.*;
class Chanword extends Frame implements ActionListener,TextListener
{
TextArea text;
TextField input;
Chanword(String s)
{
super(s);
setLayout(new FlowLayout());
input=new TextField(20);
add(input);
input.addActionListener(this);
input.addTextListener(this);
text=new TextArea(10,20);
add(text);
text.setEditable(false);
setBounds(100,100,300,300);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e)
{
text.setText(null);
}
public void textValueChanged(TextEvent e)
{
String sh=input.getText();
text.append(sh);
}
}
public class lmwjava
{
public static void main(String argsp[])
{
Chanword change=new Chanword("修改");
}
}
15.使用InetAddress类的方法获取www.ytu.edu.cn的主机的IP地址;获取本地机的名称和IP地址。
import java.net.InetAddress;
public class lmwjava {
public static void main(String[] args) throws Exception {
InetAddress localAddress = InetAddress.getLocalHost();
InetAddress remoteAddress = InetAddress.getByName("www.ytu.edu.cn");
System.out.println("本机的IP地址:" + localAddress.getHostAddress());
System.out.println("本地的主机名:" + localAddress.getHostName());
System.out.println("烟台大学的IP地址:" + remoteAddress.getHostAddress());
}
}
16.多线程计算1到100的求和
public class lmwjava {
public static int sum = 0;
public static Object LOCK = new Object();
public static void main(String[] args) throws InterruptedException {
lmwjava add = new lmwjava();
ThreadTest thread1 = add.new ThreadTest(1, 25);
ThreadTest thread2 = add.new ThreadTest(26, 50);
ThreadTest thread3 = add.new ThreadTest(51, 75);
ThreadTest thread4 = add.new ThreadTest(76, 100);
thread1.start();
thread2.start();
thread3.start();
thread4.start();
thread1.join();
thread2.join();
thread3.join();
thread4.join();
System.out.println("total result: "+sum);
}
class ThreadTest extends Thread {
private int begin;
private int end;
@Override
public void run() {
synchronized (LOCK) {
for (int i = begin; i <= end; i++) {
sum += i;
}
System.out.println("from "+Thread.currentThread().getName()+" sum="+sum);
}
}
public ThreadTest(int begin, int end) {
this.begin = begin;
this.end = end;
}
}
}