Java语言程序设计(第3版)沈泽刚主编第6,7,8章课后习题答案
第6章 字符串
6.1 编写程序,提示用户输入一个字符串,显示它的长度,第一个字符和最后一个字符。
import java.util.Scanner;
public class StringDemo {
public static void main(String[] args){
Scanner input = new Scanner(System.in) ;
System.out.print("请输入一个字符串:") ;
String s = input.next() ;
System.out.println("字符串的长度为:" + s.length()) ;
System.out.println("第1个字符:" + s.charAt(0)) ;
System.out.println("最后一个字符:" + s.charAt(s.length()-1)) ;
}
}
6.2 编写程序,提示用户输入两个字符串,检测第二个字符串是否是第一个字符串的子串。
import java.util.Scanner;
public class SubStringDemo {
public static void main(String[] args){
Scanner input = new Scanner(System.in) ;
System.out.print("请输入第1个字符串:") ;
String s1 = input.nextLine() ;
System.out.print("请输入第2个字符串:") ;
String s2 = input.nextLine() ;
System.out.println("字符串" + s2 + (s1.indexOf(s2) >= 0 ? "是" : "不是")+ "字符串" + s1 + "的子串") ;
}
}
6.4 使用下面方法签名编写一个方法,统计字符串中包含字母的个数。
public class CountLetters {
public static int countLetters(String s){
int count = 0 ;
for(int i=0; i<s.length(); i++){
if(Character.isLetter(s.charAt(i))){
count ++ ;
}
}
return count ;
}
public static void main(String[] args){
String s = "1adAA24e%$#@R" ;
System.out.println("字符串" + s + "含有" + countLetters(s) + "个字母") ;
}
}
6.5 编写方法将10进制整数转换为2进制整数。
import java.util.Scanner;
public class ToBinary {
public static String toBinary(int value){
StringBuilder s = new StringBuilder("") ;
while(value > 0){
s.append(value % 2) ;
value /= 2 ;
}
s.reverse() ;
return new String(s) ;
}
public static void main(String[] args){
Scanner input = new Scanner(System.in) ;
System.out.print("请输入一个正整数:") ;
int num = input.nextInt() ;
System.out.println("十进制数" + num + "对应的二进制数为:" + toBinary(num)) ;
}
}
6.6 使用方法签名,返回排好序的字符串。
import java.util.Arrays;
public class Sort {
public static String sort(String s){
char [] c = s.toCharArray() ;
Arrays.sort(c) ;
return String.valueOf(c) ;
}
public static void main(String[] args){
String s = "morning" ;
System.out.println(sort(s)) ;
}
}
6.7 编写一个加密程序,要求键盘输入一个字符串,输出加密的字符串。
import java.util.Scanner;
public class Encrypt {
public static String encrypt(String s){
StringBuilder ss = new StringBuilder(s) ;
for(int i=0; i<ss.length(); i++){
char c = ss.charAt(i) ;
if(s.charAt(i) == 'Z' || s.charAt(i) == 'z'){
c = (char)(c - 25) ;
}else{
c = (char)(c + 1) ;
}
ss.setCharAt(i,c) ;
}
return new String(ss) ;
}
public static void main(String[] args){
Scanner input = new Scanner(System.in) ;
System.out.print("请输入一个待加密的字符串:") ;
String s = input.nextLine() ;
System.out.println("加密后的字符串:" + encrypt(s)) ;
}
}
6.8 为上一题编写一个解密程序,输入密文,输出明文。
import java.util.Scanner;
public class Encrypt1 {
public static void main(String[] args){
Scanner input = new Scanner(System.in) ;
System.out.print("请输入已经加密的字符串:") ;
String s = input.nextLine() ;
StringBuilder ss = new StringBuilder(s) ;
for(int i=0; i<ss.length(); i++){
char c = ss.charAt(i) ;
if(ss.charAt(i) == 'A' || ss.charAt(i) == 'a'){
c = (char) (c + 25) ;
}else{
c = (char) (c - 1) ;
}
ss.setCharAt(i,c) ;
}
System.out.println("解密后的明文为:" + ss) ;
}
}
6.9 将字符串“no pains,no gains.“解析成含有4个单词的字符串数组。
public class SplitDemo {
public static void main(String[] args){
String s = new String("no pains,no gins.") ;
String [] array = s.split("[ ,.]") ;
for(int i=0; i<array.length; i++){
System.out.print(array[i] + " ") ;
}
}
}
第7章 继承与多态
7.1 根据Animal类及其子类的继承关系,编写方法实现这些类。
class Bird extends Animal{
public int numberOfWings ;
public void fly(){
System.out.println("I can fly") ;
}
}
class Fish extends Animal{
public int numberOfFins ;
public void swim(){
System.out.println("I can swim") ;
}
}
class Dog extends Animal{
public int numberOfLegs ;
public void walk(){
System.out.println("I can walk") ;
}
}
public class Animal {
public double weight ;
public void eat(){
System.out.println("I can eat anything") ;
}
public static void main(String[] args){
Animal animal = new Animal() ;
animal.eat() ;
Bird bird = new Bird() ;
bird.fly();
bird.eat() ;
}
}
7.2 定义一个名为Cylinder的圆柱类,继承Circle类,求圆柱的表面积和体积。
import java.util.Scanner;
class Circle{
double centerX ;
double centerY ;
public double radius ;
public Circle(double radius){
this.radius = radius ;
}
public void setRadius(double radius){
this.radius = radius ;
}
public double getRadius(){
return radius ;
}
public double getArea(){
return Math.PI * radius * radius ;
}
public double getPerimeter(){
return 2 * Math.PI * radius ;
}
}
public class Cylinder extends Circle {
double height ;
public Cylinder(double radius, double height) {
super(radius);
this.height = height ;
}
@Override
public double getArea(){
return getPerimeter() * height ;
}
public double getVolume(){
return getArea() * height ;
}
public static void main(String[] args){
Scanner input = new Scanner(System.in) ;
System.out.print("请输入圆柱的底面的半径:") ;
double radius = input.nextDouble() ;
System.out.print("请输入圆柱的高:") ;
double height = input.nextDouble() ;
Cylinder cylinder = new Cylinder(radius, height) ;
System.out.println("圆柱的表面积:" + cylinder.getArea()) ;
System.out.println("圆柱的体积:" + cylinder.getVolume()) ;
}
}
7.3 定义一个汽车类Auto,定义一个Auto的子类Bus,编写程序测试Bus类的使用。
class Bus extends Auto{
public int passenger ;
public void gotOn(int passenger){
this.passenger += passenger ;
System.out.println("车上上了" + passenger + "名乘客," + "现在车上有" + this.passenger + "名乘客") ;
}
public void getOff(int passenger){
this.passenger -= passenger ;
System.out.println("车上下了" + passenger + "名乘客," + "现在车上有" + this.passenger + "名乘客") ;
}
}
public class Auto {
public double speed ;
public void start(){
System.out.println("汽车启动了") ;
}
public void speedUp(double speed){
this.speed = speed ;
System.out.println("汽车加速到" + speed) ;
}
public void stop(){
this.speed = 0 ;
System.out.println("汽车停止运行") ;
}
public static void main(String[] args){
Bus bus = new Bus() ;
bus.start() ;
bus.speedUp(60.0) ;
bus.gotOn(20) ;
bus.getOff(10) ;
bus.stop() ;
}
}
7.4 定义一个名为Square类表示正方形,使其继承Shape抽象类,覆盖Shape中的抽象方法。
abstract class Shape{
public String name ;
public Shape(){}
public Shape(String name){
this.name = name ;
}
public abstract double getPerimeter() ;
public abstract double getArea() ;
}
public class Square extends Shape{
double side ;
public Square(double side){
this.side = side ;
}
@Override
public double getPerimeter() {
return side * 4 ;
}
@Override
public double getArea() {
return side * side ;
}
public static void main(String[] args){
Square square = new Square(2.0) ;
square.name = "正方形" ;
System.out.println(square.name + "的周长:" + square.getPerimeter()) ;
System.out.println(square.name + "的面积:" + square.getArea()) ;
}
}
7.5 定义名为Cuboid的长方体类,继承长方形类Rectangle,编写程序,求长宽高分别为10,5,2的长方体的体积。
class Rectangle{
public double length ;
public double width ;
public Rectangle(double length, double width){
this.length = length ;
this.width = width ;
}
public double getArea(){
return length * width ;
}
public double getPerimeter(){
return (length + width) * 2 ;
}
}
public class Cuboid extends Rectangle{
public double height ;
public Cuboid(double length, double width,double height) {
super(length, width);
this.height = height ;
}
public double getVolume(){
return getArea() * height ;
}
public static void main(String[] args){
Cuboid cuboid = new Cuboid(10, 5, 2) ;
System.out.println("长方体的体积为:" + cuboid.getVolume()) ;
}
}
第8章 Java常用核心类
8.1 定义一个名为Square的类表示正方形,编写一个程序测试clone(),equals(),toString()等方法的使用。
public class Square implements Cloneable{
public double length ;
public Square(double length){
this.length = length ;
}
public void setLength(double length){
this.length = length ;
}
public double getLength(){
return length ;
}
public boolean equals(Square square){
return square.length == this.length ;
}
@Override
public String toString(){
return "Square[length=" + length + "]" ;
}
public static void main(String[] args) throws CloneNotSupportedException{
Square square = new Square(100 );
Square square1 = (Square)square.clone() ;
System.out.println(square1.toString()) ;
System.out.println(square.equals(square1)) ;
}
}
8.2编写程序,随机生成1000个16之间的整数,统计16每个数出现的概率,修改程序,生成1000个随机数,并统计概率,比较结果并给出结论。
public class RandomTest {
public static void main(String[] args){
int [] count1 = new int [6] ;
int [] count2 = new int [6] ;
for(int i=0; i<100; i++){
int r = (int)(Math.random() * 6) + 1 ;
switch(r){
case 1 : count1[0] ++ ; break ;
case 2 : count1[1] ++ ; break ;
case 3 : count1[2] ++ ; break ;
case 4 : count1[3] ++ ; break ;
case 5 : count1[4] ++ ; break ;
case 6 : count1[5] ++ ; break ;
}
}
for(int i=0; i<1000; i++){
int r = (int)(Math.random() * 6) + 1 ;
switch(r){
case 1 : count2[0] ++ ; break ;
case 2 : count2[1] ++ ; break ;
case 3 : count2[2] ++ ; break ;
case 4 : count2[3] ++ ; break ;
case 5 : count2[4] ++ ; break ;
case 6 : count2[5] ++ ; break ;
}
}
for(int i=0; i<count1.length; i++){
System.out.print(count1[i] + " ") ;
}
System.out.println() ;
for(int i=0; i<count2.length; i++){
System.out.print(count2[i] + " ") ;
}
}
}
8.3 有一个三角形的两条边长分别为4.0和5.0,夹角为30°,编写程序计算该三角形的面积。
public class AreaTest {
public static void main(String[] args){
System.out.println(0.5 * 4 * 5 * Math.sin(Math.toRadians(30))) ;
}
}
8.4 编写程序,输出6中数值型包装类的最大值和最小值。
public class NumberTest {
public static void main(String[] args){
System.out.println(Byte.MAX_VALUE + " " + Byte.MIN_VALUE) ;
System.out.println(Short.MAX_VALUE + " " + Short.MIN_VALUE) ;
System.out.println(Integer.MAX_VALUE + " " + Integer.MIN_VALUE) ;
System.out.println(Long.MAX_VALUE + " " + Long.MIN_VALUE) ;
System.out.println(Float.MAX_VALUE + " " + Float.MIN_VALUE) ;
System.out.println(Double.MAX_VALUE + " " + Double.MIN_VALUE) ;
}
}
8.6程序员的生日
import java.time.LocalDate;
public class Birthday {
public static void main(String[] args){
LocalDate birth = LocalDate.of(2017,1,1).plusDays(255) ;
System.out.println("2017年程序员的生日为:" + birth) ;
}
}
8.7 编写程序,计算您从出生到现在已经过去了多少天?
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class Days {
public static void main(String[] args){
LocalDate birth = LocalDate.of(1996, 7, 25) ;
LocalDate day = LocalDate.now() ;
long days = ChronoUnit.DAYS.between(birth, day) ;
System.out.println(days) ;
}
}