欢迎光临
我们一直在努力

sdut-Java面向对象-05 类和对象(函数题:23-34题)

6-23 二维向量定义及相加(Java)

裁判测试程序样例中展示的是一段二维向量类TDVector的定义以及二维向量求和的Java代码,其中缺失了部分代码,请补充完整,以保证测试程序正常运行。

函数接口定义:

提示:需要补充的成员方法有:
1. 无参构造方法
2. 带参构造方法
3. getX
4. getY
5. setX
6. setY
7. add方法

裁判测试程序样例:

import java.util.Scanner;
class TDVector {
private double x;
private double y;
public String toString() {
return "("+this.x+","+this.y+")";
}

/** 你所提交的代码将被嵌在这里(替换此行) **/

}
public class Main {
public static void main(String[] args) {
TDVector a = new TDVector(); /* 无参构造,向量的x和y默认为0 */
Scanner sc = new Scanner(System.in);
double x,y,z;
x = sc.nextDouble();
y = sc.nextDouble();
z = sc.nextDouble();
TDVector b = new TDVector(x, y); /* 按照参数构造向量的x和y */
TDVector c = new TDVector(b); /* 按照向量b构造向量 */
a.setY(z);
System.out.println(a);
System.out.println(b);
System.out.println(c);
c.setX(z);
a = b.add(c);
System.out.println(a);
System.out.println("b.x="+b.getX()+" b.y="+b.getY());
sc.close();
}
}

输入样例:

3.14 1.9 2.72

输出样例:

(0.0,2.72)
(3.14,1.9)
(3.14,1.9)
(5.86,3.8)
b.x=3.14 b.y=1.9

实现代码:

TDVector()
{
x= 0.0;
y= 0.0;
}
TDVector(double x1,double y1){
x=x1;
y=y1;
}
public TDVector(TDVector b) {
x=b.x;
y=b.y;
}
public double getX(){
return x;
}
public double getY(){
return y;
}
public void setX(double x1){
x=x1;
}
public void setY(double y1){
y=y1;
}
public TDVector add(TDVector c){
TDVector a = new TDVector();
a.x=this.x+c.x;
a.y+=this.y+c.y;
return a;
}

6-24 根据派生类写出基类(Java)

裁判测试程序样例中展示的是一段定义基类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 People() {

}
//有参
public People(String id,String name) {
this.id = id;
this.name = name;
}
//setter & getter
public void setId(String id) {
this.id = id;
}

public String getId() {
return this.id;
}

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

public String getName() {
return this.name ;
}

public void say() {
System.out.println("I'm a person! My name is " + this.name +".");
}

6-25 写出派生类构造方法(Java)

裁判测试程序样例中展示的是一段定义基类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;

6-26 Logical Operator

This program reads a line of logical expression with one logical operator and two boolean values, and evaluates the result. A logical expression is like:

true and false

The result of the expression above is: false.

The Main class and a skeleton of enum LogicalOp are provided.

函数接口定义:

enum LogicalOp {

boolean test(boolean p1, boolean p2) {
return false;
};
}

Your LogicalOp should provide and and or.

裁判测试程序样例:

public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
boolean p1 = in.nextBoolean();
String op = in.next();
boolean p2 = in.nextBoolean();
System.out.println(LogicalOp.valueOf(op).test(p1, p2));
in.close();
}
}
/* 请在这里填写答案 */

输入样例:

true and false

输出样例:

false

实现代码:

enum LogicalOp {
and{
boolean test(boolean p1, boolean p2) {
return p1&&p2;
};
},
or{
boolean test(boolean p1, boolean p2) {
return p1||p2;
};
};
boolean test(boolean p1, boolean p2) {
return false;
};
}

6-27 Int Operator

This program reads a line of prefix integer expression with one leading intergal operator and two integer values, and evaluates the result. A prefix integer expression is like:

add 5 3

The result of the expression above is: 8.

The Main class and a skeleton of enum IntOp are provided.

函数接口定义:

enum IntOp {

int eval(int i1, int i2) {
return 0;
};
}

