java pta 练习题 1-4

本文探讨了面向对象编程的基本概念,通过实例展示了如何构建类、使用构造函数、重写`toString()`和`equals()`方法。此外,还涉及了三角形、圆和大数运算的相关计算。文章强调了类的设计原则以及在实际编程中应用这些原则的重要性。
摘要由CSDN通过智能技术生成

6-1 图书类 (40 分)

构建一个书类Book,包括名称(字符串),价格(整型),作者(字符串,多个作者当做一个字符串处理),版本号(整型),提供带参数的构造函数Book(String name, int price, String author, int edition),提供该类的toString()和equals()方法,toString方法返回所有成员属性的值的字符串形式,形如“name: xxx, price: xxx, author: xxx, edition: xxx”,当两个Book对象的名称(不关心大小写,无空格)、作者(不关心大小写,无空格)、版本号相同时,认为两者表示同一本书。 Main函数中,读入两本书,输出他们是否相等,打印两本书的信息。
输入描述:
两本书信息
输出描述:
两本书的打印信息 两本书是否相等

import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        Book b1 = new Book(s.next(),
                s.nextInt(),
                s.next(),
                s.nextInt());
        Book b2 = new Book(s.next(),s.nextInt(),s.next(),s.nextInt());

        System.out.println(b1);
        System.out.println(b2);
        System.out.println(b1.equals(b2));

    }

}

/* 你的代码被嵌在这里 */

答案

class Book{
    
    String name;
    int price;
    String author;
    int edition;
    
    public Book(String name,int price,String author,int edition){
        this.name=name;
        this.price=price;
        this.author=author;
        this.edition=edition;
    }//name: xxx, price: xxx, author: xxx, edition: xxx
    
    @Override
    public String toString(){
        return ("name: "+this.name+", price: "+this.price+", author: "+this.author+", edition: "+this.edition);
    }
    
    @Override
    public boolean equals(Object o){
        if(o==null)return false;
        Book b=(Book)o;
        if(b.name.equalsIgnoreCase(this.name)&&b.author.equalsIgnoreCase(this.author)&&b.edition==this.edition){
            return true;
        }else{
            return false;
        }
    }
    
    @Override
    public int hashCode(){
        int result=11;
        result=result*31+this.name.hashCode();
        result=result*31+this.author.hashCode();
        result=result*31+this.edition;
        return result;
    }
}

class BookList{
    
    Book[] list;
    int count;
    public BookList(){
        list=new Book[999];
        count=0;
    }
    
    public void addBook(Book b){
        list[count++]=b;
    }
    
    public void searchBook(Book b){
        for(int i=0;i<count;i++){
            if(list[i].equals(b)==true){
                System.out.println("found: "+i);//found: 1 
                return ;
            }
        }
        System.out.println("not found");
        return ;
    }
}

6-2 设计一个三角形Triangle类 (10 分)

设计一个三角形Triangle类。这个类包括: 两个名为width和height的double型数据域,它们分别表示三角形的底宽和高。一个为width和height指定初值的构造方法。 一个名为getArea()的方法返回这个三角形的面积。
类名和构造方法:
类名为:Triangle
构造方法为:Triangle ( double w, double h );
例如:其中 w 和 h 都是用户传入的参数。 w 表示三角形的底; h 表示三角形的高。

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();
    Triangle myTriangle = new Triangle(w, h);
    System.out.println(myTriangle.getArea());
    input.close();
  }
}

答案

class Triangle{
    double width;
    double height;
    public Triangle ( double width,double height ){
        this.width=width;
        this.height=height;
    }
    public double getArea(){
        return(0.5*this.width*this.height);
    }
}

6-3 设计一个Circle类(成员变量为私有) (10 分)

设计一个Circle类,其半径其半径radius设置为私有double型,初始值为1.0。其中设计一个无参的构造方法和一个参数的构造方法,get/set方法分别获取半径值和设置半径值,getArea()和grLengt()分别计算Circle的面积和周长。

Circle类模板定义:
按以下模板设计一个Circle类并提交该段代码。

class Circle{
    private double radius;  
    public Circle() {
         .......
    }

    public Circle (double radius){
         .......
    }

    public double getRadius() {
             .......
    }

    public void setRadius(double radius) {
             .......
    }

