Java语⾔程序设计(第3版)沈泽刚主编第6,7,8章课后习题答案Java语⾔程序设计(第3版)沈泽刚主编第6,7,8章课后习题答案
第6章 字符串
6.1 编写程序,提⽰⽤户输⼊⼀个字符串,显⽰它的长度,第⼀个字符和最后⼀个字符。
import Scanner;
public class StringDemo {
public static void main(String[] args){
Scanner input =new Scanner(System.in);
System.out.print("请输⼊⼀个字符串:");
String s = ();
System.out.println("字符串的长度为:"+ s.length());
System.out.println("第1个字符:"+ s.charAt(0));
System.out.println("最后⼀个字符:"+ s.charAt(s.length()-1));
}
}
6.2 编写程序,提⽰⽤户输⼊两个字符串,检测第⼆个字符串是否是第⼀个字符串的⼦串。
import Scanner;
public class SubStringDemo {
public static void main(String[] args){
Scanner input =new Scanner(System.in);
System.out.print("请输⼊第1个字符串:");
String s1 = Line();
System.out.print("请输⼊第2个字符串:");
String s2 = Line();
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 Scanner;
public class ToBinary {
public static String toBinary(int value){
StringBuilder s =new StringBuilder("");
while(value >0){
s.append(value %2);
value /=2;
}
return new String(s);
}
public static void main(String[] args){
Scanner input =new Scanner(System.in);
System.out.print("请输⼊⼀个正整数:");
int num = Int();
System.out.println("⼗进制数"+ num +"对应的⼆进制数为:"+toBinary(num)); }
}
6.6 使⽤⽅法签名,返回排好序的字符串。
import 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 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 = Line();
System.out.println("加密后的字符串:"+encrypt(s));
}
}
6.8 为上⼀题编写⼀个解密程序,输⼊密⽂,输出明⽂。
import Scanner;
public class Encrypt1 {
public static void main(String[] args){
Scanner input =new Scanner(System.in);
System.out.print("请输⼊已经加密的字符串:");
String s = Line();
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 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(){java switch case string
return radius ;
}
public double getArea(){
return Math.PI * radius * radius ;
}
public double getPerimeter(){
return2* 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 = Double();
System.out.print("请输⼊圆柱的⾼:");
double height = Double();
Cylinder cylinder =new Cylinder(radius, height);
System.out.println("圆柱的表⾯积:"+ Area());
System.out.println("圆柱的体积:"+ Volume());
}
}
7.3 定义⼀个汽车类Auto,定义⼀个Auto的⼦类Bus,编写程序测试Bus类的使⽤。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论