Java编程练习5

7-1 sdut-oop-2 Shift Dot(类和对象)

题目
7-1 sdut-oop-2 Shift Dot(类和对象)
分数 10
作者 周雪芹
单位 山东理工大学
给出平面直角坐标系中的一点,并顺序给出n个向量,求该点根据给定的n个向量位移后的位置。
设计点类Point,内含:
(1)整型属性x和y,表示点的横坐标和纵坐标;
(2)带参构造方法,便于使用初始值构造该类对象;
(3)点移动的方法move(x1,y1),其 中x1和y1表示移动的向量,该方法返回移动后的新的点对象;
(4)展示点对象的字符串方法toString(),以“(横坐标,纵坐标)”的形式。
输入格式:
多组输入。
第一行是三个整数x,y,n,表示点的坐标(x,y),和向量的个数n。
接下来n行,每行两个数xi,yi,表示第i个向量。
题目中所有数据不会超出整型范围。
输出格式:
对于每组输入,输出一行,形式为:“(x,y)”,表示点的最终点位置的横坐标和纵坐标。

输入样例:
0 0 1
2 3
0 0 2
1 2
2 3
输出样例:
(2,3)
(3,5)
正确答案
1

import java.io.*;
import java.util.*;
class Point{
    int x,y;
    Point(int x,int y){
        this.x=x;
        this.y=y;
    }
    void move(int x1,int y1){
        this.x+=x1;
        this.y+=y1;
    }
        public String toString(){
            return "("+this.x+","+this.y+")";
        }
}
public class Main{
    public static void main(String[] args){
        try{
                BufferedReader br=new BufferedReader(new InputStreamReader
                (System.in));
            while(true){
                String str=br.readLine();
                String[] data=str.split(" ");
                int x=Integer.parseInt(data[0]);
                int y=Integer.parseInt(data[1]);
                int n=Integer.parseInt(data[2]);
                Point point=new Point(x,y);
                for(int i=0;i<n;i++){
                    String str1=br.readLine();
                    String[] data1=str1.split(" ");
                    int x1=Integer.parseInt(data1[0]);
                    int y1=Integer.parseInt(data1[1]);
                    point.move(x1,y1);
                }
                System.out.println(point.toString());
            }
            
        }catch(Exception e){
            
        }
              
    }
}

2

import java.util.Scanner;
class Point {
	private int x;
	private int y;
	Point(int x,int y){
		this.x=x;
		this.y=y;
	}
	void move(int x1,int y1){
        this.x+=x1;
        this.y+=y1;
    }
	public String toString(){
        return "("+this.x+","+this.y+")";
    }
}

public class Main {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Scanner in = new Scanner(System.in);
		Point a=null;
		while(in.hasNext()) {
			int x =in.nextInt();
			int y =in.nextInt();
			a=new Point(x,y);
			int n=in.nextInt();
			for(int i=0;i<n;i++) {
				int x1=in.nextInt();
				int y1=in.nextInt();
				a.move(x1, y1);
			}
			System.out.println(a.toString());
		}
	}
}

