Java语言程序设计(第3版)沈泽刚主编第10,11,12章课后习题答案
Posted nuist__NJUPT
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java语言程序设计(第3版)沈泽刚主编第10,11,12章课后习题答案相关的知识,希望对你有一定的参考价值。
Java语言程序设计(第3版)沈泽刚主编第10,11,12章课后习题答案
第10章 接口和Lambda表达式
10.1 定义Duck类实现Swimmable接口和flyable接口。
interface Swimmable{
public void swim() ;
}
interface flyable{
public void fly() ;
}
public class Duck implements Swimmable, flyable{
@Override
public void swim() {
System.out.println("I can swimming") ;
}
@Override
public void fly() {
System.out.println("I can fly") ;
}
public static void main(String[] args){
Duck duck = new Duck() ;
duck.fly() ;
duck.swim() ;
Swimmable s = duck ; //向上转换,自动类型转换
s.swim() ;
Duck d = (Duck)s ; //向下转换,强制类型转换
d.fly() ;
d.swim() ;
}
}
10.2 定义一个名为RandomIntSequence的类实现IntSequence接口。
interface IntSequence{
public boolean hasNext() ;
public int next() ;
}
public class RandomIntSequence implements IntSequence{
private int n ;
@Override
public boolean hasNext() {
n = (int)(Math.random() * 90 + 10) ;
return true;
}
@Override
public int next() {
return n;
}
public static void main(String[] args){
RandomIntSequence r = new RandomIntSequence() ;
if(r.hasNext()){
System.out.println(r.next()) ;
}
}
}
10.3 设计一个名为SequenceTest的类,在其中编写一个static方法用于计算一个整数n的平均值
public class SequenceTest {
public static double average(IntSequence seq, int n){
double sum = 0.0 ;
int m = n ;
while(seq.hasNext() && m > 0){
sum += seq.next() ;
m -- ;
}
return sum / n ;
}
public static void main(String[] args){
RandomIntSequence r = new RandomIntSequence() ;
double ave = average(r, 10) ;
System.out.println(ave) ;
}
}
10.5 修改Employee的定义,让根据员工年龄进行比较
public class Employee implements Comparable<Employee>{
int id ;
int age ;
double salary ;
String name ;
public Employee(int id, String name, int age, double salary){
this.id = id ;
this.age = age ;
this.salary = salary ;
this.name = name ;
}
@Override
public int compareTo(Employee employee) {
return employee.age - this.age ;
}
public String toString(){
return "id = " + id + " name = " + name + " age = " + age + " salary = " + salary ;
}
public static void main(String[] args){
Employee [] employees = new Employee[]{new Employee(1, "张三", 12, 2200), new Employee(2, "李四", 23, 2300)} ;
if(employees[0].compareTo(employees[1]) <= 0){
System.out.println(employees[0]) ;
System.out.println(employees[1]) ;
}else{
System.out.println(employees[1]) ;
System.out.println(employees[0]) ;
}
}
}
10.6 用compareTo()方法计算连个面积之差
public class Circle implements Comparable<Circle>{
double radius ;
public Circle(double radius){
this.radius = radius ;
}
public double getArea(){
return Math.PI * radius * radius ;
}
@Override
public int compareTo(Circle circle) {
return (int)(circle.getArea() - getArea());
}
public static void main(String[] args){
Circle [] circles = new Circle[]{new Circle(1.0), new Circle(2.0), new Circle(3.0)} ;
System.out.println(circles[0].compareTo(circles[1])) ;
}
}
10.7 设计一个Position类,该类有两个成员变量x,y表示坐标。
public class Position implements Comparable<Position> {
int x ;
int y ;
public Position(int x, int y){
this.x = x ;
this.y = y ;
}
@Override
public int compareTo(Position o) {
double dist1 = Math.sqrt(x*x + y*y) ;
double dist2 = Math.sqrt(o.x*o.x +o.y*o.y) ;
return (int)(dist1 - dist2);
}
public static void main(String[] args){
Position position1 = new Position(1, 2) ;
Position position2 = new Position(2,4) ;
System.out.println(position1.compareTo(position2)) ;
}
}
11.8 实现Student对象按姓名排序。
import java.util.Arrays;
import java.util.Comparator;
class Comp implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
if(o1.name.compareTo(o2.name) > 0){
return 1 ;
}else if(o1.name.compareTo(o2.name) < 0){
return -1 ;
}else{
return 0 ;
}
}
}
public class Student implements Comparable<Student>{
int id ;
String name ;
public Student(int id, String name){
this.id = id ;
this.name = name ;
}
@Override
public int compareTo(Student o) {
return this.id - o.id;
}
public String toString(){
return "id = " + id + " name = " + name ;
}
public static void main(String[] args){
Student [] student = new Student[]{new Student(1,"李四"), new Student(2, "张三"), new Student(3, "王五")} ;
Arrays.sort(student, new Comp()) ;
for(Student s : student){
System.out.println(s) ;
}
}
}
10.9 编写程序,对字符串数组进行降序排序。
import java.util.Arrays;
import java.util.Comparator;
public class DescSort implements Comparator<String> {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
public static void main(String[] args){
String [] s = new String[] {"google", "bai du", "sou hu"} ;
Arrays.sort(s, new DescSort()) ;
for(String s1 : s){
System.out.println(s1) ;
}
}
10.12 使用Lambda表达式实现calculate()方法。
interface Calculate{
public double calculate(double a, double b) ;
}
public class CalculatorDemo {
public static void main(String[] args){
Calculate c = (a, b) -> Math.pow(a,2) + Math.pow(b,2) ;
int a = 10 ;
int b = 20 ;
System.out.println(c.calculate(a,b)) ;
}
}
第11章 泛型与集合
11.1
public class Point<T> {
T x, y ;
public Point(T x, T y){
this.x = x ;
this.y = y ;
}
public void setX(T x){
this.x = x ;
}
public T getX(){
return x ;
}
public void setY(T y){
this.y = y ;
}
public T getY(){
return y ;
}
public void translate(T x, T y){
this.x = x ;
this.y = y ;
}
public String toString(){
return "x = " + x + " y = " + y ;
}
public static void main(String[] args){
Point<Integer> point1 = new Point<>(1, 2) ;
Point<Double> point2 = new Point<>(3.0,4.0) ;
point1.translate(5, 6);
System.out.println(point1) ;
System.out.println(point2) ;
}
}
11.4 创建一个ArrayList对象,在其中添加若干元素,
编写程序,使用3种方法将每个字符转换成大写。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class UpperCaseDemo {
public static void main(String[] args){
List<String> list = new ArrayList<>() ;
String [] s = {"google", "bai du", "zi jie"} ;
for(int i=0; i<s.length; i++){
list.add(s[i]) ;
}
for(int i=0; i<list.size(); i++){
String name = list.get(i) ;
name = name.toUpperCase() ;
list.set(i, name);
}
System.out.println(list) ;
List<String> list1 = Arrays.asList(s) ;
Iterator iterator = list1.iterator() ;
while(iterator.hasNext()){
String name = (String)iterator.next() ;
name = name.toUpperCase() ;
System.out.print(name + " ") ;
}
list1.replaceAll(String::toUpperCase);
System.out.println(list1) ;
}
}
11-5 编写程序,将一个字符串中的单词解析出来,然后将它们添加到一个HashSet中, 并输出每个重复的单词,不同单词的个数,消除重复单词后的列表。
import java.util.HashSet;
import java.util.Set;
以上是关于Java语言程序设计(第3版)沈泽刚主编第10,11,12章课后习题答案的主要内容,如果未能解决你的问题,请参考以下文章
Java语言程序设计(第3版)沈泽刚主编第6,7,8章课后习题答案
Matlab程序设计与应用(第3版,刘卫国主编)课后习题参考答案