这个问题需要实现一个函数,判断输入的整数是否是偶数,如果是偶数,返回true,否则返回false。
函数接口定义:
public static boolean isOdd(int data)
说明:其中 data
是用户输入的参数。 data
的值不超过int
的范围。函数须返回 true
或者 false
。
裁判测试程序样例:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); int data=in.nextInt(); System.out.println(isOdd(data)); } /* 请在这里给出isOdd(i)函数 */ }
输入样例:
8
输出样例:
true
public static boolean isOdd(int data) { if(data%2==0) { return true; }else { return false; } }
设计一个名字Rectangle类表示矩形。这类包括: 两个名为width和height的double型数据域分别表示矩形的宽度和高度。width和height默认值为1. 一个无参构造方法。 一个为width和height指定值的矩形结构方法。 一个名为getArea()方法返回矩形面积。 一个名为getPerimeter()方法返回矩形周长。
类名为:
Rectangle
裁判测试程序样例:
import java.util.Scanner; /* 您的代码将嵌入到这里 */ public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); double w = input.nextDouble(); double h = input.nextDouble(); Rectangle myRectangle = new Rectangle(w, h); System.out.println(myRectangle.getArea()); System.out.println(myRectangle.getPerimeter()); input.close(); } }
输入样例:
3.14 2.78
输出样例:
8.7292 11.84
class Rectangle{ double width=1; double height=1; Rectangle(){ } Rectangle(double width,double height){ this.width=width; this.height=height; } double getArea() { return width*height; } double getPerimeter() { return 2*width 2*height; } }
裁判测试程序样本显示了一个定义基类People
、派生类Student
以及测试两类的相关性Java部分代码缺失,请补充完整,以确保测试程序的正常运行。
函数接口定义:
提示: 观察派生类代码和main方法中的测试代码,补充缺失的代码。
裁判测试程序样例:
注:真实测试程序中使用的数据可能与样本测试程序不同,但相关方法(函数)仅根据样本中的格式调用。
class People{ protected String id; protected String name; /** 您提交的代码将嵌入此处(更换此行) **/ } class Student extends People{ protected String sid; protected int score; public Student() { name = "Pintia Student"; } public Student(String id, String name, String sid, int score) { super(id, name); this.sid = sid; this.score = score; } public void say() { System.out.println("I'm a student. My name is " this.name "."); } } public class Main { public static void main(String[] args) { Student zs = new Student(); zs.setId("370211X"); zs.setName("Zhang San"); zs.say(); System.out.println(zs.getId() " , " zs.getName()); Student ls = new Student("330106","Li Si","20183001007",98); ls.say(); System.out.println(ls.getId() " : " ls.getName()); People ww = new Student(); ww.setName("Wang Wu"); ww.say(); People zl = new People("370202", "Zhao Liu"); zl.say(); } }
输入样例:
以下是一组输入。
(无)
输出样例:
这里给出相应的输出。
I'm a student. My name is Zhang San. 370211X , Zhang San I'm a student. My name is Li Si. 330106 : Li Si I'm a student. My name is Wang Wu. I'm a person! My name is Zhao Liu.
public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void say() { System.out.println("I'm a person! My name is " this.name "."); } People(){ } People(String id,String name){ this.id=id; this.name=name; }
1.类是对某一类事物的抽象描述,对象是某一类中一个或多个具体的事物
类是创建对象的模板,对象是类的例子
2.封装:将对象的属性(域/数据)和行为(方法/函数)定义在一起,形成一个类
隐藏:对类中属性和行为起到一定的保护作用,不能直接修改,提供一些接口、函数访问
3.成员变量与局部变量
前者是在类中,后者是方法中定义的变量或方法的参变量
成员变量属于类,可以用public,protected,private,static修饰而局部变量不允许。
都可以用final修饰
4.方法的重载
指一个类中可以声明多个同名、但参数列表不同的成员方法。(参数的个数不同、参数的类型不同)
5.对象运算符instanceof用于判断当前对象是否是属于某个类。
6.this用法
This是当前对象,也可以看作是当前对象的别名,哪一对象调用这个方法,this就表示的是哪一个对象。一般用于指代对象本身、访问本类的成员变量和成员方法、调用本类重载的构造方法。
经典实例:
区分成员变量和局部变量
Point(int x,int y){
this.x=x;
this.y=y;//成员变量x和参数x同名,用this区分
}
void setX(int x){
this.x=x;
}
void printInfo(){
System.out.println("坐标值("+this.x+","+this.y+")");
}
调用实例方法
void changePoint(int x,int y){
this.setX(x);
this.y=y;
}
void changePoint(Point p){
this.setX(p.x);
this.y=p.getY();
}
拷贝构造方法
Point(int x,int y){
this.x=x;
this.y=y;
}
Point(){
this(1,1);//设置无参构造方法的初始值
}
Point(Point p){
this(p.x,p.y);
}
比较对象
public boolean equals(Point p){
return (this==p)||(p!=null&&this.x==p.x&&this.y==y);
//this指代调用当前方法的对象,通过this引用访问当前对象的成员变量
}
7.在静态成员方法体中,不能访问实例成员,不能使用this引用。static不能修饰方法的局部变量。
8.如果一个类中的成员变量是由其他类所定义的对象,这个类称为组合类,组合类所定义的对象称为组合对象。
9.在类的内部定义类,称为嵌套类或者内部类;嵌套类的定义形式与非嵌套类的定义形式基本相同,在嵌套类中可以定义变量和方法;可以将嵌套的类看作外层类的一个成员,所以它可以访问外层类中的任何成员。
裁判测试程序样例中展示的是一段定义基类People
、派生类Student
以及测试两个类的相关Java代码,其中缺失了部分代码,请补充完整,以保证测试程序正常运行。
函数接口定义:
提示: 观察类的定义和main方法中的测试代码,补全缺失的代码。
裁判测试程序样例:
注意:真正的测试程序中使用的数据可能与样例测试程序中不同,但仅按照样例中的格式调用相关方法(函数)。
class People{
private String id;
private String name;
public People(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
}
class Student extends People{
private String sid;
private int score;
public Student(String id, String name, String sid, int score) {
/** 你提交的代码将被嵌在这里(替换此行) **/
}
public String toString(){
return ("(Name:" + this.getName()
+ "; id:" + this.getId()
+ "; sid:" + this.sid
+ "; score:" + this.score
+ ")");
}
}
public class Main {
public static void main(String[] args) {
Student zs = new Student("370202X", "Zhang San", "1052102", 96);
System.out.println(zs);
}
}
输入样例:
在这里给出一组输入。例如:
(无)
输出样例:
(Name:Zhang San; id:370202X; sid:1052102; score:96)
super(id,name);
this.sid=sid;
this.score=score;
在类Student中重写Object类的equals方法。使Student对象学号(id)相同时判定为同一对象。
函数接口定义:
在类Student中重写Object类的equals方法。使Student对象学号(id)相同时判定为同一对象。
裁判测试程序样例:
import java.util.Scanner;
class Student {
int id;
String name;
int age;
public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
/* 请在这里填写答案 */
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Student s1 = new Student(sc.nextInt(),sc.next(),sc.nextInt());
Student s2 = new Student(sc.nextInt(),sc.next(),sc.nextInt());
System.out.println(s1.equals(s2));
sc.close();
}
}
答案代码如下:
public boolean equals(Object obj) {
boolean b=false;
if(obj instanceof Student){
Student s=(Student) obj;
if(s.id==this.id) {
b=true;
}
}
return b;
}
请从下列的抽象类shape类扩展出一个圆形类Circle,这个类圆形的半径radius作为私有成员,类中应包含初始化半径的构造方法。
public abstract class shape {// 抽象类
public abstract double getArea();// 求面积
public abstract double getPerimeter(); // 求周长
}
主类从键盘输入圆形的半径值,创建一个圆形对象,然后输出圆形的面积和周长。保留4位小数。
圆形类名Circle
裁判测试程序样例:
import java.util.Scanner;
import java.text.DecimalFormat;
abstract class shape {// 抽象类
/* 抽象方法 求面积 */
public abstract double getArea( );
/* 抽象方法 求周长 */
public abstract double getPerimeter( );
}
/* 你提交的代码将被嵌入到这里 */
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
DecimalFormat d = new DecimalFormat("#.####");// 保留4位小数
double r = input.nextDouble( );
shape c = new Circle(r);
System.out.println(d.format(c.getArea()));
System.out.println(d.format(c.getPerimeter()));
input.close();
}
}
输入样例:
3.1415926
输出样例:
31.0063
19.7392
class Circle extends shape{
private double radius;
public Circle(double radius) {
this.radius=radius;
}
public double getArea() {
return Math.PI*radius*radius;
}
public double getPerimeter() {
return 2.0*Math.PI*radius;
}
}
创建一个直角三角形类(regular triangle)RTriangle类,实现下列接口IShape。两条直角边长作为RTriangle类的私有成员,类中包含参数为直角边的构造方法。
interface IShape {// 接口
public abstract double getArea(); // 抽象方法 求面积
public abstract double getPerimeter(); // 抽象方法 求周长
}
###直角三角形类的定义:
直角三角形类的构造函数原型如下: RTriangle(double a, double b);
其中 a
和 b
都是直角三角形的两条直角边。
裁判测试程序样例:
import java.util.Scanner;
import java.text.DecimalFormat;
interface IShape {
public abstract double getArea();
public abstract double getPerimeter();
}
/*你写的代码将嵌入到这里*/
public class Main {
public static void main(String[] args) {
DecimalFormat d = new DecimalFormat("#.####");
Scanner input = new Scanner(System.in);
double a = input.nextDouble();
double b = input.nextDouble();
IShape r = new RTriangle(a, b);
System.out.println(d.format(r.getArea()));
System.out.println(d.format(r.getPerimeter()));
input.close();
}
}
输入样例:
3.1 4.2
输出样例:
6.51
12.5202
class RTriangle implements IShape{
private double width;
private double length;
public RTriangle(double width,double length) {
this.length=length;
this.width=width;
}
public double getArea() {
return 0.5*width*length;
}
public double getPerimeter() {
return length+width+Math.sqrt(length*length+width*width);
}
}
如果try块中的代码有可能抛出多种异常,且这些异常之间可能存在继承关系,那么在捕获异常的时候需要注意捕获顺序。
补全下列代码,使得程序正常运行。
裁判测试程序:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String choice = sc.next();
try {
if (choice.equals("number"))
throw new NumberFormatException();
else if (choice.equals("illegal")) {
throw new IllegalArgumentException();
} else if (choice.equals("except")) {
throw new Exception();
} else
break;
}
/*这里放置你的答案*/
}//end while
sc.close();
}
###输出说明 在catch
块中要输出两行信息:要输出自定义信息。如下面输出样例的number format exception
使用System.out.println(e)
输出异常信息,e是所产生的异常。
输入样例
number illegal except quit
输出样例
number format exception
java.lang.NumberFormatException
illegal argument exception
java.lang.IllegalArgumentException
other exception
java.lang.Exception
catch(Exception e) {
if(choice.equals("number")) {
System.out.println("number format exception");
System.out.println(e);
}
if(choice.equals("illegal")) {
System.out.println("illegal argument exception");
System.out.println(e);
}
if(choice.equals("except")) {
System.out.println("other exception");
System.out.println(e);}
}
输入整数n,创建n个对象,放入同一个数组中。
如果输入c
,则new Computer();
//注意:Computer是系统中已有的类,无需自己编写
如果输入d
,则根据随后的输入创建Double
类型对象。
如果输入i
,则根据随后的输入创建Integer
类型对象。
如果输入s
,则根据随后的输入创建String
类型对象。
如果不是以上这些输入,则不创建对象,而是将null
存入数组相应位置。
最后输出数组中的所有对象,如果数组中相应位置的元素为null
则不输出。
裁判测试程序:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//这边是你的代码
sc.close();
}
输入样例:
5
c
d 2.3
other
i 10
s Test
输出样例:
Test
10
2.3
//这行显示Computer对象toString方法
int n=sc.nextInt();
Object obj[]=new Object[n];
for(int i=0;i<n;i++) {
String s=sc.next();
switch(s) {
case "c":
obj[i]=new Computer();
break;
case "d":
obj[i]=new Double(sc.nextDouble());
break;
case "i":
obj[i]=new Integer(sc.nextInt());
break;
case "s":
obj[i]=new String(sc.next());
break;
default:
obj[i]=null;
}
}
for(int i=n-1;i>=0;i--) {
if(obj[i]!=null)
System.out.println(obj[i]);
}
本题运行时要求键盘输入10个人员的信息(每一个人信息包括:姓名,性别,年龄,民族),要求同学实现一个函数,统计民族是“汉族”的人数。
函数接口定义:
public static int numofHan(String data[])
其中 data[]
是传入的参数。 data[]
中的每一个元素都是一个完整的人员信息字符串,该字符串由“姓名,性别,年龄,民族”,各项之间用英文半角的逗号分隔。函数须返回 值是汉族的人数。
裁判测试程序样例:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
final int HUMANNUM=10;
String persons[]=new String[HUMANNUM];
Scanner in=new Scanner(System.in);
for(int i=0;i<persons.length;i++)
persons[i]=in.nextLine();
int result=numofHan(persons);
System.out.println(result);
}
/*在此处给出函数numofHan()*/
}
输入样例:
Tom_1,男,19,汉族
Tom_2,女,18,汉族
Tom_3,男,20,满族
Tom_4,男,18,汉族
Tom_5,男,19,汉族人
Tom_6,女,17,汉族
Tom_7,男,19,蒙古族
汉族朋友_1,男,18,汉族
Tom_8,male,19,老外
Tom_9,female,20,汉族
输出样例:
7
public static int numofHan(String data[]) {
int count=0;
String s="汉族";
for(String str:data) {
if(str.indexOf(s)>=0) {
count++;
}
}
return count;
}
备注:多种方法可见链接博客
java 6-1 人口统计_故园归梦的博客-CSDN博客_人口统计java
编写以下两个函数
//以空格(单个或多个)为分隔符,将line中的元素抽取出来,放入一个List
public static List<String> convertStringToList(String line)
//在list中移除掉与str内容相同的元素
public static void remove(List<String> list, String str)
裁判测试程序:
public class Main {
/*covnertStringToList函数代码*/
/*remove函数代码*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()){
List<String> list = convertStringToList(sc.nextLine());
System.out.println(list);
String word = sc.nextLine();
remove(list,word);
System.out.println(list);
}
sc.close();
}
}
底下展示了4组测试数据。
输入样例
1 2 1 2 1 1 1 2
1
11 1 11 1 11
11
2 2 2
1
1 2 3 4 1 3 1
1
输出样例
[1, 2, 1, 2, 1, 1, 1, 2]
[2, 2, 2]
[11, 1, 11, 1, 11]
[1, 1]
[2, 2, 2]
[2, 2, 2]
[1, 2, 3, 4, 1, 3, 1]
[2, 3, 4, 3]
public static List<String> convertStringToList(String line) {
List<String> list=new ArrayList<String>();
String line1[]=line.split("\\s+");
for(int i=0;i<line1.length;i++) {
list.add(line1[i]);
}
return list;
}
public static void remove(List<String> list, String str) {
for(int i=0;i<list.size();) {
if(str.equals(list.get(i))) {
list.remove(i);
i=0;
}else {
i++;
}
}
}
什么是泛型?
泛型允许类的成员的类型可以由外部程序来指定,也就是说可以以参数形式来指定类型,即“参数化类型”
常用泛型有泛型接口、泛型类、泛型方法
public class ArrayList<E>{}
public interface List<E>{}
public boolean add(E e){}
集合类的特点
空间自主调整,提高空间利用率,动态存储多个对象
提供不同的数据结构和算法,减少编程工作量
提高程序的处理速度和质量
备注:集合类支持引用类型,包括包装类;集合类中存放的是对象的引用,而不是对象本身。
集合类均采用泛型进行定义,分为Collection和Map两种体系
Collection接口:List(元素有序,可重复的集合) ;Set(元素无需,不可重复的集合)
Map接口:具有映射关系的”Key-value对”的集合
Collection接口方法列表:(除特殊标注外均为boolean类型)
Add(E e):向集合中添加新元素
addAll(Collection c):将指定集合中的所有元素添加到当前集合中
remove(Object o):删除当前集合中包含的指定元素
removeAll(Collection c):删除当前集合中与指定集合相同的所有元素
retainAll(Collection c):保留当前集合中与指定集合相同的所有元素
clear():删除当前集合中的所有元素 void
contains(Object o):查找当前集合中是否有指定元素
containsAll(Collection c):查找当前集合中是否包含指定集合中的所有元素
isEmpty():当前集合是否为空
size():返回当前集合的元素个数 int
iterator():返回一个可遍历当前集合的迭代器 iterator
stream():返回一个连接集合的顺序流 Stream
toArray():返回一个当前集合所有元素的数组 Object[]
List集合
List集合接口,也称之为线性表,是一个有序列表;集合中的元素是顺序存储,可以通过下标访问;List集合中允许出现重复元素;实现List集合接口的常用类:ArrayList,LinkedList,Vector和Stack.
List接口的定义:public interface List<E> extends Collection<E>
List接口中的主要方法:
Boolean add(E e)把元素加到表的尾部
Void add(int index,E e)把元素e加到表的index位置,原index位置元素顺序后移动
Boolean equals(Object o)比较对象o是否与表中的元素是同一元素
E get(int index)得到表中index位置的元素
Int indexOf(Object o)判断元素o在表中是否存在,如果不存在,则返回-1
Iterator<E> iterator()获得表的遍历器
E set(int index,E e)修改位置上的元素
使用数组方式实现List接口,检索效率很高,删除效率很低,即数据结构的顺序表
是一种动态数组,元素只能是对象,与ArrayList,LinkedList相比,线程安全,通过synchronized关键字修饰方式,实现线程安全。
编写类继承自Thread
。创建MyThread类对象时可指定循环次数n。输出从0到n-1的整数。 并在最后使用System.out.println(Thread.currentThread().getName()+" "+isAlive())
打印标识信息
裁判测试程序:
import java.util.Scanner;
/*这里放置你的答案,即MyThread类的代码*/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Thread t1 = new MyThread(Integer.parseInt(sc.next()));
t1.start();
}
}
输入样例:
3
输出样例:
0
1
2
标识信息
class MyThread extends Thread{
Integer n=3;
public MyThread(Integer n){
this.n=n;
}
public void run() {
for(int i=0;i<n;i++) {
System.out.println(i);
}
System.out.println(Thread.currentThread().getName()+" "+isAlive());
}
}
自定义线程类:class 类名 extends Thread
需要重写run方法,run方法是线程执行的内容
public class MyThread extends Thread{
public void run(){
//线程体
} }
启动线程的方法:start方法
MyThread mt=new MyThread();//创建线程对象
mt.start();//启动线程
编写类实现Runnable
接口。输出从0到n-1的整数(n在创建PrintTask对象的时候指定)。并在最后使用System.out.println(Thread.currentThread().getName());
输出标识信息。
裁判测试程序:
import java.util.Scanner;
/*这里放置你的答案,即PrintTask类的代码*/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
PrintTask task = new PrintTask(Integer.parseInt(sc.next()));
Thread t1 = new Thread(task);
t1.start();
sc.close();
}
}
输入样例:
3
输出样例:
0
1
2
标识信息
class PrintTask implements Runnable{
Integer n;
public PrintTask(Integer n) {
this.n=n;
}
public void run() {
for(int i=0;i<n;i++) {
System.out.println(i);
}
System.out.println(Thread.currentThread().getName());
}
}
public interface Runnable{
public void run();//只有一个run方法
}
class 类名 implements Runnable//自定义线程类
需要重写run方法,run方法是线程执行内容
创建线程
Runnable runnable=new MyRunnable();//创建Runnable对象
Thread thread=new Thread(runnable);
thread.start();//启动线程
import java.util.Scanner;
class MyRunnable implements Runnable{
private String name;
public MyRunnable(String name) {
this.name=name;
}
public void run() {
System.out.println("线程"+this.name+"执行开始");
try {
Thread.sleep(2000);//休眠2秒
}catch(InterruptedException e) {
e.printStackTrace();
}
System.out.print("线程"+this.name+"执行结束");
}
}
public class Main {
public static void main(String[] args) {
Runnable r1=new MyRunnable("1号");
Thread thread1=new Thread(r1);
Runnable r2=new MyRunnable("2号");
Thread thread2=new Thread(r2);
thread1.start();
thread2.start();
}
}
执行结果如下:
有Person
类,Company
类,Employee
类。
其中Employee类继承自Person类,属性为:
private Company company;
private double salary;
现在要求编写Employee类的toString
方法,返回的字符串格式为:
函数接口定义:
public String toString()
输入样例:
输出样例:
Li-35-true-MicroSoft-60000.0
public String toString(){
return super.toString()+"-"+company.toString()+"-"+salary;
}
定义Student学生类,拥有学号、姓名、性别属性,提供构造函数,以及相应属性的get set函数,提供函数attendClass(String className)表示上课。 定义CollegeStudent大学生类继承自Student类,拥有新增属性专业,提供构造函数,提供新增属性的get和set函数 定义GraduateStudent研究生类继承自CollegeStudent类,拥有新增属性导师,提供构造函数,提供新增属性的get和set函数,提供函数doResearch() 表示做研究(打印xx is doing research)。
main函数中对构造的类进行测试
输入描述:
学生类信息,学号、姓名、性别 大学生类信息,学号、姓名、性别、专业 研究生类信息,学号、姓名、性别、专业、导师
输出描述:
学生类信息 大学生类信息 研究生类信息
裁判测试程序样例:
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int no = scan.nextInt();
String name = scan.next();
String sex = scan.next();
Student s = new Student(no, name, sex);
s.print();
no = scan.nextInt();
name = scan.next();
sex = scan.next();
String major = scan.next();
CollegeStudent c = new CollegeStudent(no, name, sex, major);
c.print();
no = scan.nextInt();
name = scan.next();
sex = scan.next();
major = scan.next();
String supervisor = scan.next();
GraduateStudent g = new GraduateStudent(no, name, sex, major, supervisor );
g.print();
g.doResearch();
scan.close();
}
}
/* 你的代码被嵌在这里*/
输入样例:
在这里给出一组输入。例如:
1 liu female
2 chen female cs
3 li male sc wang
输出样例:
在这里给出相应的输出。例如:
no: 1
name: liu
sex: female
no: 2
name: chen
sex: female
major: cs
no: 3
name: li
sex: male
major: sc
supervisor: wang
li is doing research
class Student{
public int id;
public String name;
public String sex;
public Student(int id,String name,String sex) {
this.id=id;
this.name=name;
this.sex=sex;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 void attendClass(String className) {
}
public void print() {
System.out.println("no: "+this.id);
System.out.println("name: "+this.name);
System.out.println("sex: "+this.sex);
}
}
class CollegeStudent extends Student{
String major;
public CollegeStudent(int id,String name,String sex,String major){
super(id,name,sex);
this.major=major;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public void print() {
super.print();
System.out.println("major: "+this.major);
}
}
class GraduateStudent extends CollegeStudent{
String supervisor;
public GraduateStudent(int id,String name,String sex,String major,String supervisor){
super(id,name,sex,major);
this.supervisor=supervisor;
}
public String getSupervisor() {
return supervisor;
}
public void setSupervisor(String supervisor) {
this.supervisor = supervisor;
}
public void doResearch() {
System.out.println(this.name+" is doing research");
}
public void print() {
super.print();
System.out.println("supervisor: "+this.supervisor);
}
}
定义一个形状类Shape,提供计算周长getPerimeter()和面积getArea()的函数 定义一个子类正方形类Square继承自Shape类,拥有边长属性,提供构造函数,能够计算周长getPerimeter()和面积getArea() 定义一个子类长方形类Rectangle继承自Square类,拥有长、宽属性,提供构造函数,能够计算周长getPerimeter()和面积getArea() 定义一个子类圆形类Circle继承自Shape,拥有半径属性,提供构造函数,能够计算周长getPerimeter()和面积getArea()
在main函数中,分别构造三个子类的对象,并输出他们的周长、面积. 提示:用System.out.printf("%.2f",d)进行格式化输出
输入描述:
正方形类的边长 长方形类的长宽 圆类的半径
输出描述:
正方形的周长、面积 长方形的周长、面积
裁判测试程序样例:
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double length = scan.nextDouble();
Square s = new Square(length);
System.out.printf("%.2f ",s.getPerimeter());
System.out.printf("%.2f\n",s.getArea());
length = scan.nextDouble();
double wide = scan.nextDouble();
Rectangle r = new Rectangle(length,wide);
System.out.printf("%.2f ",r.getPerimeter());
System.out.printf("%.2f\n",r.getArea());
double radius = scan.nextDouble();
Circle c = new Circle(radius);
System.out.printf("%.2f ",c.getPerimeter());
System.out.printf("%.2f\n",c.getArea());
scan.close();
}
}
/* 你的代码被嵌在这里 */
输入样例:
在这里给出一组输入。例如:
1
1 2
2
输出样例:
在这里给出相应的输出。例如:
4.00 1.00
6.00 2.00
12.57 12.57
abstract class Shape{
public abstract double getPerimeter();
public abstract double getArea();
}
class Square extends Shape{
public double length;
public Square(double length) {
this.length=length;
}
public double getPerimeter() {
return 4.0*length;
}
public double getArea() {
return length*length;
}
}
class Rectangle extends Shape{//长方形类
double broad;
double length;
public Rectangle(double length,double broad) {
this.length=length;
this.broad=broad;
}
public double getPerimeter() {
return 2*length+2*this.broad;
}
public double getArea() {
return this.length*this.broad;
}
}
class Circle extends Shape{
double radius;
public Circle(double radius) {
this.radius=radius;
}
public double getPerimeter() {
return Math.PI*radius*2.0;
}
public double getArea() {
return Math.PI*radius*radius;
}
}
定义一个长方形类Rectangle,拥有长、宽属性,提供构造函数,能够计算周长getPerimeter()和面积getArea() 定义一个子类长方体类,拥有长、宽、高属性,提供构造函数,getPerimeter函数计算所有边的周长,getArea函数计算表面积,新增getVolume函数,计算体积 在main函数中,分别构造长方形类和长方体类的对象,并输出他们的周长、面积、体积,保留两位小数
输入描述:
长方形类的长、宽 长方体类的长、宽、高
输出描述:
长方形的周长和面积 长方体的周长,表面积,体积
裁判测试程序样例:
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double length = scan.nextDouble();
double wide = scan.nextDouble();
Rectangle r = new Rectangle(length,wide);
System.out.printf("%.2f ",r.getPerimeter());
System.out.printf("%.2f",r.getArea());
System.out.println();
length = scan.nextDouble();
wide = scan.nextDouble();
double height = scan.nextDouble();
Cuboid c = new Cuboid (length, wide, height);
System.out.printf("%.2f ",c.getPerimeter());
System.out.printf("%.2f ",c.getArea());
System.out.printf("%.2f",c.getVolume());
scan.close();
}
}
/* 你的代码被嵌在这里 */
输入样例:
在这里给出一组输入。例如:
1 2
1 2 3
输出样例:
在这里给出相应的输出。例如:
6.00 2.00
24.00 22.00 6.00
class Rectangle{
public double length;
public double wide;
public Rectangle(double length,double wide) {
this.length=length;
this.wide=wide;
}
public double getPerimeter() {
return 2*length+2*wide;
}
public double getArea() {
return wide*length;
}
}
class Cuboid extends Rectangle{
public double height;
public Cuboid(double length, double wide,double height) {
super(length, wide);
this.height=height;
}
public double getPerimeter() {
return 4*length+4*wide+4*height;
}
public double getArea() {
return 2*length*wide+2*wide*height+2*height*length;
}
public double getVolume() {
return length*wide*height;
}
}
设计一个BankAccount类,这个类包括:
(1)一个int型的balance变量表示账户余额。
(2)一个无参数构造方法,将账户余额初始化为0。
(3)一个带一个参数的构造方法,将账户余额初始化为输入的参数。
(4)一个int型的accountNumber变量表示开户数量,每创建一个BankAccount对象就自动加1。
(5)一个getBalance()方法:返回账户余额。
(6)一个withdraw()方法:带一个int型的amount参数,从账户余额中提取amount指定的款额。 如果amount<0或者大于当前账户余额,则输出“Invalid Amount”。
(7)一个deposit()方法:带一个int型的amount参数,将amount指定的款额存储到该银行账户上。 如果amount<0,则输出“Invalid Amount”。
裁判测试程序样例:
import java.util.Scanner;
/* 请在这里填写答案 */
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean choice1 = scanner.nextBoolean();
//是否创建对象
if (choice1==true)
{
//账户1
BankAccount myAccount1=new BankAccount();
//账户2
BankAccount myAccount2=new BankAccount(100);
//选择操作类型
int choice2 = scanner.nextInt();
switch(choice2){
case 0://存款
int depositAmount=scanner.nextInt();
myAccount2.deposit(depositAmount);
break;
case 1://取款
int withdrawAmount=scanner.nextInt();
myAccount2.withdraw(withdrawAmount);
break;
}
System.out.println(myAccount2.getBalance());
}
System.out.println(BankAccount.accountNumber);
scanner.close();
}
}
输入样例:
输入1:
true
0 20
输出样例:
120
2
输入样例:
输入2:
true
1 120
输出样例:
输出2:
Invalid Amount
100
2
输入样例:
输入3:
false
输出样例:
输出3:
0
class BankAccount{
int balance;//账户余额
static int accountNumber=0;//开户数量
public BankAccount() {
accountNumber++;
balance=0;
}
public BankAccount(int balance) {
accountNumber++;
this.balance=balance;
}
public int getBalance() {
return this.balance;
}
public int withdraw(int amount) {
if(amount<0||amount>balance) {
System.out.println("Invalid Amount");}
else {
this.balance=this.balance-amount;}
return balance;
}
public int deposit(int amount) {
if(amount<0) {
System.out.println("Invalid Amount");}
else{
this.balance=this.balance+amount;}
return balance;
}
}
1.构造一个Circle类:
1)该类有一个double型成员变量radius存放半径;
2)该类有一个有参构造方法,为成员变量radius赋值;
3)该类具有getArea和getLength两个方法,能够利用半径和Math.PI计算高精度的面积和周长。
2.构造一个Column类:
1)该类有一个Circle型成员变量bottom为圆柱体的底面;
2)该类有一个double型成员变量height存放圆柱体的高;
3)该类有getBottom和setBottom方法作为成员变量bottom的访问方法和赋值方法;
4)该类有getHeight和setHeight方法作为成员变量height的访问方法和赋值方法;
5)该类有getVolume方法,计算并返回圆柱体的体积。
裁判测试程序样例:
在这里给出函数被调用进行测试的例子。例如:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
double r=scanner.nextDouble();
double h=scanner.nextDouble();
Circle c = new Circle(r);
Column column=new Column();
column.setBottom(c);
column.setHeight(h);
System.out.printf("底面面积和周长分别为:%.2f %.2f\n",column.getBottom().getArea(),column.getBottom().getLength());
System.out.printf("体积为:%.2f\n",column.getVolume());
scanner.close();
}
}
/* 请在这里填写答案 */
输入样例:
在这里给出一组输入。例如:
10 2
输出样例:
在这里给出相应的输出。例如:
底面面积和周长分别为:314.16 62.83
体积为:628.32
class Circle {
double radius;
public Circle(double radius) {
this.radius=radius;
}
public double getArea() {
return Math.PI*radius*radius;
}
public double getLength() {
return Math.PI*radius*2.0;
}
}
class Column {
Circle bottom;
public double height;
public Circle getBottom() {
return bottom;
}
public void setBottom(Circle bottom) {
this.bottom = bottom;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getVolume(){
return height*bottom.getArea();
}
}