    public double getArea() {
         .......
    }
    public double getLength() {
         .......
    }
}
import java.util.Scanner; 
public class Main {
 public static void main(String[] args) {
 	Scanner scanner=new Scanner(System.in); 
 	Circle circle1 = new Circle();
 	System.out.println(circle1.getRadius()+" "+circle1.getArea()); 
 	Circle circle2 = new Circle(25); 
 	System.out.println(circle2.getRadius()+" "+circle2.getArea());
 	circle2.setRadius(100);
  	System.out.println(circle2.getRadius()+" "+circle2.getArea());
	} 
}

答案

class Circle{
    private double radius;  
    public Circle() {
         
    }

    public Circle (double radius){
         this.radius=radius;
    }

    public double getRadius() {
         return radius;
    }

    public void setRadius(double radius) {
             this.radius=radius;
    }

    public double getArea() {
         double A = radius*radius*Math.PI;
		return A;
    }
    public double getLength() {
         return 2*Math.PI*this.radius;
    }
}

7-1 大数整除 (10 分)

请编写程序,从键盘输入一个整数n,找出大于long.MAX_VALUE且能被n整除的前3个数字。

输入格式:
输入一个作为除数的整数n,例如: 17

输出格式:
输出大于long.MAX_VALUE且能被n整除的前3个数字,例如下列三个数能被17整除且大于long.MAX_VALUE: 9223372036854775816

9223372036854775833

9223372036854775850

import java.math.BigInteger;
import java.util.Scanner;

public class Main{
         public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        int n=input.nextInt();
        int count=0;
        BigInteger j=new BigInteger(String.valueOf(Long.MAX_VALUE));
        j=j.add(BigInteger.valueOf(1));
        while(count<3){
            if(j.mod(BigInteger.valueOf(n)).intValue()==0){
                System.out.println(j.toString());
                ++count;
                }
        j=j.add(BigInteger.valueOf(1));
        }
        input.close();
         }
}

7-2 求阶乘factorial (10 分)

编程从键盘输入一个整数,计算出阶乘并输出。
输入格式:
输入 39
输出格式:
输出20397882081197443358640281739902897356800000000

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		int num=in.nextInt();
		BigInteger big=BigInteger.valueOf(1);   //或BigInteger big = new BigInteger("1");
		for(int i=1;i<=num;i++){
			big=big.multiply(BigInteger.valueOf(i));
		}
		System.out.println(big);
	}
}

7-3 标识符命名方式转换:Camel2Snake (10 分)

在程序设计语言中,标识符都不能包含空格。如果我们需要使用两个或两个以上的单词来命名标识符,目前的工程实际中有两种常用的命名方式:Snake方式和Camel方式。 Snake方式是指单词用小写字母,单词间下划线(“_”)代替空格;Camel方式是指相邻单词首字母用大写表示。
例如,你想定义一个变量表示一个数组数字之和,并且用英文“sum of array”。我们使用Snake方式的变量名为:sum_of_array;用Camel命名方式的变量名为:sumOfArray。
现在请你将一个Camel方式命名的变量,转换成Snake方式命名的变量。
【数据范围】 输入字符串长度不超过255,只包含小写字母和大写字母,第一个和最后一个字符不可能是大写字母,且保证没有两个连续的大写字母出现。

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine();
		//将该字符串变成字符数组(不能使用toCharArray()方法)
		char[] ch = new char[str.length()];
		for(int x = 0;x < str.length();x++) {
			ch[x] = str.charAt(x);
			//将字符数组中的所有大写字母变成小写字母           
			if(ch[x] >= 'A' && ch[x] <= 'Z') {
				ch[x] += 32;
		    	System.out.print("_"+ch[x]);
			}
			else{
			    System.out.print(ch[x]);
			}
		}
	}
}

7-4 日期加减 (10 分)

请编程从键盘输入一个长整型的值,该值表示从1970年1月1日算起的一个特定时间(毫秒数),以此时间构造一个日期对象。再输入一个普通整型值,该值表示天数,加上该天数后,然后输出对应的年、月、日。

输入格式:
1234567898765 (第一行输入一个长整型数)
158 (第二行输入一个普通整型数,表示天数)

import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		GregorianCalendar gc=new GregorianCalendar();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		long millis=in.nextLong();
		Long day=in.nextLong();
		Long ms=day*24*60*60*1000+millis;  //将天数转换成毫秒数
		gc.setTimeInMillis(millis);
		System.out.println(sdf.format(gc.getTime()));//抽象类Calendar的getTime()方法返回一个Date对象
		gc.setTimeInMillis(ms);
		System.out.println(sdf.format(gc.getTime()));
	}
}
  • 6
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值