7-2 sdut-oop-3 输出人的信息(程序改错

题目
下述代码有错,请参照程序的输出修改程序。

public class Main {
public static void main(String[] args) {
Person[] p = new Person[3];
p[0].name = “zhangsan”;
p[0].age = 18;
p[1].name = “lisi”;
p[1].age = 20;
p[2].name = “wangwu”;
p[2].age = 22;
for (int i = 0; i < p.length; i++) {
System.out.println(p[i]);
}
}
}

class Person {
private String name;
private int age;

public Person(String name, int age) {
    super();
    this.name = name;
    this.age = age;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

}
输入格式:
无输入。

输出格式:
Person类对象的属性name和age,中间用1个空格分隔。

输入样例:

输出样例:
zhangsan 18
lisi 20
wangwu 22
正确答案

public class Main {
    public static void main(String[] args) {
        Person[] p = new Person[3];
        p[0]=new Person("zhangsan",18);
        p[1]=new Person("lisi",20);
        p[2]=new Person("wangwu",22);
        for (int i = 0; i < p.length; i++) {
            System.out.println(p[i].toString());
        }
    }
}

class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    public String toString(){
        return this.name+" "+this.age;
    }
}

7-3 sdut-oop-4-求圆的面积(类与对象)

题目
按要求完成程序的编写。
1、定义圆类Circle,其中包括:
(1)成员变量:private int radius
(2)无参构造方法 ,给radius赋值为2,并输出信息:“This is a constructor with no para.”;
(2)有参构造方法 ,接收用户给的radius赋值,并输出"This is a constructor with para."(如果给的半径小于等于0,则赋默认值为2);
(3)为radius添加set方法,接收用户输入的半径,如果用户输入半径为<=0,则让半径的值为2;
(4)为radius半径添加get方法,返回用户输入的半径;
(5)求圆面积方法getArea(), π使用Math.PI代替;
(6)定义toString方法,public String toString( )方法体为:
return “Circle [radius=” + radius + “]”;

2、定义Main类,在main方法中,完成下列操作:
(1)定义并创建Circle类对象c1,输出c1对象,求c1的面积并输出;
(2)定义并创建Circle类对象c2,输出c2对象,求c2的面积并输出;
(3)从键盘接收整数半径,赋值给c2的半径,输出c2对象,求c2的面积并输出;
(4)从键盘接收整数半径,用有参构造方法创建Circle类对象c3,输出c3对象,求c3的面积后输出。
输入格式:
输入有多行。每行包括一个整数的圆的半径。

输出格式:
按照题目要求,输出圆构造方法的输出信息、圆对象的字符中描述信息、及圆的面积(其中圆的面积保留2位小数)。上述信息各占一行。

输入样例:
4
5
输出样例:
This is a constructor with no para.
Circle [radius=2]
12.57
This is a constructor with no para.
Circle [radius=2]
12.57
Circle [radius=4]
50.27
This is a constructor with para.
Circle [radius=5]
78.54
正确答案

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
    	Scanner in = new Scanner(System.in);
        Circle c1=new Circle();
        System.out.println(c1.toString());
        System.out.printf("%.2f\n",c1.getArea());
        Circle c2=new Circle();
        System.out.println(c2.toString());
        System.out.printf("%.2f\n",c2.getArea());
        c2.set(in.nextInt());
        System.out.println(c2.toString());
        System.out.printf("%.2f\n",c2.getArea());
        Circle c3=new Circle(in.nextInt());
        System.out.println(c3.toString());
        System.out.printf("%.2f",c3.getArea());
    }
}

class Circle {
	private int radius;
	
	Circle(){
		this.radius=2;
		System.out.println("This is a constructor with no para.");
	}
	Circle(int radius){
		this.radius=radius>0?radius:2;
		System.out.println("This is a constructor with para.");
	}
	public void set(int radius) {
		this.radius=radius>0?radius:2;
	}
	public int get() {
		return radius;
	}
	public double getArea() {
		return Math.PI*radius*radius;
	}
	public String toString() {
		return "Circle [radius=" + radius + "]";
	}
}

7-4 sdut-oop-5 计算长方体和四棱锥的表面积和体积(类的继承)

题目
计算如下立体图形的表面积和体积。

008.jpg

从图中观察,可抽取长方体和四棱锥两种立体图形的共同属性到父类Rect中:长度:l 宽度:h 高度:z。

编程要求:

(1)在父类Rect中,定义求底面周长的方法length( )和底面积的方法area( )。

(2)定义父类Rect的子类立方体类Cubic,计算立方体的表面积和体积。其中表面积area( )重写父类的方法。

(3)定义父类Rect的子类四棱锥类Pyramid,计算四棱锥的表面积和体积。其中表面积area( )重写父类的方法。

(4)在主程序中,输入立体图形的长(l)、宽(h)、高(z)数据,分别输出长方体的表面积、体积、四棱锥的表面积和体积。

提示:

(1)四棱锥体积公式:V=
3
1

Sh,S——底面积 h——高

(2)在Java中,利用Math.sqrt(a)方法可以求得a的平方根(方法的参数及返回结果均为double数据类型)

(3)在Python中,利用math模块的sqrt(a)方法,求得a的平方根。

输入格式:
输入多行数值型数据(double);

每行三个数值,分别表示l、h、z,数值之间用空格分隔。

若输入数据中有0或负数,则不表示任何图形,表面积和体积均为0。