Your IntOp should provide add and sub, which represents addition and substract resprectively.

裁判测试程序样例:

enum IntOp {
/* 请在这里填写答案 */
int eval(int i1, int i2) {
return 0;
};
}

public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String op = in.next();
int i1 = in.nextInt();
int i2 = in.nextInt();
System.out.println(IntOp.valueOf(op).eval(i1, i2));
in.close();
}
}

输入样例:

add 5 2

输出样例:

7

实现代码:

add{
int eval(int i1, int i2) {
return i1+i2;
};
},
sub{
int eval(int i1, int i2) {
return i1-i2;
};
};

6-28 TDVector

There is a class TDVector that is incompleted. Please complete the class according to the test code in Main.

函数接口定义:

class TDVector {
private double x;
private double y;
public String toString() {
return "("+this.x+","+this.y+")";
}
}

裁判测试程序样例:

import java.util.Scanner;
class TDVector {
private double x;
private double y;
public String toString() {
return "("+this.x+","+this.y+")";
}

/** 你所提交的代码将被嵌在这里(替换此行) **/

}

public class Main {
public static void main(String[] args) {
TDVector a = new TDVector(); // default ctor, x and y are zeros
Scanner sc = new Scanner(System.in);
double x,y,z;
x = sc.nextDouble();
y = sc.nextDouble();
z = sc.nextDouble();
TDVector b = new TDVector(x, y); // ctor by x and y
TDVector c = new TDVector(b); // ctor by another TDVector
a.setY(z);
System.out.println(a);
System.out.println(b);
System.out.println(c);
c.setX(z);
a = b.add(c);
System.out.println(a);
System.out.println("b.x="+b.getX()+" b.y="+b.getY());
sc.close();
}
}

输入样例:

3.14 1.9 2.72

输出样例:

(0.0,2.72)
(3.14,1.9)
(3.14,1.9)
(5.86,3.8)
b.x=3.14 b.y=1.9

实现代码:

TDVector(){

}
TDVector(double x,double y){
this.x=x;
this.y=y;
}
TDVector(TDVector A){
this.x=A.x;
this.y=A.y;
}
void setX(double z){
this.x=z;
}
void setY(double z) {
this.y=z;
}
double getX() {
return x;
}
double getY() {
return y;
}
public TDVector add(TDVector C) {
C.x=C.x+this.x;
C.y=C.y+C.y;
return C;
}

6-29 Point

There is a class Point that is incompleted. Please complete the class according to the test code in Main.

函数接口定义:

class Point {
private double x;
private double y;
public String toString() {
return "("+this.x+","+this.y+")";
}
}

裁判测试程序样例:

import java.util.Scanner;
class Point {
private double x;
private double y;
public String toString() {
return "("+this.x+","+this.y+")";
}

/** 你所提交的代码将被嵌在这里(替换此行) **/

}

public class Main {
public static void main(String[] args) {
Point a = new Point(); // default ctor, x and y are zeros
Scanner sc = new Scanner(System.in);
double x,y,z;
x = sc.nextDouble();
y = sc.nextDouble();
z = sc.nextDouble();
Point b = new Point(x, y); // ctor by x and y
Point c = new Point(b); // ctor by another Point
a.setY(z);
System.out.println(a);
System.out.println(b);
System.out.println(c);
c.setX(z);
a = b.add(c);
System.out.println(a);
System.out.println("b.x="+b.getX()+" b.y="+b.getY());
sc.close();
}
}

输入样例:

3.14 1.9 2.72

输出样例:

(0.0,2.72)
(3.14,1.9)
(3.14,1.9)
(5.86,3.8)
b.x=3.14 b.y=1.9

实现代码:

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

}
public Point(Point c){
this.x = c.x;
this.y = c.y;
}
public double getX() {
return x;
}

public void setX(double x) {
this.x = x;
}

public double getY() {
return y;
}

public void setY(double y) {
this.y = y;
}
public Point add(Point t){
Point s = (Point)t;
s.x+=this.x;
s.y+=this.y;
return s;
}

