Java练习(面向对象)

来源:互联网 发布:科学网 博弈 复杂网络 编辑:程序博客网 时间:2024/05/17 09:16
2、模拟ATM机进行账户余额查询
【提示:
编写账户类。属性:账户余额;方法:查询余额。
编写测试类,显示账户余额。】
package com.jredu.ch07;

import java.util.Scanner;

public class Atm {
public int money;

public void show(){
System.out.println("账户余额为:"+money);
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Atm a=new Atm();
int moneySum=20000;
System.out.println("金钱总额:"+moneySum);
System.out.print("取款:");
int num=sc.nextInt();
a.money=moneySum-num;
a.show();
}

}

3、某班实施测试,测试科目为java,学生有姓名和成绩信息,成绩有科目和分数信息。学生类的getResult方法显示输出成绩信息,setData方法实现初始化学生信息。编写学生类和成绩类并测试
学生类:
package com.jredu.ch07;

public class Student {
public String name;
publicScore1 score;//成绩类的引用,Score1为成绩类
//setData方法实现初始化学生信息
public void setData(String name,Score1 score){
this.name=name;
this.score=score;
}
//getResult方法显示输出成绩信息
public void getResult(){
System.out.println(name+"的成绩信息:"+score.name+","+score.score);

}
public static void main(String[] args) {
Student stu=new Student();
stu.setData("小张",new Score1("java",60));
stu.getResult();
}
}
成绩类:
package com.jredu.ch07;

public class Score1 {
public String name;
public int score;
public Score1(String name,int score){
this.name=name;
this.score=score;
}
}
 

 
1.定义一个学生类Student,包括 IDnamesexage等成员变量;要求有构造方法并有构造方法的重载,有相关成员方法来设置及获取其成员变量的值用输出成员信息的成员方法。并创建一个测试类,用不同的构造方法创建2Student类的对象,调用相关的方法设置对象的成员变量值,并输出相关信息。
package com.jredu.ch07;

public class Stu1 {
public int id;
public String name;
public char sex;
public int age;
public Stu1(){
}
public Stu1(int id, String name, char sex, int age) {
super();
this.name = name;
this.id = id;
this.sex = sex;
this.age = age;
}
public void show(){
System.out.println(id+" "+name+" "+sex+" "+age+"岁");
}
public static void main(String[] args) {
Stu1 s1=new Stu1();
s1.id=14174231;
s1.name="zqq";
s1.sex='女';
s1.age=21;
s1.show();
Stu1 s2=new Stu1(14174231,"zqq",'女',21);
s2.show();
}
}  
 


2.创建一个Rectangle类。该类拥有属性lengthwidth,每个属性的默认值均为1。该类拥有方法perimeterarea,分别用于计算矩形的周长和面积。该类还有设置和读取属性lengthwidth的方法。并测试Rectangle类的使用。  
 package com.jredu.ch07;

import java.util.Scanner;
public class Rectangle {
public int length;
public int width;
public void show(){
System.out.println("长方形的长为"+length+",宽为"+width);
}
public void perimeter(){
System.out.println("周长为:"+(length+width)*2);
}
public void area(){
System.out.println("面积为:"+length*width);
}

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("请输入长方形的长:");
Rectangle n=new Rectangle();
n.length=sc.nextInt();
System.out.print("请输入长方形的宽:");
n.width=sc.nextInt();
n.show();
n.perimeter();
n.area();
}
}

3.创建一个名为Fan的类来模拟风扇,该类包含属性speedonradius,有合适
的构造方法,此外还要求提供一个方法用来设置风扇的速度和开关状态等信息。另外编写一个类来测试Fan的使用。 
package com.jredu.ch07;

import java.util.Scanner;
public class Fan {
public double speed;
public String on;
public double radius;
Scanner sc=new Scanner(System.in);
public void set(){
System.out.print("风扇的状态(on/off):");
on=sc.nextLine();
if(on.equals("on")){
System.out.print("请输入转速:");
speed=sc.nextDouble();
System.out.print("请输入半径:");
radius=sc.nextDouble();
System.out.println("风扇处于开的状态\n转速为:"+speed+"转每秒\n风扇半径为:"+radius+"cm");
}else{
System.out.print("风扇处于停的状态");
return;
}
}
public static void main(String[] args) {
Fan a=new Fan();
a.set();
}
}
 

4.创建一个名为Dog的类,它包含2String:namesays。在main()方法中,创建两个Dog对象,一个名为spot(它的叫声为”Ruff!),另一个名为scruffy(它的叫声为”wurf!)。然后显示它们的名字和叫声。  
 package com.jredu.ch07;
public class Dog1 {
public String name;
public String says;
public Dog1(String name, String says) {
super();
this.name = name;
this.says = says;
}
public void show(){
System.out.println("名字:"+name+"\t叫声:"+says);
}
public static void main(String[] args) {
Dog1 a=new Dog1("spot", "Ruff!");
Dog1 b=new Dog1("scruffy", "wurf!");
a.show();
b.show();
}
}

