面向对象程序设计-实验1_类和对象

74 篇文章 12 订阅
1 篇文章 0 订阅

做题前需了解:

java中如何输入变量(数字,字符,浮点型)

Java多组输入实现

A - C/C++训练1—最大公约数与最小公倍数

Description

输入两个正整数,求它们的最大公约数与最小公倍数。

Input

输入两个正整数,两个整数之间用空格分开。

数据保证在 int 范围内。

Output

第一行输出最大公约数;
第二行输出最小公倍数。

答案保证在 int 范围内。

Sample
Input

64 48

Output

16
192

答案:

import java.util.Scanner;

public class Main {
	
	public static int gcd(int a,int b) {
		int k=0;
		while(b!=0) {
			k=a%b;
			a=b;
			b=k;
		}
		return a;
	}
	
	public static int lcm(int a,int b) {
		return a*b/gcd(a,b);
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int n=sc.nextInt();
		int m=sc.nextInt();
		System.out.println(gcd(n,m));
		System.out.println(lcm(n,m));
	}

}

B - C/C++经典程序训练3—模拟计算器

Description

简单计算器模拟:输入两个整数和一个运算符,输出运算结果。

Input

第一行输入两个整数,用空格分开;
第二行输入一个运算符(+、-、*、/)。
所有运算均为整数运算,保证除数不包含0。

Output

输出对两个数运算后的结果。

Sample
Input

30 50
*

Output

1500

答案:

import java.util.Scanner;

public class Main {
	
	public static int gcd(int a,int b) {
		int k=0;
		while(b!=0) {
			k=a%b;
			a=b;
			b=k;
		}
		return a;
	}
	
	public static int lcm(int a,int b) {
		return a*b/gcd(a,b);
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int n=sc.nextInt();
		int m=sc.nextInt();
		String c;
		c=sc.next();
		char s;
		s=c.charAt(0);
		if(s=='+')
		System.out.println(n+m);
		if(s=='-')
			System.out.println(n-m);
		if(s=='*')
			System.out.println(n*m);
		if(s=='/')
			System.out.println(n/m);
	}

}

C - 手机键盘

Description

大家应该都见过那种九键的手机键盘,键盘上各字母的分布如下图所示。

在这里插入图片描述

当我们用这种键盘输入字母的时候,对于有些字母,往往会需要按多次键才能输入。

比如:a, b, c 都在“2”键上,输入 a 只需要按一次,而输入 c 需要连续按三次。

连续输入多个字母的规则如下:

  1. 如果前后两个字母不在同一个按键上,则可在输入前一个字母之后直接输入下一个字母,如:ad 需要按两次键盘,kz 需要按 6 次。

  2. 如果前后两个字母在同一个按键上,则输入完前一个字母之后需要等待一段时间才能输入下一个字母,如 ac,在输入完 a
    之后,需要等一会儿才能输入 c。

现在假设每按一次键盘需要花费一个时间段,等待时间需要花费两个时间段。

现在给出一串只包含小写英文字母的字符串,计算出输入它所需要花费的时间。

Input

输入包含多组测试数据,对于每组测试数据:

输入为一行只包含小写字母的字符串,字符串长度不超过100。

Output

对于每组测试数据,输出需要花费的时间。

Sample

Input

bob
www

Output

7
7

答案:

import java.util.Scanner;

public class Main {

	static int pos(char a) {
		if (a >= 'a' && a <= 'c')
			return 2;
		else if (a >= 'd' && a <= 'f')
			return 3;
		else if (a >= 'g' && a <= 'i')
			return 4;
		else if (a >= 'j' && a <= 'l')
			return 5;
		else if (a >= 'm' && a <= 'o')
			return 6;
		else if (a >= 'p' && a <= 's')
			return 7;
		else if (a >= 't' && a <= 'v')
			return 8;
		else
			return 9;
	}

	static int get_Time(char a) {
		int flag = 0;
		if (a == 'a' || a == 'd' || a == 'g' || a == 'm' || a == 'p' || a == 't' || a == 'w' || a == 'j')
			flag = 1;
		else if (a == 'b' || a == 'e' || a == 'h' || a == 'k' || a == 'n' || a == 'q' || a == 'u' || a == 'x')
			flag = 2;
		else if (a == 'c' || a == 'f' || a == 'i' || a == 'l' || a == 'o' || a == 'r' || a == 'v' || a == 'y')
			flag = 3;
		else if (a == 's' || a == 'z')
			flag = 4;
		return flag;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner cin = new Scanner(System.in);
		while (cin.hasNext()) {
			String str = cin.nextLine();
			char[] a = str.toCharArray();
			int sum = 0;
			for (int i = 0; i < str.length(); i++) {
				sum += get_Time(a[i]);
			}
			for (int i = 1; i < str.length(); i++) {
				if (pos(a[i]) == pos(a[i - 1])) {
					sum += 2;
				}
			}
			System.out.println(sum);
		}
	}

}

