第四章习题集(1)
函数
jmu-Java-03面向对象基础-覆盖与toString
public String toString(){
return super.toString()+"-"+company+"-"+salary;
}
重写父类方法equals
public boolean equals(Student s2){
if(this.id==s2.id)
return true;
else return false;
}
编程
7-2 jmu-Java-01入门-取数字浮点数
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
while(true){
String s=in.nextLine();
int sum=0;
for(int i=0;i<s.length();i++){
if(s.charAt(i)>='0'&&s.charAt(i)<='9')
sum+=s.charAt(i)-'0';
}
System.out.println(sum);
}
}
}
7-3 无聊的小明来数1
import java.util.Scanner;
public class Main{
public static void main(String []args){
Scanner in=new Scanner(System.in);
int T=in.nextInt();
int[] tArr = new int[T];
for(int i = 0;i < T;i++){
tArr[i] = getNum(in.nextInt());
System.out.println(tArr[i]);
}
}
public static int getNum(int num){
int count=0;
int number;
while(num != 0){
number = num % 2;
if(number == 1)
count++;
num/=2;
}
return count;
}
}
7-5 教师类
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
int no1=in.nextInt();
String name1=in.next();
int age1=in.nextInt();
String seminary1=in.next();
Teacher t1=new Teacher(no1,name1,age1,seminary1);
int no2=in.nextInt();
String name2=in.next();
int age2=in.nextInt();
String seminary2=in.next();
Teacher t2=new Teacher(no2,name2,age2,seminary2);
System.out.println(t1.toString());
System.out.println(t2.toString());
System.out.println(t1.equals(t2));
}
}
class Teacher{
private int no,age;
private String name,seminary;
public Teacher(int no,String name,int age,String seminary){
this.no=no;
this.name=name;
this.age=age;
this.seminary=seminary;
}
public boolean equals(Teacher t){
if(this.no==t.no)
return true;
else return false;
}
public String toString(){
return "no: "+no+", name:"+name+", age: "+age+", seminary: "+seminary;
}
}
第四章习题集(2)
7-1 声明图书类,记录图书总册数,利用静态变量赋值
public class Main{
public static void main(String[] args){
Book b1=new Book("Java程序设计",34.5);
b1.print();
Book b2=new Book("数据结构",44.8);
b2.print();
Book b3=new Book("C++程序设计",35.0);
b3.print();//注意打印顺序
System.out.println("图书总册数为:"+b3.sum);
}
}
class Book{
private final String name;
static int num=0;
private final double price;
static int sum=0;
Book(String name,double price){
this.name=name;
this.price=price;
num++;
sum++;
}
void print(){
System.out.println("书名:"+name+", 书号:"+num+", 书价:"+price);
}
}
7-2 定义类2
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
double dd = RR.fun();
System.out.printf("%.2f",dd);
}
}
class RR{
public static int fun(){
Scanner in=new Scanner(System.in);
int[] n = new int[5];
for(int i=0;i<5;i++) n[i] = in.nextInt();
return n[2];
}
}
第四章习题集(3)
6-1 从抽象类shape类扩展出一个圆形类Circle
class Circle extends shape{
private double radius;
public Circle(double r){
radius=r;
}
public double getArea(){
return Math.PI*radius*radius;
}
public double getPerimeter(){
return 2*Math.PI*radius;
}
}
6-2 创建一个直角三角形类实现IShape接口
class RTriangle implements IShape{
private double a,b;
RTriangle(double a, double b){
this.a=a;
this.b=b;
}
public double getArea(){
return a*b*0.5;
}
public double getPerimeter(){
return a+b+Math.pow((Math.pow(a,2)+Math.pow(b,2)),0.5);
}
}
7-1 jmu-Java-03面向对象基础-04-形状-继承
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
int n=in.nextInt();
in.nextLine();
Shape []arr=new Shape[n];
for(int i=0;i<n;i++){
String s=in.next();
in.nextLine();
if(s.equals("rect")){
int width=in.nextInt();
int length=in.nextInt();
in.nextLine();
arr[i]=new Rectangle(width,length);
}
else if(s.equals("cir")){
int radius=in.nextInt();
in.nextLine();
arr[i]=new Circle(radius);
}
}
System.out.println(sumAllPerimete(arr));
System.out.println(sumAllArea(arr));
System.out.println(Arrays.toString(arr));
for(int i=0;i<n;i++){
System.out.println(arr[i].getClass()+","+arr[i].getClass().getSuperclass());
}
}
public static double sumAllArea(Shape[] arr){
double sumarea=0;
for(int i=0;i<arr.length;i++){
sumarea+=arr[i].getArea();
}
return sumarea;
}
public static double sumAllPerimete(Shape[] arr){
double sumperimeter=0;
for(int i=0;i<arr.length;i++){
sumperimeter+=arr[i].getPerimeter();
}
return sumperimeter;
}
}
abstract class Shape{
final public double PI=3.14;
public abstract double getPerimeter();
public abstract double getArea();
}
class Rectangle extends Shape{
private int width,length;
public Rectangle(int width,int length){
this.width=width;
this.length=length;
}
public String toString(){
return "Rectangle [width="+width+", length="+length+"]";
}
public double getPerimeter(){
return 2*(width+length);
}
public double getArea(){
return width*length;
}
}
class Circle extends Shape{
private int radius;
public Circle(int radius){
this.radius=radius;
}
public String toString(){
return "Circle [radius="+radius+"]";
}
public double getPerimeter(){
return 2*PI*radius;
}
public double getArea(){
return PI*radius*radius;
}
}
7-2 集体评分
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] grade = new int[5];
for(int i=0; i<grade.length; i++){
grade[i] = in.nextInt();
}
RR rr = new RT(grade);
double dd = rr.mark();
System.out.printf("%.2f",dd);
}
}
abstract class RR{
int[] grade;
public RR(int[] grade){
this.grade = grade;
}
public abstract double mark();
}
class RT extends RR{
private double sum=0;
public RT(int[] grade) {
super(grade);
}
public double mark(){
for(int i=1;i<4;i++){
sum+=grade[i];
}
return sum/3;
}
}
7-3 定义ClassName接口,设计类Company,实现获取类名称的功能
interface ClassName{
public abstract String getClassName();
}
class Company implements ClassName{
public Company(){}
public String getClassName(){
return "Company";
}
}
public class Main{
public static void main(String[] args){
Company c=new Company();
System.out.println(c.getClassName());
}
}
7-4 集体评分2
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] grade = new int[5];
for(int i=0; i<grade.length; i++){
grade[i] = in.nextInt();
}
RR rr = new RT(grade);
double dd = rr.mark();
System.out.printf("%.2f",dd);
}
}
interface RR{
double mark();
}
class RT implements RR{
int[] grade;
public double sum=0;
public RT(int[] grade){
this.grade = grade;
}
public double mark(){
for(int i=1;i<4;i++){
sum+=grade[i];
}
return sum/3;
}
}
7-5 接口--四则计算器
import java.util.Scanner;
interface ICompute{
public abstract int computer(int n,int m);
}
class Add implements ICompute{
public Add(){}
public int computer(int n,int m){
return n+m;
}
}
class Sub implements ICompute{
public Sub(){}
public int computer(int n,int m){
return n-m;
}
}
public class Main{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
int n=in.nextInt();
int m=in.nextInt();
Add a=new Add();
Sub s=new Sub();
System.out.println(a.computer(n,m));
System.out.println(s.computer(n,m));
}
}
7-6 USB接口的定义
interface USB{
public abstract void work();
public abstract void stop();
}
class Mouse implements USB{
public Mouse(){}
public void work(){
System.out.println("我点点点");
}
public void stop(){
System.out.println("我不能点了");
}
}
class Upan implements USB{
public Upan(){}
public void work(){
System.out.println("我存存存");
}
public void stop(){
System.out.println("我走了");
}
}
public class Main{
public static void main(String[] args){
USB usb1=new Mouse();
usb1.work();
usb1.stop();
USB []usb2=new USB[2];
usb2[0]=new Upan();
usb2[1]=new Mouse();
for(int i=0;i<2;i++){
usb2[i].work();
usb2[i].stop();
}
}
}
第五章习题集
6-1 jmu-Java-06异常-finally
System.out.println("resource open success");
}
catch(Exception e){
System.out.println(e);
}
try{
resource.close();
System.out.println("resource release success");
}
catch(RuntimeException e){
System.out.println(e);
}
6-2 jmu-Java-06异常-多种类型异常的捕获
分数 10
catch(NumberFormatException e){
System.out.println("number format exception");
System.out.println(e);
}
catch(IllegalArgumentException e){
System.out.println("illegal argument exception");
System.out.println(e);
}
catch(Exception e){
System.out.println("other exception");
System.out.println(e);
}
6-3 求圆面积自定义异常类
class Circle{
private double r;
Circle(double r){
this.r=r;
}
public double area() throws CircleException{
if(this.r<0){
CircleException e=new CircleException(r);
throw e;
}
return 3.14*r*r;
}
}
class CircleException extends Exception{
double radius;
public CircleException(double r){
this.radius=r;
}
public void print(){
System.out.println("圆半径为"+radius+"不合理");
}
}
7-1 jmu-Java-06异常-01-常见异常
分
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
int []arr=new int[5];
while(true){
String s=in.next();
try{
if(s.equals("arr")){
int index=in.nextInt();
int x=arr[index];
}
else if(s.equals("null")){
throw new NullPointerException();
}
else if(s.equals("cast")){
Object obj=new String("hhh");
Integer num=(Integer)obj;//记不住啊啊啊啊//记住啦
}
else if(s.equals("num")){
String str=in.next();
int num1=Integer.parseInt(str);
}
else break;
}
catch(Exception e){
System.out.println(e);
}
}
}
}
7-2 jmu-Java-06异常-02-使用异常机制处理异常输入
import java.util.Scanner;
import java.util.Arrays;
public class Main{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
int n=in.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++){
try{
a[i]=Integer.parseInt(in.next());
}
catch(Exception e){
System.out.println(e);
i--;
}
}
System.out.println(Arrays.toString(a));
}
}
7-4 成绩录入时的及格与不及格人数统计
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
int n=in.nextInt();
int pass=0,unpass=0;
for(int i=0;i<n;){
try{
int score=in.nextInt();
if(score>=60&&score<=100){
pass++;
i++;
}
else if(score<60&&score>=0){
unpass++;
i++;
}
else{
throw new ScoreOutOfRoundException(score);
}
}
catch(ScoreOutOfRoundException e){
System.out.println(e.toString());
}
}
System.out.println(pass);
System.out.println(unpass);
}
}
class ScoreOutOfRoundException extends Exception{
int score;
public ScoreOutOfRoundException(int score){
this.score=score;
}
public String toString(){
return score+"invalid!";
}
}
7-5 设计一个Tiangle异常类
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double s1 = input.nextDouble();
double s2 = input.nextDouble();
double s3 = input.nextDouble();
try {
Triangle t = new Triangle(s1,s2,s3);
System.out.println(t);
}
catch (IllegalTriangleException ex) {
System.out.println(ex.getMessage());
}
}
}
class Triangle{
private double side1,side2,side3;
public Triangle(double side1, double side2, double side3) throws IllegalTriangleException {
this.side1=side1;
this.side2=side2;
this.side3=side3;
if(side1+side2<=side3||side2+side3<=side1||side1+side3<=side2){
IllegalTriangleException e=new IllegalTriangleException(side1,side2,side3);
throw e;
}
}
public String toString(){
return "Triangle [side1=" + side1 + ", side2=" + side2 + ", side3=" + side3 + "]";
}
}
class IllegalTriangleException extends Exception{
private double side1,side2,side3;
public IllegalTriangleException(double side1,double side2,double side3){
this.side1=side1;
this.side2=side2;
this.side3=side3;
}
public String getMessage(){
return "Invalid: "+side1+","+side2+","+side3;
}
}
7-5 自定义异常类:成绩异常(ScoreException)
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
Student zhangsan=new Student();
double score=in.nextDouble();
try{
zhangsan.setScore(score);
System.out.println("成绩为"+zhangsan.getScore());
}
catch(ScoreException e){
e.show();
}
finally{
System.out.println("程序结束");
}
}
}
class ScoreException extends Exception{
private String message;
public ScoreException(){
message="您输入的成绩异常,请核实!";
}
public void show(){
System.out.println(message);
}
}
class Student{
private double score;
public Student(){}
public void setScore(double s) throws ScoreException{
this.score=s;
if(score<0||score>100){
ScoreException e=new ScoreException();
throw e;
}
}
public double getScore(){
return score;
}
}
第六章习题集
7-2 单词替换
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
String s=in.nextLine();
String inquire=in.nextLine();
String replace=in.nextLine();
Replace r=new Replace();
r.rep(s,inquire,replace);
}
}
class Replace{
public void rep(String s,String inquire,String replace){
String[] m=s.split(" ");
for(int i=0;i<m.length;i++){
if(m[i].equals(inquire))
m[i]=replace;
}
for(int i=0;i<m.length;i++){
if(i!=m.length-1)
System.out.print(m[i]+" ");
else System.out.print(m[i]);
}
}
}
7-3 图书价格汇总
import java.util.*;
public class Main{
static int sum=0;
public static void main(String[] args){
Scanner in=new Scanner(System.in);
String s=in.nextLine();
String []m=s.split(";");
for(int i=0;i<m.length;i++){
for(int j=0;j<m[i].length();j++){
if(m[i].charAt(j)>='0'&&m[i].charAt(j)<='9'){
sum+=(m[i].charAt(j)-'0')*10+m[i].charAt(j+1)-‘0’;/*char类型运算与数形运算不同,char运算用到的是ACII码,两个字符相减实际上是ACII码对应的数相减,str.charAt(i)是一个char型,减去'0',就相当于把字符转成数字*/
break;
}
}
}
for(int i=0;i<m.length;i++){
System.out.println(m[i]);
}
System.out.println("总价格为"+sum);
}
}
7-4 通过键盘输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
String s=in.nextLine();
int zimu=0,shuzi=0,kongge=0,other=0;
for(int i=0;i<s.length();i++){
if((s.charAt(i)>='a'&&s.charAt(i)<='z')||(s.charAt(i)>='A'&&s.charAt(i)<='Z'))
zimu++;
else if(s.charAt(i)>='0'&&s.charAt(i)<='9')
shuzi++;
else if(s.charAt(i)==' ')
kongge++;
else other++;
}
System.out.println("字母个数:"+zimu);
System.out.println("数字个数:"+shuzi);
System.out.println("空格个数:"+kongge);
System.out.println("其他字符个数:"+other);
}
}
7-6 判断登录信息是否正确--字符串比较
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
String usename=in.next();
String password=in.next();
if(usename.equals("SwpuIot")){
if(password.equals("123456"))
System.out.println("Welcome");
else System.out.println("Mismatch");
}
else System.out.println("NotExist");
}
}
7-8 伪随机数
import java.util.Scanner;
import java.util.Random;
public class Main{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
int n=in.nextInt();
int m=in.nextInt();
int k=in.nextInt();
int count=0;
Random r=new Random(k);
while(true){
int randomNum=r.nextInt(m);
count++;
if(count==n){
System.out.println(randomNum);
break;
}
}
}
}
7-7 sdut-String-3 字符串字母大小写转换逆序输出
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
String s=in.nextLine();
for(int i=s.length()-1;i>=0;i--){
if(s.charAt(i)>='a'&&s.charAt(i)<='z'){
System.out.print((char)(s.charAt(i)-32));
}
else if(s.charAt(i)>='A'&&s.charAt(i)<='Z'){
System.out.print((char)(s.charAt(i)+32));
}
else System.out.print(s.charAt(i));
}
}
}
7-9 sdut-String-4 去除字符串中数字字符逆序输出
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
String s=in.nextLine();
for(int i=s.length()-1;i>=0;i--){
if(!(s.charAt(i)>='0'&&s.charAt(i)<='9'))
System.out.print(s.charAt(i));
}
}
}
当想对分开的字符串进行循环时,循环条件为s.length
当想对字符进行循环时,循环条件为s.length()
第九章习题集
7-3 计算正五边形的面积和周长
import java.util.*;
import java.text.*;
public class Main{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
double a=in.nextDouble();
double perimeter=0,area=0;
perimeter=a*5;
area=a*a*Math.sqrt(25+10*Math.sqrt(5))/4;
DecimalFormat df=new DecimalFormat("#.####");
System.out.println(df.format(area));
System.out.println(df.format(perimeter));
}
}
第十章习题集
函数
6-1 jmu-Java-07多线程-Thread
class MyThread extends Thread{
int n;
public MyThread(int n){
this.n=n;
}
public void run(){
for(int i=0;i<n;i++){
System.out.println(i);
}
System.out.println(Thread.currentThread().getName()+" "+isAlive());
}
}
6-2 jmu-Java-07多线程-PrintTask
class PrintTask implements Runnable{
int n;
public PrintTask(int n){
this.n=n;
}
public void run(){
for(int i=0;i<n;i++){
System.out.println(i);
}
System.out.println(Thread.currentThread().getName());
}
}
编程
7-1 创建一个倒数计数线程
import java.util.Scanner;
class ReverseTime implements Runnable{
int n;
public ReverseTime(int n){
this.n=n;
}
public void run(){
for(int i=n;i>=0;i--){
System.out.println(i);
}
}
}
public class Main{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
int n=in.nextInt();
ReverseTime r=new ReverseTime(n);
r.run();
}
}
7-2 程序改错题4
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x = in.nextInt();
for(int i=0;i<x;i++){
Thread t = new Thread(new RunHandler());
t.start();
}
}
}
class RunHandler extends Thread {
public void run() {
System.out.println("run");
}
}
7-3 试试多线程
class A extends Thread{
int sum;
public A(){
sum=0;
}
public void run(){
for(int i=1;i<=25;i++){
sum+= i;
}
}
public int getSum(){
return sum;
}
}
class B extends Thread{
int sum;
public B(){
sum=0;
}
public void run(){
for(int i=26;i<=50;i++){
sum+= i;
}
}
public int getSum(){
return sum;
}
}
class C extends Thread{
int sum;
public C(){
sum=0;
}
public void run(){
for(int i=51;i<=75;i++){
sum+= i;
}
}
public int getSum(){
return sum;
}
}
class D extends Thread{
int sum;
public D(){
sum=0;
}
public void run(){
for(int i=76;i<=100;i++){
sum+= i;
}
}
public int getSum(){
return sum;
}
}
public class Main{
public static void main(String[] args){
A a=new A();
B b=new B();
C c=new C();
D d=new D();
a.start();
b.start();
c.start();
d.start();
try {
c.sleep(1000);
}
catch(Exception e1){
}
System.out.println(a.getSum()+b.getSum()+c.getSum()+d.getSum());
}
}