输出格式:
行数与输入相对应,数值为长方体表面积 长方体体积 四棱锥表面积 四棱锥体积(中间有一个空格作为间隔,数值保留两位小数)。

输入样例:
1 2 3
0 2 3
-1 2 3
3 4 5
输出样例:
22.00 6.00 11.25 2.00
0.00 0.00 0.00 0.00
0.00 0.00 0.00 0.00
94.00 60.00 49.04 20.00
注意
split()中的运算符需要转义
正确答案

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
    	Scanner in = new Scanner(System.in);
        while(in.hasNext()) {
        	double l=in.nextDouble();
        	double h=in.nextDouble();
        	double z=in.nextDouble();
        	Cubic cubic=new Cubic(l,h,z);
        	Pyramid pyramid=new Pyramid(l,h,z);
        	System.out.printf("%.2f %.2f %.2f %.2f\n",cubic.area(),cubic.ti(),pyramid.area(),pyramid.ti());
        }
    }
}

class Rect {
	double l;	
	double h;
	double z;
	
	public Rect(double l, double h, double z) {
		super();
		this.l = l;
		this.h = h;
		this.z = z;
	}
	Rect(){
		
	}
	double length(){
		return (l+h)*2;
	}
	double area() {
		return l*h;
	}
}
class Cubic extends Rect {
	Cubic(double i,double h,double z){
		super(i,h,z);
	}
	
	@Override
	double area() {
		// TODO 自动生成的方法存根
		return (l<=0||h<=0||z<=0)?0:super.area()*2+l*z*2+h*z*2;
	}
	double ti(){
		return (l<=0||h<=0||z<=0)?0:l*z*h;
	}
}
class Pyramid extends Rect {
	Pyramid(double i,double h,double z){
		super(i,h,z);
	}
	
	@Override
	double area() {
		// TODO 自动生成的方法存根
		return (l<=0||h<=0||z<=0)?0:Math.sqrt(z*z+h/2*h/2)*l+Math.sqrt(z*z+l/2*l/2)*h+super.area();
	}
	
	double ti(){
		return (l<=0||h<=0||z<=0)?0:super.area()*z/3;
	}
	
}

7-5 sdut-oop-6 计算各种图形的周长(多态)

题目
定义接口或类 Shape,定义求周长的方法length()。

定义如下类,实现接口Shape或父类Shape的方法。

(1)三角形类Triangle (2)长方形类Rectangle (3)圆形类Circle等。

定义测试类ShapeTest,用Shape接口(或类)定义变量shape,用其指向不同类形的对象,输出各种图形的周长。并为其他的Shape接口实现类提供良好的扩展性。

提示: 计算圆周长时PI取3.14。

输入格式:
输入多组数值型数据(double);

一行中若有1个数,表示圆的半径;

一行中若有2个数(中间用空格间隔),表示长方形的长度、宽度。

一行中若有3个数(中间用空格间隔),表示三角形的三边的长度。(需要判断三个边长是否能构成三角形)

若输入数据中有0或负数,则不表示任何图形,周长为0。

输出格式:
行数与输入相对应,数值为根据每行输入数据求得的图形的周长。

输入样例:
在这里给出一组输入。例如:

1
2 3
4 5 6
2
-2
-2 -3
输出样例:
在这里给出相应的输出。例如:

6.28
10.00
15.00
12.56
0.00
0.00
正确答案

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
    	Scanner in = new Scanner(System.in);
        while(in.hasNext()) {
        	String[] s=in.nextLine().split(" ");
        	if(s.length==3) {
        		Triangle triangle=new Triangle(Double.valueOf(s[0]),Double.valueOf(s[1]),Double.valueOf(s[2]));
        		System.out.printf("%.2f\n",triangle.length());
        	}else if(s.length==2) {
        		Rectangle rectangle=new Rectangle(Double.valueOf(s[0]),Double.valueOf(s[1]));
        		System.out.printf("%.2f\n",rectangle.length());
        	}else {
        		Circle circle=new Circle(Double.valueOf(s[0]));
        		System.out.printf("%.2f\n",circle.length());
        	}
        }
    }
}

interface Shape {
    public double length();
}

class Triangle implements Shape {
    private double a;
    private double b;
    private double c;