6-30 定义Person类

请定义Person类,

  • 包含两个属性(name:String, age:int),

  • 根据主方法中创建对象的形式,定义带参构造方法,

  • show()方法:输出对象的信息,格式参考例程运行结果。

  • 注意:对象的年龄在[0,150]之间,超出范围时,年龄为0。

类的定义:

class Person{} //注意不要使用public修饰

裁判测试程序样例:

import java.util.Scanner;
public class Main {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Person p1;
String name = sc.next();
int age = sc.nextInt();
p1= new Person(name,age);
p1.show();
sc.close();
}

}

/* 请在这里填写答案 */

输入样例1:

zhang
18

输出样例1:

name:zhang
age:18

输入样例2:

zhang
-5

输出样例2:

name:zhang
age:0

实现代码:

class Person{
String name;
int age;
public Person(String name, int age){
this.name = name;
this.age = age;
}
public void show(){
System.out.println("name:"+this.name);
if(age<0||age>150)
System.out.println("age:"+0);
else
System.out.println("age:"+this.age);
}
}

6-31 Implement Book

创建一个Book类。

拷贝如下的代码框架。该类定义了一些方法:获取书名、判断是否可用、借书和还书。然而,我们所提供的框架缺少这些方法的实现。请在方法体中填上合适的代码。

主方法用于测试这些方法,运行此程序,应该有如下的输出:

Title (should be The Da Vinci Code): The Da Vinci Code
Borrowed? (should be false): false
Borrowed? (should be true): true
Borrowed? (should be false): false

提示:查看主方法看每个方法如何使用,然后在每个方法中填入代码。

程序框架如下:

class Book {

String title;
boolean borrowed;

// Creates a new Book
public Book(String bookTitle) {
// Implement this method

}
// Marks the book as rented
public void rented() {
// Implement this method
}
// Marks the book as not rented
public void returned() {
// Implement this method

}
// Returns true if the book is rented, false otherwise
public boolean isBorrowed() {
// Implement this method

}
// Returns the title of the book
public String getTitle() {
// Implement this method

}
}

仔细阅读测试程序中的main()方法,根据上述的样例框架补充完整缺失的方法实现部分。

裁判测试程序样例:

public class Main {
public static void main(String[] arguments) {
// Small test of the Book class
Book example = new Book("The Da Vinci Code");
System.out.println("Title (should be The Da Vinci Code): " + example.getTitle());
System.out.println("Borrowed? (should be false): " + example.isBorrowed());
example.rented();
System.out.println("Borrowed? (should be true): " + example.isBorrowed());
example.returned();
System.out.println("Borrowed? (should be false): " + example.isBorrowed());
}
}
/* 请在这里填写答案 */

输入样例:

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

输出样例:

在这里给出相应的输出。例如:

Title (should be The Da Vinci Code): The Da Vinci Code
Borrowed? (should be false): false
Borrowed? (should be true): true
Borrowed? (should be false): false

实现代码:

class Book {

String title;
boolean borrowed;

public Book(String bookTitle)
{
this.title = bookTitle;
borrowed = false;
}

public void rented()
{
borrowed = true;
}

public void returned()
{
borrowed = false;

}
public boolean isBorrowed()
{
if(borrowed == false)
return false;
else
return true;

}

public String getTitle()
{
return title;
}
}

6-32 设计一个Circle类

构造一个Circle类,该类有一个double变量radius存放半径,该类具有getArea和getLength两个方法,能够利用半径和Math.PI计算高精度的面积和周长。

Circle类定义:

根据Main类中的main方法,编写类似如下结构的CIrcle类,并提交

class Circle{
…….;
public Circle (double r){
…….
}

public double getArea() {
…….
}
public double getLength() {
…….
}
}

裁判测试程序样例:

该段程序会自动提交