D - 3-1 Point类的构造函数

Description

通过本题目的练习可以掌握类的构造函数的定义;

设计一个点类Point,私有数据成员有x、y;公有成员函数有:无参数的构造函数Point(),带参数的构造函数Point(int,int);ShowPoint()输出点对象的信息

在主函数main()中调用相应成员函数,从键盘接收时间对象的x和y的值,并向显示器输出相应的值。

Input

输入2个整数,用一个空格间隔

Output

要求先输出默认的点值,再输出用户构造的点的值

点的格式为:一对圆括号内 x,y的值,中间用“,”间隔;

Sample

Input

10 11

Output

(0,0)
(10,11)

答案:

import java.util.Scanner;

public class Main {
	
	public static class Point{
		int n;
		int m;
		
		void Point(int n,int m) {
			this.n=n;
			this.m=m;
		}
		
		void ShowPoint(int n,int m) {
			System.out.println("("+n+","+m+")");
		}
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		Point p=new Point();
		int n=sc.nextInt();
		int m=sc.nextInt();
//		String c;
//		c=sc.next();
		p.Point(n,m);
		final int a=0;
		final int b=0;
		System.out.println("("+a+","+b+")");
		p.ShowPoint(n,m);
	}

}

E - 区域内点的个数

Description

X晚上睡不着的时候不喜欢玩手机,也不喜欢打游戏,他喜欢数星星。

Input

多组输入。

每组先输入一个整数N(N <= 10000),接着输入两个点代表矩形的左下点B(x,y)和右上点T(x,y),然后输入N个(X,Y)代表N颗星星。问有多少颗星星在窗子内部,在窗边上的不计。

Output
输出一个整数,代表有多少颗星星在窗子内部。

Sample

Input

3
0 1
3 4
1 1
2 2
3 3
2
1 1
5 5
4 4
0 6

Output

1
1

答案:

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner cin = new Scanner(System.in);
		int n;
		while (cin.hasNextInt()) {
			n = cin.nextInt();
			int x1, y1;
			int x2, y2;
			x1 = cin.nextInt();
			y1 = cin.nextInt();
			x2 = cin.nextInt();
			y2 = cin.nextInt();
			int x, y;
			int cnt = 0;
			while (n > 0) {
				x = cin.nextInt();
				y = cin.nextInt();
				if (x > x1 && x < x2 && y > y1 && y < y2)
					cnt++;
				n--;
			}
			System.out.println(cnt);
		}
	}
}

F - 计算长方体、四棱锥的表面积和体积

Description

计算如下立体图形的表面积和体积。
在这里插入图片描述

从图中观察,可抽取其共同属性到父类Rect中:长度:l 宽度:h 高度:z

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

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

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

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

Input

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

每行三个数值,分别表示l h z

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

Output

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

Sample

Input

1 2 3
0 2 3
-1 2 3
3 4 5

Output

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

Hint
四棱锥体公式:V=1/3Sh,S——底面积 h——高
答案:

import java.util.Scanner;

class Rect {
	double l, h, z;

	Rect(double l, double h, double z) {
		this.l = l;
		this.h = h;
		this.z = z;
	}

	double length(double l, double h) {
		return 2 * (l + h);
	}

	double area(double l, double h) {
		return l * h;
	}
}

class Cubic extends Rect {
	Cubic(double l, double h, double z) {
		super(l, h, z);
	}

	double Get_V() {
		return l * h * z;
	}

	double area() {
		return 2 * (l * h + l * z + z * h);
	}
}

class Pyramid extends Rect {
	Pyramid(double l, double h, double z) {
		super(l, h, z);
	}

	double Get_V() {
		return (l * h * z) / 3;
	}

	double area() {
		double sum = l * h;
		double x, y;
		x = Math.sqrt(z * z + (h / 2) * (h / 2));
		y = Math.sqrt(z * z + (l / 2) * (l / 2));
		sum += x * l + y * h;
		return sum;
	}
}