    public Triangle(double a, double b, double c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    public double length() {
        return (a<=0||b<=0||c<=0||a+b<=c||a+c<=b||b+c<=a)?0:a + b + c;
    }
}

class Rectangle implements Shape {
    private double l;
    private double h;

    public Rectangle(double l, double h) {
            this.l = l;
            this.h = h;
    }

    public double length() {
        return (l<=0||h<=0)?0:2 * (l + h);
    }
}

class Circle implements Shape {
    double r;

    public Circle(double r) {
        this.r = r;
    }

    public double length() {
        return r<=0?0:3.14 * 2 * r;
    }
}

7-6 sdut-oop-7 答答租车系统(类的继承与多态 面向对象综合练习)

题目
各位面向对象的小伙伴们,在学习了面向对象的核心概念——类的封装、继承、多态之后,答答租车系统开始营运了。

请你充分利用面向对象思想,为公司解决智能租车问题,根据客户选定的车型和租车天数,来计算租车费用,最大载客人数,最大载载重量。

公司现有三种车型(客车、皮卡车、货车),每种车都有名称和租金的属性;其中:客车只能载人,货车只能载货,皮卡车是客货两用车,即可以载人,也可以载货。

下面是答答租车公司的可用车型、容量及价目表:

007.jpg

要求:根据客户输入的所租车型的序号及天数,计算所能乘载的总人数、货物总数量及租车费用总金额。

输入格式:
首行是一个整数:代表要不要租车 1——要租车(程序继续),0——不租车(程序结束);

第二行是一个整数,代表要租车的数量N;

接下来是N行数据,每行2个整数m和n,其中:m表示要租车的编号,n表示租用该车型的天数。

输出格式:
若成功租车,则输出一行数据,数据间有一个空格,含义为:
载客总人数 载货总重量(保留2位小数) 租车金额(整数)
若不租车,则输出:
0 0.00 0(含义同上)

输入样例:
1
2
1 1
2 2
输出样例:
在这里给出相应的输出。例如:

15 0.00 1600
注意
用instanceof判断左边是否为右边的实例化对象
用Car作为父类定义一个数组来存储各个子类从而存储各个车型的信息
正确答案

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Car[] carsForRent = { new PassengerCar("a", 800, 5), new PassengerCar("b", 400, 5),
				new PassengerCar("c", 800, 5), new PassengerCar("d", 1300, 51), new PassengerCar("e", 1500, 55),
				new PickUp("f", 500, 5, 0.45), new PickUp("g", 450, 5, 2.0), new Trunk("h", 200, 3),
				new Trunk("i", 1500, 25), new Trunk("j", 2000, 35) };

		Scanner scan = new Scanner(System.in);
		int input = scan.nextInt();

		int totalPeopley = 0; // 载客总人数
		double totalCargo = 0; // 载货总重量
		int totalMoney = 0; // 租金总额

		if (input == 1) {

			int rentNum = scan.nextInt();
			int[] carsId = new int[rentNum]; // 租车Id
			int[] days = new int[rentNum]; // 租车天数

			for (int j = 0; j < rentNum; j++) {
				carsId[j] = scan.nextInt();
				days[j] = scan.nextInt();
			}

			for (int j = 0; j < rentNum; j++) {
				totalMoney += carsForRent[carsId[j] - 1].getRent() * days[j];
				if (carsForRent[carsId[j] - 1] instanceof PassengerCar) {
					totalPeopley += ((PassengerCar) carsForRent[carsId[j] - 1]).getPeopleCapacity() * days[j];
				}
				if (carsForRent[carsId[j] - 1] instanceof PickUp) {
					totalPeopley += ((PickUp) carsForRent[carsId[j] - 1]).getPeopleCapacity() * days[j];
					totalCargo += ((PickUp) carsForRent[carsId[j] - 1]).getCargoCapacity() * days[j];
				}
				if (carsForRent[carsId[j] - 1] instanceof Trunk) {
					totalCargo += ((Trunk) carsForRent[carsId[j] - 1]).getCargoCapacity() * days[j];
				}
			}
		}
		System.out.println(totalPeopley + " " + String.format("%.2f", totalCargo) + " " + totalMoney);
		scan.close();
	}
}