5.设计一个银行账户类,其中包括:账户信息:账号、姓名、开户时间、身份证号码、账户上的金额等成员。有:存款方法、取款方法、显示开户时间的方法、获得账上的金额的方法等。并编写测试类。  
 package com.jredu.ch07;

import java.util.Scanner;
public class Account {
public int id;
public String name;
public int time;
public String idcard;
public int money;
public Account(int id, String name, int time, String idcard, int money) {
super();
this.id = id;
this.name = name;
this.time = time;
this.idcard = idcard;
this.money = money;
}
Scanner sc=new Scanner(System.in);
public void save(){
System.out.println("请输入存入的金额:");
int num=sc.nextInt();
money+=num;
System.out.println("账上的金额:"+money);
}
public void take(){
System.out.println("请输入取出的金额:");
int num=sc.nextInt();
money-=num;
System.out.println("账上的金额:"+money);
}
public void showmoney(){
System.out.println("账上的金额:"+money);
}
public void showtime(){
System.out.println("开户时间:"+time);
}
public static void main(String[] args) {
Account a=new Account(23, "张三", 20170801, "142322199504178888", 6666666);
System.out.println("账号:"+a.id+" 姓名:"+a.name);
a.showmoney();
a.showtime();
a.save();
a.take();
}
}

6.设计一个简单的计算器,输入两个数和一个四则运算符,计算后输出相应的运算结果  
package com.jredu.ch07;
import java.util.Scanner;
public class JiSuQi {
public double num1;
public String a;
public double num2;
public void Result(){
switch(a){
case "+":
System.out.println(num1+num2);
break;
case "-":
System.out.println(num1-num2);
break;
case "*":
System.out.println(num1*num2);
break;
case "/":
System.out.println(num1/num2);
break;
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
JiSuQi b= new JiSuQi();
System.out.print("请输入第一个数:");
b.num1=sc.nextDouble();
System.out.print("请输入运算符:");
b.a=sc.next();
System.out.print("请输入第二个数:");
b.num2=sc.nextDouble();
b.Result();
}
}

7. 构造一个类来描述屏幕上的一个点,该类的构成包括点的xy两个坐标,以及一些对点进行的操作,包括:取得点的坐标值,对点的坐标进行赋值,编写应用程序生成该类的对象并对其进行操作。
package com.jredu.ch07;

import java.util.Scanner;

public class Point {
public double x;
public double y;
Scanner sc=new Scanner(System.in);
public Point(){
}
public Point(double x, double y) {
super();
this.x = x;
this.y = y;
}
public void show(){
System.out.println("该点的坐标为:("+x+","+y+")");
}
public void set(){
System.out.print("请输入该点的横坐标:");
x=sc.nextDouble();
System.out.print("请输入该点的纵坐标:");
y=sc.nextDouble();
}
public static void main(String[] args) {
Point p=new Point();
p.set();
p.show();
}
}  

8.练习题目:
使用面向对象的思想描述房源信息
问题描述:
某公司要开发“房购房网”,请使用面向对象的思想,设计
地区类和房屋类描述房源信息。编写测试类,构造地区对象和房屋对象,
并调用相关方法(测试数据信息自定)
其中设定
地区类
属性:地区编号,地区名称
要求:设置带参构造函数实现属性赋值
房屋类
属性:房屋编号,房屋类型(一居室、两居室等等),所在区域,
房屋价格
方法:房源展示的方法,介绍房源的详细信息。
要求:
1、设置带参构造函数实现属性赋值
2、房源展示信息包括房屋编号,类型,价格和所在区域信息
地区类:
package com.jredu.ch08;

public class Local {
public int lid;
public String lname;
public Local(int lid, String lname) {
super();
this.lid = lid;
this.lname = lname;
}
}
房屋类:
package com.jredu.ch08;

public class House {
public int hid;
public String type;
public int price;
public Local local;
public House(int hid, String type, int price,Local local) {
super();
this.hid = hid;
this.type = type;
this.price = price;
this.local=local;
}
public void show(){
System.out.println("房屋编号:"+hid+"\n房屋类型:"+type+"\n房屋价格:"+price+"\n所在区域:"+local.lname+",该地区的编号是"+local.lid);
}
public static void main(String[] args) {
House a=new House(001,"三室一厅",8000,new Local(1, "烟台莱山区191号"));
a.show();
}
}

9.输入三门成绩,然后计算总成绩和平均成绩
package com.jredu.ch08;

import java.util.Scanner;

public class ScoreCalc {
public int sc1;
public int sc2;
public int sc3;
public void sum(){
System.out.println("总成绩是:"+(sc1+sc2+sc3));
}
public void avg(){
System.out.println("平均成绩是:"+(sc1+sc2+sc3)/3);
}
public static void main(String[] args) {
ScoreCalc a=new ScoreCalc();
Scanner sc=new Scanner(System.in);
System.out.print("请输入Java成绩:");
a.sc1=sc.nextInt();
System.out.print("请输入C#成绩:");
a.sc2=sc.nextInt();
System.out.print("请输入DB成绩:");
a.sc3=sc.nextInt();
a.sum();
a.avg();
}
}

原创粉丝点击