6-1 设计一个矩形类Rectangle
分数 10
设计一个名为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 ,height;
public Rectangle() {
width = 1;
height = 1;
}
public Rectangle(double width, double height) {
super();
this.width = width;
this.height = height;
}
double getArea() {
return width * height;
}
double getPerimeter() {
return (width + height) * 2;
}
}
6-2 创建一个直角三角形类实现IShape接口
分数 10
单位 西安邮电大学创建一个直角三角形类(regular triangle)RTriangle类,实现下列接口IShape。两条直角边长作为
###直角三角形类的定义:
直角三角形类的构造函数原型如下: 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
package Java面向对象程序设计3面向对象基础;
import java.util.Scanner;
import java.text.DecimalFormat;
interface IShape {// 接口
public abstract double getArea(); // 抽象方法 求面积
public abstract double getPerimeter(); // 抽象方法 求周长
}
public class JJ6_2 {
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();
}
}
class RTriangle implements IShape {
private double a;
private double b;
public RTriangle (double a ,double b) {
super();
this.a = a;
this.b = b;
}
@Override
public double getArea() {
// TODO Auto-generated method stub
return this.a * this.b / 2.0;
}
@Override
public double getPerimeter() {
// TODO Auto-generated method stub
return this.a + this.b + Math.sqrt(this.a * this.b + this.b * this.b);
}
}
6-3 jmu-Java-03面向对象基础-clone方法、标识接口、深拷贝
分数 10
Object的clone
方法可以帮助我们克隆对象。现在需编写一个类Car
包含:
1.属性:
private String name;
private CarDriver driver;
private int[] scores;
2.无参构造函数
public Car() {
}
3.方法:
@Override
public String toString() {
return "Car [name=" + name + ", driver=" + driver + ", scores=" + Arrays.toString(scores) + "]";
}
setter/getter方法与clone
方法。注意:clone
方法需实现对象的深度克隆。
CarDriver
为已经定义好的类,部分代码如下:
class CarDriver {
private String name;
public CarDriver() {}
//setter/getter
//toString
}
/* 请在这里填写答案,即Car类的完整代码 */
package Java面向对象程序设计3面向对象基础;
import java.util.Arrays;
class CarDriver {
@SuppressWarnings("unused")
private String name;
public CarDriver() {}
//setter/getter
//toString
public Object getName() {
// TODO Auto-generated method stub
return null;
}
public void setName(Object name2) {
// TODO Auto-generated method stub
}
}
class Car implements Cloneable{
private String name;
private CarDriver driver;
private int[] scores;
public Car(){
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setScores(int [] scores){
this.scores = scores;
}
public int[] getScores(){
return scores;
}
public void setDriver(CarDriver driver){
this.driver = driver;
}
public CarDriver getDriver(){
return driver;
}
@Override
public String toString() {
return "Car [name=" + name + ", driver=" + driver + ", scores=" + Arrays.toString(scores) + "]";
}
public Car clone() throws CloneNotSupportedException{
Car car = new Car();
CarDriver cd = new CarDriver();
if(driver == null){
cd = null;
}else{
cd.setName(driver.getName());
}
car.setDriver(cd);
car.setName(name);
if(scores == null){
car.setScores(null);
}else{
int [] arr = Arrays.copyOf(scores, scores.length);
car.setScores(arr);
}
return car;
}
}
public class JJ6_3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
6-8 sdust-Java-可实现多种排序的Book类
分数 10
裁判测试程序样例:
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Book[] books = new Book[4];
//1. 从键盘接收用户输入的4本书的名称(仅有英文字符构成)、出版日期(格式:1998-10-09)、价格,生成Book对象,构造包含4本书的数组
for(int i=0;i<4;i++){
String name = scan.next();
String date_str = scan.next();
Date date = null;
//将键盘录入的日期字符串转换为Date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
date = sdf.parse(date_str);
} catch (ParseException e) {
System.out.println("日期格式有误");;
}
double price = Double.parseDouble(scan.next());
Book book = new Book(name, date, price);
books[i] = book;
}
//2.将books按照出版日期降序排序;然后输出books
Arrays.sort(books, new BookComparatorByPubDate());
for(Book book:books){
System.out.println(book);
}
//3.将books按照价格升序排序,如果价格相同,则按照书名字母顺序排列。然后输出books
Arrays.sort(books, new BookComparatorByPrice());
for(Book book:books){
System.out.println(book);
}
scan.close();
}
}
/* 请在这里填写答案 */
输入样例:
Java
2011-08-01
29
Python
2014-01-01
48
C
2004-09-08
17.5
DataBase
2012-09-17
17.5
输出样例:
书名:Python,定价:48.0
书名:DataBase,定价:17.5
书名:Java,定价:29.0
书名:C,定价:17.5
书名:C,定价:17.5
书名:DataBase,定价:17.5
书名:Java,定价:29.0
书名:Python,定价:48.0
package Java面向对象程序设计3面向对象基础;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class JJ6_4 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Book[] books = new Book[4];
//1. 从键盘接收用户输入的4本书的名称(仅有英文字符构成)、出版日期(格式:1998-10-09)、价格,生成Book对象,构造包含4本书的数组
for(int i=0;i<4;i++){
String name = scan.next();
String date_str = scan.next();
Date date = null;
//将键盘录入的日期字符串转换为Date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
date = sdf.parse(date_str);
} catch (ParseException e) {
System.out.println("日期格式有误");
}
double price = Double.parseDouble(scan.next());
Book book = new Book(name, date, price);
books[i] = book;
}
//2.将books按照出版日期降序排序;然后输出books
Arrays.sort(books, new BookComparatorByPubDate());
for(Book book:books){
System.out.println(book);
}
//3.将books按照价格升序排序,如果价格相同,则按照书名字母顺序排列。然后输出books
Arrays.sort(books, new BookComparatorByPrice());
for(Book book:books){
System.out.println(book);
}
scan.close();
}
}
/* 请在这里填写答案 */
class Book {
public String name;
public Date publishDate;
public double price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getPublishDate() {
return publishDate;
}
public void setPublishDate(Date publishDate) {
this.publishDate = publishDate;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Book(String name, Date publishDate, double price) {
this.name = name;
this.publishDate = publishDate;
this.price = price;
}
@Override
public String toString() {
return "书名:"+this.name+",定价:"+this.price;
}
}
class BookComparatorByPubDate implements Comparator<Book> {
@Override
public int compare(Book b1, Book b2) {
return -(b1.publishDate.compareTo(b2.publishDate));
}
}
class BookComparatorByPrice implements Comparator<Book> {
@Override
public int compare(Book b1, Book b2) {
if ((b1.price - b2.price) > 0) {
return 1;
} else if ((b1.price - b2.price) < 0) {
return -1;
} else {
return (b1.name.compareTo(b2.name));
}
}
}
6-9 Book类的设计
分数 10
阅读测试程序,设计一个Book类。
函数接口定义:
class Book{}
该类有 四个私有属性
分别是 书籍名称
、 价格
、 作者
、 出版年份
,以及相应的set 与get方法;该类有一个含有四个参数的构造方法,这四个参数依次是书籍名称
、 价格
、 作者
、 出版年份
。
裁判测试程序样例:
import java.util.*;
public class Main {
public static void main(String[] args) {
List <Book>books=new ArrayList<Book>();
Scanner in=new Scanner(System.in);
for(int i=0;i<5;i++)
{ String str=in.nextLine();
String []data=str.split(",");
Book book=new Book(data[0],Integer.parseInt(data[1]),data[2],Integer.parseInt(data[3]));
books.add(book);
}
System.out.println(totalprice(books));
}
/*计算所有book的总价*/
public static int totalprice(List <Book>books)
{ int result=0;
for(int i=0;i<books.size();i++){result+=books.get(i).getPrice();}
return result;
}
}
/* 请在这里填写答案 */
输入样例:
三体,100,无名氏,1998
上下五千年,50,编辑部,2015
海底世界,50,无名氏2,2000
三体1,100,无名氏3,2017
三体3,100,无名氏4,1998
输出样例:
400
为啥在编译器里会报错呢T_T
package Java面向对象程序设计3面向对象基础;
import java.util.*;
public class JJ6_5 {
public static void main(String[] args) {
List<Book> books = new ArrayList<Book>();
@SuppressWarnings("resource")
Scanner in = new Scanner(System.in);
for (int i = 0; i < 5; i++) {
String str = in.nextLine();
String[] data = str.split(",");
Book book = new Book(data[0], Integer.parseInt(data[1]), data[2], Integer.parseInt(data[3]));
books.add(book);
}
System.out.println(totalprice(books));
}
/* 计算所有book的总价 */
public static int totalprice(List<Book> books) {
int result = 0;
for (int i = 0; i < books.size(); i++) {
result += books.get(i).getPrice();
}
return result;
}
}
/* 请在这里填写答案 */
class Book {
private String name = null;
private int price = 0;
private String author = null;
private int year = 0;
public Book(String name ,int price ,String author ,int year) {
this.name = name;
this.price = price;
this.author = author;
this.year = year;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
}