public class Main {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner cin = new Scanner(System.in);
		double l, h, z;
		double v, s;
		while (cin.hasNext()) {
			l = cin.nextDouble();
			h = cin.nextDouble();
			z = cin.nextDouble();
			if (l <= 0 || h <= 0 || z <= 0) {
				System.out.println("0.00 0.00 0.00 0.00");
			} else {
				Cubic cub = new Cubic(l, h, z);
				v = cub.Get_V();
				s = cub.area();
				System.out.printf("%.2f %.2f ", s, v);
				Pyramid pyr = new Pyramid(l, h, z);
				v = pyr.Get_V();
				s = pyr.area();
				System.out.printf("%.2f %.2f", s, v);
				System.out.println();
			}
		}
	}

}

G - 计算各种图形的周长(接口与多态)

Description

定义接口Shape,定义求周长的方法length()。
定义如下类实现接口Shape的抽象方法:
(1)三角形类Triangle (2)长方形类Rectangle (3)圆形类Circle等。
定义测试类ShapeTest,用Shape接口定义变量shape,用其指向不同类形的对象,输出各种图形的周长。并为其他的Shape接口实现类提供良好的扩展性。

Input

输入多组数值型数据(double);
一行中若有1个数,表示圆的半径;
一行中若有2个数(中间用空格间隔),表示长方形的长度、宽度。
一行中若有3个数(中间用空格间隔),表示三角形的三边的长度。
若输入数据中有负数,则不表示任何图形,周长为0。

Output

行数与输入相对应,数值为根据每行输入数据求得的图形的周长(保留2位小数)。

Sample

Input

1
2 3
4 5 6
2
-2
-2 -3

Output

6.28
10.00
15.00
12.56
0.00
0.00

Hint
构造三角形时要判断给定的三边的长度是否能组成一个三角形,即符合两边之和大于第三边的规则; 计算圆周长时PI取3.14。
答案:

import java.util.Scanner;

class Circle { // 定义圆的类
	double r;

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

	public double len() {
		if (r <= 0)
			return 0;
		return 2 * r * 3.14;
	}
}

class Rectangle { // 定义长方形的类
	double x, y;

	public Rectangle(double x, double y) {
		this.x = x;
		this.y = y;
	}

	public double len() {
		if (x <= 0 || y <= 0)
			return 0;
		return 2 * (x + y);
	}
}

class Triangle { // 定义三角形的类
	double a, b, c;

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

	public double len() {
		if (a <= 0 || b <= 0 || c <= 0)
			return 0;
		if (a + b <= c || a + c <= b || b + c <= a)
			return 0;
		return a + b + c;
	}
}

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner cin = new Scanner(System.in);
		while (cin.hasNext()) {
			String s = cin.nextLine();
			int i;
			double x, y;
			double a, b, c;
			for (i = 0; i < s.length(); i++) {
				if (s.charAt(i) == ' ')
					break;
			}
			if (i >= s.length()) {
				Circle cir = new Circle(Double.valueOf(s.toString()));
				System.out.printf("%.2f\n", cir.len());
			} else {
				String s1[] = s.split(" ");
				if (s1.length == 2) {
					x = Double.valueOf(s1[0].toString());
					y = Double.valueOf(s1[1].toString());
					Rectangle rec = new Rectangle(x, y);
					System.out.printf("%.2f\n", rec.len());
				} else if (s1.length == 3) {
					a = Double.valueOf(s1[0].toString());
					b = Double.valueOf(s1[1].toString());
					c = Double.valueOf(s1[2].toString());
					Triangle tri = new Triangle(a, b, c);
					System.out.printf("%.2f\n", tri.len());
				}
			}
		}
	}
}

H - 计算长方形的周长和面积(类和对象)

Description

设计一个长方形类Rect,计算长方形的周长与面积。
成员变量:整型、私有的数据成员length(长)、width(宽);
构造方法如下:

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

成员方法:包含求面积和周长。(可适当添加其他方法)
要求:编写主函数,对Rect类进行测试,输出每个长方形的长、宽、周长和面积。

Input

输入多组数据;
一行中若有1个整数,表示正方形的边长;
一行中若有2个整数(中间用空格间隔),表示长方形的长度、宽度。
若输入数据中有负数,则不表示任何图形,长、宽均为0。

Output

每行测试数据对应一行输出,格式为:(数据之间有1个空格)
长度 宽度 周长 面积

Sample

Input

1
2 3
4 5
2
-2
-2 -3

Output

