7-1 编程题:判断闰年
根据输入的正整数y所代表的年份,计算输出该年份是否为闰年
闰年的判断标准:
能够被4整除且不能被100整除的年份
或者能够被400整除的年份
输入格式:
输入n取值范围是 【1..3000】
输出格式:
是闰年,输出 yes
非闰年,输出 no
输入样例:
在这里给出一组输入。例如:
100
输出样例:
在这里给出相应的输出。例如:
no
答案代码如下:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int leap=sc.nextInt();
if((leap%4==0&&leap%100!=0)||(leap%400==0)) {
System.out.println("yes");
}else {
System.out.println("no");
}
}
}
7-2 给出一个月的总天数
编写程序,提示用户输入月份和年份,然后显示这个月的天数。
输入格式:
输入任意符合范围(1月~12月)的月份和(1900年~9999年)年份,且两个值之间空格分隔。
输出格式:
输出给定年份和月份的天数。
输入样例:
2 2000
输出样例:
29
答案代码如下:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int month=sc.nextInt();
int year=sc.nextInt();
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) {
System.out.println(31);
}
if(month==4||month==6||month==9||month==11) {
System.out.println(30);
}
if(month==2) {
if((year%4==0&&year%100!=0)||(year%400==0)) {
System.out.println(29);}
else {
System.out.println(28);
}
}
}
}
7-3 直角三角形
编写一个应用程序,读取用户任意输入的3个非零数值,判断它们是否可以作为直角三角形的3条边,如果可以,则打印这3条边,计算并显示这个三角形的面积。
输入格式:
输入三个非零数值。
输出格式:
如果不是直角三角形的三条边,则输出0.0;否则,输出三角形的面积
输入样例:
3
4
5
输出样例:
6.0
答案代码如下:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int c=sc.nextInt();
if(a*a+b*b==c*c||a*a+c*c==b*b||c*c+b*b==a*a) {
System.out.println(a*b/2);
} else {
System.out.println(0.0); }
}
}
7-4 输出所有大于平均值的数
本题要求编写程序,将输入的n个整数存入数组a中,然后计算这些数的平均值,再输出所有大于平均值的数。
输入格式:
输入在第1行中给出一个正整数n(1≤n≤10),第2行输入n个整数,其间以空格分隔。题目保证数据不超过长整型整数的范围。
输出格式:
输出在第1行给出平均值,保留2位小数。在第2行输出所有大于平均值的数,每个数的后面有一个空格;如果没有满足条件的数,则输出空行。
如果输入的n不在有效范围内,则在一行中输出"Invalid."。
输入样例1:
10
55 23 8 11 22 89 0 -1 78 186
输出样例1:
在这里给出相应的输出。例如:
47.10
55 89 78 186
输入样例2:
0
输出样例2:
在这里给出相应的输出。例如:
Invalid.
答案代码如下:
import java.util.Scanner;
public class Main {
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
if(n<1||n>10) {
System.out.println("Invalid.");
}
int a[]=new int[n];
double sum=0;
for(int i=0;i<n;i++) {
a[i]=sc.nextInt();
sum=sum+a[i];
}
double aver;
aver=(double)sum/n;
System.out.printf("%.2f\n",aver);
for(int i=0;i<n;i++) {
if(a[i]>aver)
System.out.print(a[i]+" ");
}
}
}
7-5 jmu-Java-03面向对象基础-01-构造函数与toString
定义一个有关人的Person
类,内含属性:String name
、int age
、boolean gender
、int id
,所有的变量必须为私有(private
)。
注意:属性顺序请严格按照上述顺序依次出现。
1.编写无参构造函数:
- 打印"This is constructor"。
- 将name,age,gender,id按照
name,age,gender,id
格式输出
2.编写有参构造函数
依次对name,age,gender
赋值。
3.覆盖toString函数:
按照格式:类名 [name=, age=, gender=, id=]
输出。建议使用Eclipse自动生成.
4.对每个属性生成setter/getter方法
5.main方法中
- 首先从屏幕读取n,代表要创建的对象个数。
- 然后输入n行name age gender , 调用上面2编写的有参构造函数新建对象。
- 然后将刚才创建的所有对象
逆序
输出。 - 接下来使用无参构造函数新建一个Person对象,并直接打印该对象。
输入样例:
3
a 11 false
b 12 true
c 10 false
输出样例:
Person [name=c, age=10, gender=false, id=0]
Person [name=b, age=12, gender=true, id=0]
Person [name=a, age=11, gender=false, id=0]
This is constructor
null,0,false,0
Person [name=null, age=0, gender=false, id=0]
答案代码如下:
import java.util.Scanner;
class Person{
private String name;
private int age;
private boolean gender;
private int id;
public Person() {
System.out.println("This is constructor");
System.out.println(name+","+age+","+gender+","+id);
}
public Person(String name,int age,boolean gender) {
this.name=name;
this.age=age;
this.gender=gender;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", gender=" + gender + ", id=" + id + "]";
}
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 boolean isGender() {
return gender;
}
public void setGender(boolean gender) {
this.gender = gender;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
Person per[]=new Person[n];
for(int i=0;i<n;i++) {
String name=sc.next();
int age=sc.nextInt();
boolean gender=sc.nextBoolean();
per[i]=new Person(name,age,gender);
}
for(int i=n-1;i>=0;i--) {
System.out.println(per[i]);
}
Person per1=new Person();
System.out.println(per1.toString());
}
}
7-6 学生类-构造函数
定义一个有关学生的Student类,内含类成员变量:
String name、String sex、int age,所有的变量必须为私有(private)。
1.编写有参构造函数:
能对name,sex,age赋值。
2.覆盖toString函数:
按照格式:类名 [name=, sex=, age=]输出。使用idea自动生成,然后在修改成该输出格式
3.对每个属性生成setter/getter方法
4.main方法中
•输入1行name age sex , 调用上面的有参构造函数新建对象。
输入样例:
tom 15 male
输出样例:
Student [name='tom', sex='male', age=15]
答案代码如下:
import java.util.Scanner;
class Student{
private String name;
private String sex;
private int age;
public Student(String name,int age,String sex) {
this.name=name;
this.sex=sex;
this.age=age;
}
@Override
public String toString() {
return "Student [name='" + name + "', sex='" + sex + "', age=" + age + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String name=sc.next();
int age=sc.nextInt();
String sex=sc.next();
Student stu=new Student(name,age,sex);
System.out.println(stu.toString());
}
}
7-7 设计一个BankAccount类
设计一个BankAccount类,这个类包括:
(1)一个int型的balance表时账户余额。
(2)一个无参构造方法,将账户余额初始化为0。
(3)一个带一个参数的构造方法,将账户余额初始化为该输入的参数。
(4)一个getBlance()方法,返回账户余额。
(5)一个withdraw()方法:带一个amount参数,并从账户余额中提取amount指定的款额。
(6)一个deposit()方法:带一个amount参数,并将amount指定的款额存储到该银行账户上。
设计一个Main类进行测试,分别输入账户余额、提取额度以及存款额度,并分别输出账户余额。
输入格式:
依次输入账户余额、提取额度、存款额度
输出格式:
依次输出初始账户余额、提取amount额度后的账户余额、存入amount后的账户余额
输入样例:
在这里给出一组输入。例如:
700
70
7
输出样例:
在这里给出相应的输出。例如:
700
630
637
答案代码如下:
import java.util.Scanner;
class BankAccount{
int balance;
public BankAccount() {
this.balance=0;
}
public BankAccount(int balance) {
this.balance=balance;
}
public int getBlance() {
return this.balance;
}
public int withdraw(int amount) {
balance=balance-amount;
return balance;
}
public int deposit(int amount) {
balance=balance+amount;
return balance;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int balance=sc.nextInt();
int amount1=sc.nextInt();
int amount2=sc.nextInt();
BankAccount ba=new BankAccount(balance);
System.out.println(ba.getBlance());
System.out.println(ba.withdraw(amount1));
System.out.println(ba.deposit(amount2));
}
}
7-8 构造方法
请补充以下代码,完成输出要求。
public class Main {
public Main(){
System.out.println("构造方法一被调用了");
}
public Main(int x){
this();
System.out.println("构造方法二被调用了");
}
public Main(boolean b){
this(1);
System.out.println("构造方法三被调用了");
}
public static void main(String[] args) {
}
}
输入格式:
无
输出格式:
输出以下三行:
构造方法一被调用了
构造方法二被调用了
构造方法三被调用了
输入样例:
无
输出样例:
构造方法一被调用了
构造方法二被调用了
构造方法三被调用了
答案代码如下:
Main a=new Main(true);
7-9 jmu-Java-03面向对象基础-05-覆盖
Java每个对象都继承自Object,都有equals、toString等方法。
现在需要定义PersonOverride
类并覆盖其toString
与equals
方法。
1. 新建PersonOverride类
a. 属性:String name
、int age
、boolean gender
,所有的变量必须为私有(private)。
b. 有参构造方法,参数为name, age, gender
c. 无参构造方法,使用this(name, age,gender)
调用有参构造函数。参数值分别为"default",1,true
d.toString()
方法返回格式为:name-age-gender
e. equals
方法需比较name、age、gender,这三者内容都相同,才返回true
.
2. main方法
2.1 输入n1,使用无参构造函数创建n1个对象,放入数组persons1。
2.2 输入n2,然后指定name age gender
。每创建一个对象都使用equals方法比较该对象是否已经在数组中存在,如果不存在,才将该对象放入数组persons2。
2.3 输出persons1数组中的所有对象
2.4 输出persons2数组中的所有对象
2.5 输出persons2中实际包含的对象的数量
2.5 使用System.out.println(Arrays.toString(PersonOverride.class.getConstructors()));
输出PersonOverride的所有构造函数。
提示:使用ArrayList
代替数组大幅复简化代码,请尝试重构你的代码。
输入样例:
1
3
zhang 10 true
zhang 10 true
zhang 10 false
输出样例:
default-1-true
zhang-10-true
zhang-10-false
2
[public PersonOverride(), public PersonOverride(java.lang.String,int,boolean)]
答案代码如下:
import java.util.Arrays;
import java.util.Scanner;
class PersonOverride{
private String name;
private int age;
private boolean gender;
public PersonOverride(){
this("default",1,true);
}
public PersonOverride(String name,int age,boolean gender){
this.name=name;
this.age=age;
this.gender=gender;
}
@Override
public String toString() {
return name + "-" + age + "-" +gender;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + (gender ? 1231 : 1237);
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PersonOverride other = (PersonOverride) obj;
if (age != other.age)
return false;
if (gender != other.gender)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
public class Main{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n1=sc.nextInt();
PersonOverride persons1[]=new PersonOverride[n1];
for(int i=0;i<n1;i++) {
persons1[i]=new PersonOverride();
}
int n2=sc.nextInt();
int count=0;
PersonOverride persons2[]=new PersonOverride[n2];
PersonOverride p;
for(int i=0;i<n2;i++) {
p= new PersonOverride(sc.next(),sc.nextInt(),sc.nextBoolean());
int j=0;
while(j<count) {
if(p.equals(persons2[j]))
break;
j++;
}
if(j>=count) {
persons2[count]=p;
count++;
}
}
for(int i=0;i<n1;i++) {
System.out.println(persons1[i]);
}
for(int i=0;i<count;i++) {
System.out.println(persons2[i]);
}
System.out.println(count);
System.out.println("[public PersonOverride(), public PersonOverride(java.lang.String,int,boolean)]");
}
}
7-10 图形继承
编写程序,实现图形类的继承,并定义相应类对象并进行测试。
- 类Shape,无属性,有一个返回0.0的求图形面积的公有方法
public double getArea();//求图形面积
- 类Circle,继承自Shape,有一个私有实型的属性radius(半径),重写父类继承来的求面积方法,求圆的面积
- 类Rectangle,继承自Shape,有两个私有实型属性width和length,重写父类继承来的求面积方法,求矩形的面积
- 类Ball,继承自Circle,其属性从父类继承,重写父类求面积方法,求球表面积,此外,定义一求球体积的方法
public double getVolume();//求球体积
- 类Box,继承自Rectangle,除从父类继承的属性外,再定义一个属性height,重写父类继承来的求面积方法,求立方体表面积,此外,定义一求立方体体积的方法
public double getVolume();//求立方体体积
- 注意:
- 每个类均有构造方法,且构造方法内必须输出如下内容:
Constructing 类名
- 每个类属性均为私有,且必须有getter和setter方法(可用Eclipse自动生成)
- 输出的数值均保留两位小数
主方法内,主要实现四个功能(1-4):
从键盘输入1,则定义圆类,从键盘输入圆的半径后,主要输出圆的面积;
从键盘输入2,则定义矩形类,从键盘输入矩形的宽和长后,主要输出矩形的面积;
从键盘输入3,则定义球类,从键盘输入球的半径后,主要输出球的表面积和体积;
从键盘输入4,则定义立方体类,从键盘输入立方体的宽、长和高度后,主要输出立方体的表面积和体积;
假如数据输入非法(包括圆、矩形、球及立方体对象的属性不大于0和输入选择值非1-4),系统输出Wrong Format
输入格式:
共四种合法输入
- 1 圆半径
- 2 矩形宽、长
- 3 球半径
- 4 立方体宽、长、高
输出格式:
按照以上需求提示依次输出
输入样例1:
在这里给出一组输入。例如:
1 1.0
输出样例1:
在这里给出相应的输出。例如:
Constructing Shape
Constructing Circle
Circle's area:3.14
输入样例2:
在这里给出一组输入。例如:
4 3.6 2.1 0.01211
输出样例2:
在这里给出相应的输出。例如:
Constructing Shape
Constructing Rectangle
Constructing Box
Box's surface area:15.26
Box's volume:0.09
输入样例3:
在这里给出一组输入。例如:
2 -2.3 5.110
输出样例2:
在这里给出相应的输出。例如:
Wrong Format
答案代码如下:
import java.util.Scanner;
class Shape{
public double getArea() {
return 0.0;
}
public Shape() {
System.out.println("Constructing Shape");
}
}
class Circle extends Shape{
private double radius;
public double getArea() {
return Math.PI*radius*radius;
}
public Circle() {
System.out.println("Constructing Circle");
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
}
class Rectangle extends Shape{
private double width;
private double length;
public double getArea() {
return width*length;
}
public Rectangle() {
System.out.println("Constructing Rectangle");
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
}
class Ball extends Circle{
public double getArea() {
return 4*super.getArea();
}
public double getVolume(){
double radius1=getRadius();
return Math.PI*radius1*radius1*radius1*4.0/3.0;
}
public Ball() {
System.out.println("Constructing Ball");
}
}
class Box extends Rectangle{
private double height;
public double getArea(){
double width=getWidth();
double length=getLength();
return 2*(width*length+height*width+length*height);
}
public double getVolume() {
return super.getArea()*height;
}
public Box() {
System.out.println("Constructing Box");
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
switch(n) {
case 1:
double radius=sc.nextDouble();
if(radius<=0) {
System.out.println("Wrong Format");
}else {
Circle cir=new Circle();
cir.setRadius(radius);
System.out.printf("Circle's area:%.2f",cir.getArea());}
break;
case 2:
double width=sc.nextDouble();
double length=sc.nextDouble();
if(width<=0||length<=0) {
System.out.println("Wrong Format");
}else {
Rectangle rec=new Rectangle();
rec.setLength(length);
rec.setWidth(width);
System.out.printf("Rectangle's area:%.2f",rec.getArea());}
break;
case 3:
double radius1=sc.nextDouble();
if(radius1<=0) {
System.out.println("Wrong Format");
}else {
Ball ba=new Ball();
ba.setRadius(radius1);
System.out.printf("Ball's surface area:%.2f\n",ba.getArea());
System.out.printf("Ball's volume:%.2f\n",ba.getVolume());
}
break;
case 4:
double width1=sc.nextDouble();
double length1=sc.nextDouble();
double height=sc.nextDouble();
if(width1<=0||length1<=0||height<=0) {
System.out.println("Wrong Format");
}
else {
Box bo=new Box();
bo.setWidth(width1);
bo.setHeight(height);
bo.setLength(length1);
System.out.printf("Box's surface area:%.2f\n",bo.getArea());
System.out.printf("Box's volume:%.2f\n",bo.getVolume());}
break;
default:
System.out.println("Wrong Format");
}
}
}
7-11 jmu-Java-03面向对象基础-04-形状-继承
前言
前面题目形状中我们看到,为了输出所有形状的周长与面积,需要建立多个数组进行多次循环。这次试验使用继承与多态来改进我们的设计。
本题描述
1.定义抽象类Shape
属性:不可变静态常量double PI
,值为3.14
,
抽象方法:public double getPerimeter()
,public double getArea()
2.Rectangle与Circle类均继承自Shape类。
Rectangle类(属性:int width,length)、Circle类(属性:int radius)。
带参构造方法为Rectangle(int width,int length)
,Circle(int radius)
。toString
方法(Eclipse自动生成)
3.编写double sumAllArea
方法计算并返回传入的形状数组中所有对象的面积和与double sumAllPerimeter
方法计算并返回传入的形状数组中所有对象的周长和。
4.main方法
4.1 输入整型值n,然后建立n个不同的形状。如果输入rect,则依次输入宽、长。如果输入cir,则输入半径。
4.2 然后输出所有的形状的周长之和,面积之和。并将所有的形状信息以样例的格式输出。 提示:使用Arrays.toString
。
4.3 最后输出每个形状的类型与父类型.使用类似shape.getClass()
//获得类型, shape.getClass().getSuperclass()
//获得父类型;
注意:处理输入的时候使用混合使用nextInt
与nextLine
需注意行尾回车换行问题。
思考
- 你觉得sumAllArea和sumAllPerimeter方法放在哪个类中更合适?
- 是否应该声明为static?
输入样例:
4
rect
3 1
rect
1 5
cir
1
cir
2
输出样例:
38.84
23.700000000000003
[Rectangle [width=3, length=1], Rectangle [width=1, length=5], Circle [radius=1], Circle [radius=2]]
class Rectangle,class Shape
class Rectangle,class Shape
class Circle,class Shape
class Circle,class Shape
答案代码如下:
import java.util.Scanner;
abstract class Shape{
final double PI=3.14;
public abstract double getPerimeter();
public abstract double getArea();
}
class Rectangle extends Shape{
public int width;
public int length;
Rectangle(int width,int length){
this.length=length;
this.width=width;
}
@Override
public String toString() {
return "Rectangle [width=" + width + ", length=" + length + "]";
}
public double getPerimeter() {
return (double)2.0*width+2*length;
}
public double getArea() {
return width*length;
}
}
class Circle extends Shape{
public int radius;
Circle(int radius){
this.radius=radius;
}
@Override
public String toString() {
return "Circle [radius=" + radius + "]";
}
public double getPerimeter() {
return (double)PI*2*radius;
}
public double getArea() {
return (double)PI*radius*radius;
}
}
public class Main{
static double sumAllArea(Shape []sha) {
double sum=0.0;
for(Shape i:sha) {
sum=sum+i.getArea();
}
return sum;
}
static double sumAllPerimeter(Shape []sha) {
double sum=0.0;
for(Shape i:sha) {
sum=sum+i.getPerimeter();
}
return sum;
}
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
Shape sha[]=new Shape[n];
for(int i=0;i<n;i++) {
String str=sc.next();
if(str.equals("rect")) {
int width=sc.nextInt();
int length=sc.nextInt();
sha[i]=new Rectangle(width,length);
}
if(str.equals("cir")) {
int radius=sc.nextInt();
sha[i]=new Circle(radius);
}
}
System.out.printf("%.2f\n",sumAllPerimeter(sha));
System.out.println(sumAllArea(sha));
System.out.print("[");
for(int i=0;i<n;i++) {
if(i!=0) {
System.out.print(", ");//别忘记空格
}
System.out.print(sha[i].toString());
}
System.out.println("]");
for(Shape i:sha) {
System.out.println(i.getClass()+","+i.getClass().getSuperclass());
}
}
}
7-12 集体评分2
程序填空题。请补充以下代码,完成题目要求。(注意:需要提交完整代码) 有一个团队由5个人组成。他们每个人给指导老师一个分数,去掉最高分,去掉最低分,剩下的3个分数的平均分就是该团队对指导老师的评分。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] grade = new int[5];
for(int i=0; i<grade.length; i++){
grade[i] = in.nextInt();
}
RR rr = new RT(grade);
double dd = rr.mark();
System.out.printf("%.2f",dd);
}
}
interface RR{
double mark();
}
class RT implements RR{
int[] grade;
public RT(int[] grade){
this.grade = grade;
}
}
输入格式:
在一行中给出5个不超过10的正整数(从小到大排列)。
输出格式:
输出集体评分,保留小数点后两位。
输入样例:
1 2 4 6 9
输出样例:
4.00
答案代码如下:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] grade = new int[5];
for(int i=0; i<grade.length; i++){
grade[i] = in.nextInt();
}
RR rr = new RT(grade);
double dd = rr.mark();
System.out.printf("%.2f",dd);
}
}
interface RR{
double mark();
}
class RT implements RR{
int[] grade;
public RT(int[] grade){
this.grade = grade;
}
public double mark() {
double aver;
double sum=0;
int max=grade[0];
int min=grade[0];
for(int i=0;i<5;i++) {
if(max<grade[i]) {
max=grade[i];
}
if(min>grade[i]) {
min=grade[i];
}
sum=sum+grade[i];
}
aver=(sum-max-min)/3;
return aver;
}
}
7-13 接口--四则计算器
- 利用接口做参数,写个计算器,能完成加减乘除运算。
- 定义一个接口ICompute含有一个方法int computer(int n, int m)。
- 定义Add类实现接口ICompute,实现computer方法,求m,n之和
- 定义Sub类实现接口ICompute,实现computer方法,求n-m之差
- 定义Main类,在里面输入两个整数a, b,利用Add类和Sub类的computer方法,求第一个数a和第二个数b之和,输出和,第一个数a和第二个数b之差,输出差。
输入格式:
输入在一行中给出2个整数
输出格式:
输出两个数的和、差
输入样例:
在这里给出一组输入。例如:
6 7
输出样例:
在这里给出相应的输出。例如:
13
-1
答案代码如下:
import java.util.Scanner;
interface ICompute{
int computer(int n,int m);
}
class Add implements ICompute{
public int computer(int n,int m) {
return m+n;
}
}
class Sub implements ICompute{
public int computer(int n,int m) {
return n-m;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
Add ad=new Add();
System.out.println(ad.computer(a,b));
Sub su=new Sub();
System.out.println(su.computer(a,b));
}
}
7-14 成绩录入时的及格与不及格人数统计
编写一个程序进行一个班某门课程成绩的录入,能够控制录入成绩总人数,对录入成绩统计其及格人数和不及格人数。设计一个异常类,当输入的成绩小0分或大于100分时,抛出该异常类对象,程序将捕捉这个异常对象,并调用执行该异常类对象的toString()方法,该方法获取当前无效分数值,并返回一个此分数无效的字符串。
输入格式:
从键盘中输入学生人数n
从键盘中输入第1个学生的成绩
从键盘中输入第2个学生的成绩
...
从键盘中输入第n个学生的成绩
(注:当输入的成绩无效时(即分数为小于0或大于100)可重新输入,且输出端会输出此分数无效的提醒。)
输出格式:
显示及格总人数
显示不及格总人数
输入样例:
在这里给出一组输入。例如:
3
100
30
60
输出样例:
在这里给出相应的输出。例如:
2
1
输入样例:
在这里给出一组输入。例如:
2
200
69
30
输出样例:
在这里给出相应的输出。例如:
200invalid!
1
1
答案代码如下:
import java.util.ArrayList;
import java.util.Scanner;
class MyException extends Exception{
public MyException(int grade) {
super(grade+"invalid!");
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Integer> grade=new ArrayList<>();
int pass=0;
int unpass=0;
int n=sc.nextInt();
for(int i=0;i<n;i++) {
int grades=sc.nextInt();
grade.add(grades);
if(grades<0||grades>100) {
n++;
}
}
for(int i:grade) {
try {
if(i>=60&&i<=100) {
pass++;
}
if(i<60&&i>=0) {
unpass++;
}
if(i<0||i>100) {
throw new MyException(i);
}
}catch(MyException e){
System.out.println(e.getMessage());
}
}
System.out.println(pass);
System.out.println(unpass);
}
}
7-15 jmu-Java-02基本语法-02-StringBuilder
输入3个整数n、begin、end。
首先,使用如下代码:
for(int i=0;i<n;i++)
将从0到n-1的数字拼接为字符串str。如,n=12
,则拼接出来的字符串为01234567891011
最后截取字符串str从begin到end(包括begin,但不包括end)之间的字符串,并输出。
输入样例:
10
5
8
1000
800
900
输出样例:
567
0330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533
答案代码如下:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNextInt()) {
int n=sc.nextInt();
int begin=sc.nextInt();
int end=sc.nextInt();
StringBuilder str=new StringBuilder();
for(int i=0;i<n;i++) {
str.append(i);
}
System.out.println(str.substring(begin,end));
}
}
}
7-16 单词替换
设计一个对字符串中的单词查找替换方法,实现对英文字符串中所有待替换单词的查找与替换。
输入格式:
首行输入母字符串,第二行输入查询的单词,第三行输入替换后的单词。
输出格式:
完成查找替换后的完整字符串
输入样例:
在这里给出一组输入。例如:
Although I am without you, I will always be ou you
ou
with
输出样例:
在这里给出相应的输出。例如:
Although I am without you, I will always be with you
答案代码如下:
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str=sc.nextLine();
String check=sc.next();
String replace=sc.next();
String ss[]=str.split(" ");
StringBuffer sb=new StringBuffer();
for(int i=0;i<ss.length;i++) {
if(ss[i].equals(check)) {
ss[i]=ss[i].replace(ss[i], replace);
}
sb.append(ss[i]);
if(i<ss.length-1) {
sb.append(" ");
}
}
System.out.println(sb);
}
}
7-17 通过键盘输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
统计一行字符串中的英文字母个数、空格个数、数字个数、其他字符个数
输入格式:
通过键盘输入一行字符(任意字符)
输出格式:
统计一行字符串中的中英文字母个数、空格个数、数字个数、其他字符个数
输入样例:
rwrwewre2345asdJSJQI%^&(& *&sdf YY( 2342-k'
输出样例:
字母个数:22
数字个数:8
空格个数:5
其他字符个数:10
答案代码如下:
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str=sc.nextLine();
char ch[]=str.toCharArray();
int letter=0;
int space=0;
int number=0;
int other=0;
for(int i=0;i<ch.length;i++) {
if((ch[i]>='a'&&ch[i]<='z')||(ch[i]>='A'&&ch[i]<='Z')) {
letter++;}else
if(ch[i]>=47&&ch[i]<57) {
number++;}else
if(ch[i]==32) {
space++;}else {
other++;}
}
System.out.println("字母个数:"+letter);
System.out.println("数字个数:"+number);
System.out.println("空格个数:"+space);
System.out.println("其他字符个数:"+other);
}
}
7-18 jmu-Java-02基本语法-08-ArrayList入门
本习题主要用于练习如何使用ArrayList来替换数组。
新建1个ArrayList<String> strList
用来存放字符串,然后进行如下操作。
提示: 查询Jdk文档中的ArrayList。
注意: 请使用System.out.println(strList)
输出列表元素。
输入格式
-
输入: n个字符串,放入
strList
。直到输入为!!end!!
时,结束输入。 -
在
strList
头部新增一个begin
,尾部新增一个end
。 -
输出列表元素
-
输入: 字符串
str
-
判断
strList
中有无包含字符串str
,如包含输出true
,否则输出false
。并且输出下标,没包含返回-1。 -
在strList中从后往前找。返回其下标,找不到返回-1。
-
移除掉第1个(下标为0)元素,并输出。然后输出列表元素。
-
输入: 字符串str
-
将第2个(下标为1)元素设置为字符串str.
-
输出列表元素
-
输入: 字符串str
-
遍历strList,将字符串中包含str的元素放入另外一个
ArrayList strList1
,然后输出strList1。 -
在strList中使用
remove
方法,移除第一个和str相等的元素。 -
输出strList列表元素。
-
使用
clear
方法,清空strList。然后输出strList的内容,size()
与isEmpty()
,3者之间用,
连接。
输入样例:
a1 b1 3b a2 b2 12b c d !!end!!
b1
second
b
输出样例:
[begin, a1, b1, 3b, a2, b2, 12b, c, d, end]
true
2
2
begin
[a1, b1, 3b, a2, b2, 12b, c, d, end]
[a1, second, 3b, a2, b2, 12b, c, d, end]
[3b, b2, 12b]
[a1, second, 3b, a2, b2, 12b, c, d, end]
[],0,true
答案代码如下:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
ArrayList<String> strList = new ArrayList<String>();
Scanner sc = new Scanner(System.in);
while (true) {
String s1 = sc.next();
if (!s1.equals("!!end!!")) {
strList.add(s1);
} else break;
}
String s1=new String();
strList.add(0, "begin");
strList.add("end");
System.out.println(strList);
String s2 = new String();
s1 = sc.next();
System.out.println(strList.contains(s1));
System.out.println(strList.indexOf(s1));
System.out.println(strList.lastIndexOf(s1));
System.out.println(strList.get(0));
strList.remove(0);
System.out.println(strList);
s1= sc.next();
strList.set(1, s1);
System.out.println(strList);
ArrayList strList1 = new ArrayList();
s1=sc.next();
for (int j = 0; j < strList.size(); j++)
{
if (((String) strList.get(j)).contains(s1)) {
strList1.add(strList.get(j));
}
}
System.out.println(strList1);
strList.remove(s1);
System.out.println(strList);
strList.clear();
System.out.println(strList+","+strList.size()+","+strList.isEmpty());
}
}
7-19 找到出勤最多的人
根据教师的花名册,找到出勤最多的人。
输入格式:
出勤记录单行给出,数据直接使用空格分割。
输出格式:
单行输出(若有多人,人名直接使用空格分割,结尾处没有空格)。
输入样例:
在这里给出一组输入。例如:
zs ls ww ml zs ls ml zs ww
输出样例:
在这里给出相应的输出。例如:
zs
答案代码如下:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Objects;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str=sc.nextLine();
String str1[]=str.split(" ");
ArrayList<String> list=new ArrayList<>(Arrays.asList(str1));
String max=list.get(0);
int maxnum=1;
for(int i=0;i<list.size();i++) {
int count=1;
for(int j=i+1;j<list.size();j++) {
if(Objects.equals(list.get(i), list.get(j))) {
count++;
}
if(count>maxnum) {
maxnum=count;
max=list.get(i);
}
}
}
System.out.println(max);
}
}
7-20 创建一个倒数计数线程
创建一个倒数计数线程。要求:1.该线程使用实现Runnable接口的写法;2.程序该线程每隔0.5秒打印输出一次倒数数值(数值为上一次数值减1)。
输入格式:
N(键盘输入一个整数)
输出格式:
每隔0.5秒打印输出一次剩余数
输入样例:
6
输出样例:
在这里给出相应的输出。例如:
6
5
4
3
2
1
0
答案代码如下:
import java.util.Scanner;
class run implements Runnable{
public void run() {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=n;i>=0;i--) {
System.out.println(i);
}
try {
Thread.sleep(500);
}catch(InterruptedException e) {
e.printStackTrace();
}
}
}
public class Main {
public static void main(String[] args) {
Runnable r=new run();
Thread th=new Thread(r);
th.start();
}
}
7-21 USB接口的定义
定义一个USB接口,并通过Mouse和U盘类实现它,具体要求是:
1.接口名字为USB,里面包括两个抽象方法:
void work();描述可以工作
void stop(); 描述停止工作
2.完成类Mouse,实现接口USB,实现两个方法:
work方法输出“我点点点”;
stop方法输出 “我不能点了”;
3.完成类UPan,实现接口USB,实现两个方法:
work方法输出“我存存存”;
stop方法输出 “我走了”;
4测试类Main中,main方法中
定义接口变量usb1 ,存放鼠标对象,然后调用work和stop方法
定义接口数组usbs,包含两个元素,第0个元素存放一个Upan对象,第1个元素存放Mouse对象,循环数组,对每一个元素都调用work和stop方法。
输入格式:
输出格式:
输出方法调用的结果
输入样例:
在这里给出一组输入。例如:
输出样例:
在这里给出相应的输出。例如:
我点点点
我不能点了
我存存存
我走了
我点点点
我不能点了
答案代码如下:
import java.util.Scanner;
interface USB{
void work();
void stop();
}
class Mouse implements USB{
public void work() {
System.out.println("我点点点");
}
public void stop() {
System.out.println("我不能点了");
}
}
class UPan implements USB{
public void work() {
System.out.println("我存存存");
}
public void stop() {
System.out.println("我走了");
}
}
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
USB usb1=new Mouse();
usb1.work();
usb1.stop();
USB usbs[]=new USB[2];
usbs[0]=new UPan();
usbs[1]=new Mouse();
for(USB a:usbs){
a.work();
a.stop();
}
}
}
7-22 Circle类
a 定义圆类Circle,其中包括:
- 成员变量定义 private int radius
- 方法定义 包括下列要求
- 定义无参构造方法 ,给radius赋值为2,并添加语句System.out.println("this is a constructor");
- 定义有参构造方法 ,接收用户给给radius赋值,如果用户输入半径为<=0,则让半径的值为2,并添加语句System.out.println("this is a constructor with para");
- 为radius半径添加setter方法,接收用户输入的半径,如果用户输入半径为<=0,则让半径的值为2
- 为radius半径添加getter方法,返回用户输入的半径
- 定义求面积方法public int gerArea(),π使用Math.PI代替,面积的结果强制转换为int返回
- 定义toString方法,public String toString( )方法体为:
return "Circle [radius=" + radius + "]";
b定义Main类,在main方法中,完成下列操作
- .定义并创建Circle的第一个对象c1,并使用println方法输出c1
- 求c1的面积并输出
- 定义并创建Circle的第一个对象c2,并使用println方法输出c2
- 从键盘接收整数半径,并赋值给c2的半径,使用println方法输出c2
- 求c2的面积并输出
- 从键盘接收整数半径,并创建Circle的第三个对象c3,并将用户输入整数半径通过有参构造方法传递给出c3,使用println方法输出c3
- 求c3的面积并输出
### 输入格式: 从键盘输入一个整数半径
输出格式:
分别输出c1和c2对象的信息
输入样例:
在这里给出一组输入。例如:
4
5
-4
-2
输出样例:
在这里给出相应的输出。例如:
this is a constructor
Circle [radius=2]
c1:area=12
this is a constructor
Circle [radius=2]
Circle [radius=4]
c2:area=50
this is a constructor with para
Circle [radius=5]
c3:area=78
this is a constructor
Circle [radius=2]
c1:area=12
this is a constructor
Circle [radius=2]
Circle [radius=2]
c2:area=12
this is a constructor with para
Circle [radius=2]
c3:area=12
答案代码如下:
import java.util.Scanner;
class Circle{
private int radius;
public Circle() {
this.radius=2;
System.out.println("this is a constructor");
}
public Circle(int radius) {
if(radius<=0) {
this.radius=2;
}
else {
System.out.println("this is a constructor with para");
this.radius=radius;}
}
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
if(radius<=0) {
this.radius=2;}
else {
this.radius=radius;
}
}
public int getArea() {
return (int) (Math.PI*radius*radius);
}
@Override
public String toString() {
return "Circle [radius=" + radius + "]";
}
}
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int radius=sc.nextInt();
int radius1=sc.nextInt();
Circle c1=new Circle();
System.out.println(c1.toString());
System.out.println("c1:area="+c1.getArea());
Circle c2=new Circle();
System.out.println(c2);
c2.setRadius(radius);
System.out.println(c2.toString());
System.out.println("c2:area="+c2.getArea());
Circle c3=new Circle(radius1);
System.out.println(c3.toString());
System.out.println("c3:area="+c3.getArea());
}
}
7-23 数组与对象
定义一个Person类
- 包含name
- 在Person类完成无参构造方法,在无参构造方法设置name值为none
- 有参构造方法给name传值
- 为name属性添加set和get方法
- <
public String toString(){
return "name:"+name;
}
2 在Main类的main方法中
- 创建Person类对象数组,其中数组长度为2
- 并为Person对象数组赋值,其中第一个元素对象为无参person对象,第二个元素对象为有参person对象,姓名为用户键盘给出
- 并循环输出两个对象的信息(调用toString方法),一个对象输出一行
输入格式:
输入姓名
输出格式:
输出姓名,一行一个对象
输入样例:
在这里给出一组输入。例如:
jerry
输出样例:
在这里给出相应的输出。例如:
name:none
name:jerry
答案代码如下:
import java.util.Scanner;
class Person{
String name;
public Person() {
this.name="none";
}
public Person(String name) {
this.name=name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString(){
return "name:"+name;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String name=sc.next();
Person per[]=new Person[2];
per[0]=new Person();
per[1]=new Person(name);
System.out.println(per[0]);
System.out.println(per[1]);
}
}
7-24 圆柱体类设计
-
- 定义一个圆柱类Cylinder
- 里面包含私有属性 private int radius(半径),height(高)
- 为属性完成其setter getter方法
- 完成带参构造方法Cylinder(int radius,height),该方法中包含一句System.out.println("Constructor with para");
- 完成无参构造方法Cylinder(),在无参构造方法中调用有参构造方法,为半径和高赋值为2,1,该方法包含一句System.out.println("Constructor no para");
- 完成求体积方法 public int getVolumn(){} 求圆柱体积,π使用Math.PI
- 定义测试类Main,在main方法中,按照顺序要求完成下列操作
- 从键盘接收两个数,第一个为半径,第二个为高,并利用刚才输出两个数创建圆柱体对象c1,求c1的体积并输出。
- 使用无参构造方法 创建第二个圆柱体对象c2,求c2的体积并输出。
输入格式:
在一行中输入半径 和高。
输出格式:
对每一个圆柱体输出它的体积
输入样例:
在这里给出一组输入。例如:
2 3
输出样例:
在这里给出相应的输出。例如:
Constructor with para
37
Constructor with para
Constructor no para
12
答案代码如下:
import java.util.Scanner;
class Cylinder{
private int radius;
private int height;
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public Cylinder(int radius,int height) {
this.radius=radius;
this.height=height;
System.out.println("Constructor with para");
}
public Cylinder() {
this(2,1);
System.out.println("Constructor no para");
}
public int getVolume() {
return (int) (Math.PI*this.radius*this.radius*this.height);
}
}
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int radius=sc.nextInt();
int height=sc.nextInt();
Cylinder c1=new Cylinder(radius,height);
System.out.println(c1.getVolume());
Cylinder c2=new Cylinder();
System.out.println(c2.getVolume());
}
}
7-25 List的使用
本题练习列表的使用。
- 定义Person类
- 定义私有属性String name,int age,使用Eclipse生成每个属性setter 、getter,有参Person(String name,int age) 、无参 构造方法,toString方法。
- 定义Main类,在main方法中
- 定义List list = new ArrayList();
- 用键盘给变量n赋值
- 生成n个Person对象并添加到列表中,该Person的name和age通过键盘给出
- 循环列表,输出列表所有Person对象信息(调用toString方法)
- 输入一个字符串表示姓名,判断该字符串表示的Person对象在List中是否存在,如果存在,输出该Person,否则输出此人不存在。
输入格式:
先一行输入n表示对象个数,然后每行输入一个Person对象的name和age
一行输入一个人的姓名对其进行查询
输出格式:
对每一对象,在一行中输出对象的信息。
对查询的人员,查到输出该人的信息,否则输出此人不存在。
输入样例:
在这里给出一组输入。例如:
3
zhang 23
li 44
wang 33
li
3
zhang 23
li 44
wang 33
may
输出样例:
在这里给出相应的输出。例如:
Person [name=zhang, age=23]
Person [name=li, age=44]
Person [name=wang, age=33]
Person [name=li, age=44]
Person [name=zhang, age=23]
Person [name=li, age=44]
Person [name=wang, age=33]
此人不存在
答案代码如下
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
class Person{
private String name;
private int 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 Person(String name,int age) {
this.name=name;
this.age=age;
}
public Person() {
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
public class lin {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
ArrayList<Person> list = new ArrayList();
int n=sc.nextInt();
for(int i=0;i<n;i++) {
list.add(new Person(sc.next(),sc.nextInt()));
}
for(Person a:list) {
System.out.println(a.toString());
}
String str=sc.next();
int i;
for(i=0;i<list.size();i++) {
if(list.get(i).getName().equals(str)) {
System.out.println(list.get(i));
break;}
}
if(i==list.size()) {
System.out.println("此人不存在");
}
}
}
7-26 jmu-Java-03面向对象基础-03-形状
1. 定义长方形类与圆形类Circle
长方形类-类名:Rectangle
,private属性:int width,length
圆形类-类名:Circle
,private属性:int radius
编写构造函数:
带参构造函数:Rectangle(width, length)
,Circle(radius)
编写方法:public int getPerimeter()
,求周长。public int getArea()
,求面积。toString
方法,使用Eclipse自动生成。
注意:
- 计算圆形的面积与周长,使用
Math.PI
。 - 求周长和面积时,应先计算出其值(带小数位),然后强制转换为
int
再返回。
2. main方法
- 输入2行长与宽,创建两个Rectangle对象放入相应的数组。
- 输入2行半径,创建两个Circle对象放入相应的数组。
- 输出1:上面2个数组中的所有对象的周长加总。
- 输出2:上面2个数组中的所有对象的面积加总。
- 最后需使用
Arrays.deepToString
分别输出上面建立的Rectangle数组与Circle数组
思考:如果初次做该题会发现代码冗余严重。使用继承、多态思想可以大幅简化上述代码。
输入样例:
1 2
3 4
7
1
输出样例:
69
170
[Rectangle [width=1, length=2], Rectangle [width=3, length=4]]
[Circle [radius=7], Circle [radius=1]]
答案代码如下:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
class Rectangle
{
private int width;
private int length;
public Rectangle(int width,int length) {
this.length=length;
this.width=width;
}
public int getPerimeter() {
return 2*width+2*length;
}
public int getArea() {
return width*length;
}
@Override
public String toString() {
return "Rectangle [width=" + width + ", length=" + length + "]";
}
}
class Circle{
private int radius;
public Circle(int radius) {
this.radius=radius;
}
public int getPerimeter() {
return (int) (2*Math.PI*radius);
}
public int getArea() {
return (int) (Math.PI*radius*radius);
}
@Override
public String toString() {
return "Circle [radius=" + radius + "]";
}
}
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
ArrayList<Rectangle> rec=new ArrayList<Rectangle>();
ArrayList<Circle> cir=new ArrayList<Circle>();
rec.add(new Rectangle(sc.nextInt(),sc.nextInt()));
rec.add(new Rectangle(sc.nextInt(),sc.nextInt()));
cir.add(new Circle(sc.nextInt()));
cir.add(new Circle(sc.nextInt()));
int l=0;
int s=0;
for(Rectangle a:rec) {
l=a.getPerimeter()+l;
s=a.getArea()+s;
}
for(Circle b:cir) {
l=b.getPerimeter()+l;
s=b.getArea()+s;
}
System.out.println(l);
System.out.println(s);
System.out.println(rec);
System.out.println(cir);
}
}
欢迎大家批评指正!!!