class Car {
    protected String name;
    protected int rent;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getRent() {
        return rent;
    }
    public void setRent(int rent) {
        this.rent = rent;
    }
 
}


 class PassengerCar extends Car {
    private double peopleCapacity;
    public PassengerCar(String name,int rent,double peoplecapacity){
        this.name=name;
        this.rent=rent;
        this.peopleCapacity=peoplecapacity;
    }
    public double getPeopleCapacity() {
        return peopleCapacity;
    }
 
    public void setPeopleCapacity(double peopleCapacity) {
        this.peopleCapacity = peopleCapacity;
    }
     
}


  class PickUp extends Car {
    private double cargoCapacity;
    private int peopleCapacity;
    public PickUp(String name,int rent,int peopleCapacity,double cargoCapacity){
        this.name=name;
        this.rent=rent;
        this.cargoCapacity=cargoCapacity;
        this.peopleCapacity=peopleCapacity;
    }
    public double getCargoCapacity() {
        return cargoCapacity;
    }
    public void setCargoCapacity(double cargoCapacity) {
        this.cargoCapacity = cargoCapacity;
    }
    public int getPeopleCapacity() {
        return peopleCapacity;
    }
    public void setPeopleCapacity(int peopleCapacity) {
        this.peopleCapacity = peopleCapacity;
    }
    
     
}


 class Trunk extends Car {
    private double cargoCapacity;
    public Trunk(String name,int rent,double cargoCapacity){
        this.name=name;
        this.rent=rent;
        this.cargoCapacity=cargoCapacity;
    }
    public double getCargoCapacity() {
        return cargoCapacity;
    }
 
    public void setCargoCapacity(double cargoCapacity) {
        this.cargoCapacity = cargoCapacity;
    }
     
}

 

7-7 sdut-oop-9 计算长方形的周长和面积(类和对象)

题目
设计一个长方形类Rect,计算长方形的周长与面积。

成员变量:整型、私有的数据成员length(长)、width(宽);

构造方法如下:

(1)Rect(int length) —— 1个整数表示正方形的边长

(2)Rect(int length, int width)——2个整数分别表示长方形长和宽

成员方法:包含求面积和周长。(可适当添加其他方法)

要求:编写主函数,对Rect类进行测试,输出每个长方形的长、宽、周长和面积。

输入格式:
输入多组数据;

一行中若有1个整数,表示正方形的边长;

一行中若有2个整数(中间用空格间隔),表示长方形的长度、宽度。

若输入数据中有负数,则不表示任何图形,长、宽均为0。

输出格式:
每行测试数据对应一行输出,格式为:

长度 宽度 周长 面积(数据之间有1个空格分隔)

输入样例:
在这里给出一组输入。例如:

1
2 3
4 5
2
-2
-2 -3
输出样例:
在这里给出相应的输出。例如:

1 1 4 1
2 3 10 6
4 5 18 20
2 2 8 4
0 0 0 0
0 0 0 0
注意
这里题目的描述有问题,若给出的数据有0时,长宽也应均为0。这里影响到了测试点2.
正确答案

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		while(in.hasNext()) {
			String[] s=in.nextLine().split(" ");
			Rect rect=null;
			if(s.length==1) {
				rect=new Rect(Integer.valueOf(s[0]));
			}else {
				rect=new Rect(Integer.valueOf(s[0]),Integer.valueOf(s[1]));
			}
			System.out.printf("%d %d %d %d\n",rect.getLength(),rect.getWidth(),rect.allLength(),rect.area());
		}
	}
}


class Rect {
	private int length;
	private int width;
	public int getLength() {
		return length;
	}
	public void setLength(int length) {
		this.length = length;
	}
	public int getWidth() {
		return width;
	}
	public void setWidth(int width) {
		this.width = width;
	}
	Rect(int length){
		if(length<=0) {
			this.length=0;
			this.width=0;
		}else {
			this.length=length;
			this.width=length;
		}
	}
	Rect(int length, int width){
		if(length<=0||width<=0) {
			this.length=0;
			this.width=0;	
		}else {
			this.length=length;
			this.width=width;
		}
	}
	int allLength(){
		return (length+width)*2;
	}
	int area() {
		return length*width;
	}
}
  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

漠–

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值