1 1 4 1
2 3 10 6
4 5 18 20
2 2 8 4
0 0 0 0
0 0 0 0

答案:

import java.util.Scanner;

class Rect {
	public int n;
	public int m;

	Rect(int n) {
		// this.n = n;
		System.out.println(n + " " + n + " " + 4 * n + " " + n * n);
	}

	Rect(int n, int m) {
		// this.n = n;
		// this.m = m;
		System.out.println(n + " " + m + " " + 2 * (n + m) + " " + m * n);
	}
}

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner cin = new Scanner(System.in);
		while (cin.hasNext()) {
			String s = cin.nextLine();
			char[] str = s.toCharArray();
			int flag = 0;
			int i;
			for (i = 0; i < str.length; i++) {
				if (str[i] == ' ')
					flag = 1;
			}
			if (flag == 0) {
				int x = Integer.parseInt(s);
				if (x <= 0)
					System.out.println("0 0 0 0");
				else {
					Rect rec = new Rect(x);
				}
			} else {
				String[] y = s.split(" ");
				int a = Integer.parseInt(y[0]);
				int b = Integer.parseInt(y[1]);
				if (a <= 0 || b <= 0)
					System.out.println("0 0 0 0");
				else {
					Rect rec = new Rect(a, b);
				}
			}
		}
	}
}

I - 答答租车系统(面向对象综合练习)

Description

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

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

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

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

序号     名称            载客量        载货量        租金
                        (人)        (吨)      (元/天)
  1          A            5                        800
  2          B            5                        400
  3          C            5                        800
  4          D            51                       1300
  5          E            55                       1500
  6          F            5           0.45         500
  7          G            5            2.0         450
  8          H                          3          200
  9          I                          25         1500
 10          J                          35         2000

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

Input

首行是一个整数:代表要不要租车 1——要租车(程序继续),0——不租车(程序结束);
第二行是一个整数,代表要租车的数量N;

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

Output

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

若不租车,则输出: 0 0.00 0(含义同上)

Sample

Input

1
2
1 1
2 2

Output

15 0.00 1600

答案:

import java.util.Scanner;

abstract class Vehicle {  //定义汽车父类
	//String name;  //车辆名称
	int price;  //租金

	public Vehicle(int price) {
		this.price = price;
	}

	abstract int get_Price();  //获取租金
	abstract int get_Num();   //获取载客量
	abstract double get_Weight();  //获取载货量
}

class Bus extends Vehicle {  //定义客车子类
	int num;  //载客量

	public Bus(int price, int num) {
		super(price);
		this.num = num;
	}

	int get_Price() {
		return price;
	}

	int get_Num() {
		return num;
	}

	double get_Weight() {
		return 0;
	}

}

class Van extends Vehicle {  //定义货车子类
	double weight;  //载货量

	public Van(int price, double weight) {
		super(price);
		this.weight = weight;
	}

	int get_Price() {
		return price;
	}

	int get_Num() {
		return 0;
	}

	double get_Weight() {
		return weight;
	}

}

class Pika extends Vehicle {  //定义皮卡车子类
	int num;
	double weight;

	public Pika(int price, int num, double weight) {
		super(price);
		this.num = num;
		this.weight = weight;
	}

	int get_Price() {
		return price;
	}

	int get_Num() {
		return num;
	}

	double get_Weight() {
		return weight;
	}
}

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner cin = new Scanner(System.in);
		int t;
		while(cin.hasNext()) {
			t = cin.nextInt();
			if (t == 0) {
				System.out.println("0 0.00 0");
			} else if (t == 1) {
				//根据题意定义车辆
				Vehicle[] Car = {
						new Bus(800, 5), 
						new Bus(400, 5), 
						new Bus(800, 5), 
						new Bus(1300, 51), 
						new Bus(1500, 55),
						new Pika(500, 5, 0.45),
						new Pika(450, 5, 2.0), 
						new Van(200, 3), 
						new Van(1500, 25),
						new Van(2000, 35) 
				};
				int q;
				int n;
				int m;
				int num = 0;
				int price = 0;
				double weight = 0;
				q = cin.nextInt();
				while (q-- > 0) {
					m = cin.nextInt();
					n = cin.nextInt();
					weight += (Car[m - 1].get_Weight() * n);
					num += (Car[m - 1].get_Num() * n);
					price += (Car[m - 1].get_Price() * n);
				}
				System.out.printf("%d %.2f %d", num, weight, price);
			}
		}
	}
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值