import java.util.Scanner;
public class Main {
public static void main(String[] args) {

// TODO Auto-generated method stub
Scanner scanner=new Scanner(System.in);
while(scanner.hasNextFloat()){
double r=scanner.nextDouble();
Circle c = new Circle(r);
System.out.printf("%.2f %.2f\\n",c.getArea(),c.getLength());
c.radius = 2*r;
System.out.printf("%.2f %.2f\\n",c.getArea(),c.getLength());
}
}
}

输入样例:

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

5

输出样例:

在这里给出相应的输出。例如:

78.54 31.42
314.16 62.83

实现代码:

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

public double getArea() {
return Math.PI*radius*radius;
}
public double getLength() {
return Math.PI*2*radius;
}
}

6-33 设计一个Circle类(成员变量为私有)

设计一个Circle类,其半径其半径radius设置为私有double型,初始值为1.0。其中设计一个无参的构造方法和一个参数的构造方法,get/set方法分别获取半径值和设置半径值,getArea()和grLengt()分别计算Circle的面积和周长。

Circle类模板定义:

按以下模板设计一个Circle类并提交该段代码。

class Circle{
private double radius;
public Circle() {
…….
}

public Circle (double radius){
…….
}

public double getRadius() {
…….
}

public void setRadius(double radius) {
…….
}

public double getArea() {
…….
}
public double getLength() {
…….
}
}

裁判测试程序样例:

该段程序会被自动调用
``
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
Circle circle1 = new Circle();
System.out.println(circle1.getRadius()+" "+circle1.getArea());
Circle circle2 = new Circle(25);
System.out.println(circle2.getRadius()+" "+circle2.getArea());
circle2.setRadius(100);
System.out.println(circle2.getRadius()+" "+circle2.getArea());
}
}

### 输入样例:

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

```in

输出样例:

在这里给出相应的输出。例如:

1.0 3.141592653589793
25.0 1963.4954084936207
100.0 31415.926535897932

实现代码:

class Circle{
private double radius = 1.0;
public Circle() {

}

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

public double getRadius() {
return radius;
}

public void setRadius(double radius) {
this. radius =radius;
}

public double getArea() {
return Math.PI * radius *radius;
}
public double getLength() {
return Math.PI *2*radius;
}
}

6-34 按要求编写一个类Car,练习类的定义。

编写一个类Car,根据程序需求,编写Car类的构造方法和方法。要求如下:

成员变量:carNumber,只能在Car类内访问。

方法:

setCarNumber方法:设置车牌号
重写toString方法:输出车牌信息
input方法:从键盘读入5个字符的车牌号,字符可以是数字和大小写字母,如果输入车牌不是5个字符,则重新输入。
main方法:测试Car类。在main方法中,创建了一个Car类的对象demoCar,调用input方法从键盘读入车牌号,调用该对象的setNumber方法设置车牌号,最后使用println输出该对象信息。

车牌号为长度为5的字符串,车牌号从键盘读入。

裁判测试程序样例:

在这里给出函数被调用进行测试的例子。例如:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Car.main(null);
}
}

/* 请在这里填写答案 */

输入样例:

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

12345

输出样例:

在这里给出相应的输出。例如:

My car No. is: 12345

实现代码:

class Car {
Scanner sc = new Scanner(System.in);//在成员方法外打开输入流
private String carNumber;//注意题目要求是只能在Car类中访问,所以只能是私有成员变量
Car(){}//无参构造
Car(String s){
carNumber = s;
}//带参构造(参数为车牌号)
public String input(){
String num;
num = sc.next();
while(num.length()!=5){
num = sc.next();
}
return num;
}//获取一个正确的输入的车牌号
public void setCarNumber(String n){
carNumber = n;
}//设置车牌号
public String toString(){
System.out.println("My car No. is: "+carNumber);
return null;
}//输出车牌号
public static void main(String s){
Car demoCar = new Car();
demoCar.setCarNumber(demoCar.input());
demoCar.toString();
}//主方法的主函数调用了Car类的main函数,注意是静态方法,带参构造,实参为null,再从键盘上获取一个正确的车牌号,完成题目
}

赞(0)
未经允许不得转载:171主机测评 » sdut-Java面向对象-05 类和对象(函数题:23-34题)
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址