Java final sprint review (Part I)

Bald bald bald bald 2022-02-13 07:15:28 阅读数:238

java final sprint review

One 、 Classes and objects

  1. Subject requirements :
    Use this Call the existing parameterized constructor ,width And length Respectively 5 and 6.
    by Rectangle Class coverage toString. according to width= Actual width value ,length= Format output of actual length value
public Rectangle(){

this(5,6);
}
public Rectangle(int width, int length) {

this.width = width;
this.length = length;
}
public String toString(){

return "width=" + width + ",length=" + length;
}
  1. Design a rectangular class Rectangle (20 branch )
    Design a project called Rectangle The class represents a rectangle . This class includes : Two are called width and height Of double Type data field , They represent the width and height of the rectangle, respectively .width and height The default values of are 1. A nonparametric construction method . One for width and height Rectangle construction method of specified value . A group called getArea() Returns the area of the rectangle . A group called getPerimeter() The method returns the perimeter of the rectangle .
import java.util.Scanner;
/* Your code will be embedded here */
public class Main {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);
double w = input.nextDouble();
double h = input.nextDouble();
Rectangle myRectangle = new Rectangle(w, h);
System.out.println(myRectangle.getArea());
System.out.println(myRectangle.getPerimeter());
input.close();
}
}

 Insert picture description here

class Rectangle{

double width = 1,height = 1;
public Rectangle() {
}
public Rectangle(double width,double height) {

this.width = width;
this.height = height;
}
public double getArea() {

return width*height;
}
public double getPerimeter() {

return (width+height)*2;
}
}
  1. Book Class design (20 branch )
    Read the test procedure , To design a Book class .
     Insert picture description here
    Sample referee test procedure :

import java.util.*;
public class Main {

public static void main(String[] args) {

List <Book>books=new ArrayList<Book>();
Scanner in=new Scanner(System.in);
for(int i=0;i<5;i++)
{
 String str=in.nextLine();
String []data=str.split(",");
Book book=new Book(data[0],Integer.parseInt(data[1]),data[2],Integer.parseInt(data[3]));
books.add(book);
}
System.out.println(totalprice(books));
}
/* Calculate all book Total price of */
public static int totalprice(List <Book>books)
{
 int result=0;
for(int i=0;i<books.size();i++){
result+=books.get(i).getPrice();}
return result;
}
}
/* Please fill in the answer here */

 Insert picture description here

class Book{

private String name,author;
private int price,date;
public String getName() {

return name;
}
public String getAuthor() {

return author;
}
public int getDate() {

return date;
}
public int getPrice() {

return price;
}
public void setName(String name) {

this.name = name;
}
public void setAuthor(String author) {

this.author = author;
}
public void setDate(int date) {

this.date = date;
}
public void setPrice(int price) {

this.price = price;
}
public Book(String name, int price,String author, int date) {

this.name = name;
this.author = author;
this.date = date;
this.price = price;
}
}
  1. Override parent method equals (20 branch )
    In the class Student Rewriting in Object Class equals Method . send Student Object student number (id) It is determined as the same object when it is the same .
     Insert picture description here
    Sample referee test procedure :
import java.util.Scanner;
class Student {

int id;
String name;
int age;
public Student(int id, String name, int age) {

this.id = id;
this.name = name;
this.age = age;
}
/* Please fill in the answer here */
}
public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
Student s1 = new Student(sc.nextInt(),sc.next(),sc.nextInt());
Student s2 = new Student(sc.nextInt(),sc.next(),sc.nextInt());
System.out.println(s1.equals(s2));
sc.close();
}
}

 Insert picture description here

@Override
public boolean equals(Object obj) {

Student std = (Student) obj;
if(this.id == std.id) {

return true;
}else
return false;
}
  1. To design a BankAccount class (20 branch )
    To design a BankAccount class , This class includes :
    (1) One int Type balance Account balance at the time of statement .
    (2) A nonparametric construction method , Initialize the account balance to 0.
    (3) A construction method with one parameter , Initialize the account balance as the input parameter .
    (4) One getBlance() Method , Return account balance .
    (5) One withdraw() Method : With a amount Parameters , And withdraw... From the account balance amount Specified amount .
    (6) One deposit() Method : With a amount Parameters , And will amount Deposit the specified amount into the bank account .
    To design a Main Class to test , Enter the account balance separately 、 Withdrawal limit and deposit limit , And output the account balance .
     Insert picture description here
import java.io.ObjectInputStream.GetField;
import java.util.Scanner;
class BankAccount{

int balance;
public int getBalance() {

return balance;
}
public BankAccount() {

this.balance = 0;
}
public BankAccount(int balance) {

this.balance = balance;
}
public int withdraw(int amount) {

return balance-=amount;
}
public int deposit(int amount) {

return balance+=amount;
}
}
public class Main{

public static void main(String[] args) {

Scanner input = new Scanner(System.in);
int balance,amount1,amount2;
balance = input.nextInt();
int balance1 = balance;
amount1 = input.nextInt();
amount2 = input.nextInt();
BankAccount bankAccount = new BankAccount(balance);
int result1 = bankAccount.withdraw(amount1);
int result2 = bankAccount.deposit(amount2);
System.out.println(balance1 + "\n" + result1 + "\n" + result2);
}
}

Two 、 Inheritance and polymorphism

  1. Write out the base class according to the derived class (Java) (10 branch )
    The referee test program example shows a section defining the base class People、 Derived class Student And test the correlation between the two classes Java Code , Part of the code is missing , Please complete , To ensure the normal operation of the test program .
     Insert picture description here
    Sample referee test procedure :
    Be careful : The data used in the real test program may be different from that in the sample test program , But only call the relevant methods according to the format in the example ( function ).
class People{

protected String id;
protected String name;
/** The code you submit will be embedded here ( Replace this line ) **/
}
class Student extends People{

protected String sid;
protected int score;
public Student() {

name = "Pintia Student";
}
public Student(String id, String name, String sid, int score) {

super(id, name);
this.sid = sid;
this.score = score;
}
public void say() {

System.out.println("I'm a student. My name is " + this.name + ".");
}
}
public class Main {

public static void main(String[] args) {

Student zs = new Student();
zs.setId("370211X");
zs.setName("Zhang San");
zs.say();
System.out.println(zs.getId() + " , " + zs.getName());
Student ls = new Student("330106","Li Si","20183001007",98);
ls.say();
System.out.println(ls.getId() + " : " + ls.getName());
People ww = new Student();
ww.setName("Wang Wu");
ww.say();
People zl = new People("370202", "Zhao Liu");
zl.say();
}
}

 Insert picture description here

 public People(String id,String name) {

this.id = id;
this.name = name;
}
public People() {

}
public String getId() {

return id;
}
public String getName() {

return name;
}
public void setId(String id) {

this.id = id;
}
public void setName(String name) {

this.name = name;
}
public void say() {

System.out.println("I'm a person! My name is " + getName() + ".");
}
  1. From abstract classes shape Class extends a circular class Circle (10 branch )
    Please select the following abstract classes shape Class extends a circular class Circle, The radius of this quasi circle radius As a private member , Class should contain the constructor of initialization radius .
    Sample referee test procedure :
import java.util.Scanner;
import java.text.DecimalFormat;
abstract class shape {
// abstract class 
/* Abstract method Find the area */
public abstract double getArea( );
/* Abstract method Seek perimeter */
public abstract double getPerimeter( );
}
/* The code you submit will be embedded here */
public class Main {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);
DecimalFormat d = new DecimalFormat("#.####");// Retain 4 Decimal place 
double r = input.nextDouble( );
shape c = new Circle(r);
System.out.println(d.format(c.getArea()));
System.out.println(d.format(c.getPerimeter()));
input.close();
}
}

 Insert picture description here

class Circle extends shape{

private double radius;
final double PI = 3.1415926535;
public double getRadius() {

return radius;
}
public Circle(double radius) {

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

this.radius = radius;
}
@Override
public double getArea() {

return PI * radius * radius;
}
@Override
public double getPerimeter() {

return 2 * PI * radius;
}
}
  1. Book and audio-visual rental (10 branch )
    Book and video stores provide rental services , Including books and DVD Rental of . The book includes the title (String, One word means ) And the price (double),DVD Including the title (String, One word means ). They are all rented by day , But the rent is calculated differently , The daily rent of books is... Of the book price 1%,DVD The daily rent is fixed 1 element . Construct books and DVD Class inheritance system , They all inherit from Media class , And provide methods getDailyRent() Return the daily rent , Construct audio and video stores MediaShop, Provide static functions double calculateRent(Media[] medias, int days). stay main Function is constructed Media Array , Including books and DVD The object of , call calculateRent Method to get and output the rent , Keep two decimal places
     Insert picture description here
    Sample referee test procedure :
import java.util.Scanner;
public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Media[] ms = new Media[n];
for (int i=0; i<n; i++) {

String type = sc.next();
if (type.equals("book")) {

ms[i] = new Book(sc.next(), sc.nextDouble());
}else {

ms[i] = new DVD(sc.next());
}
}
double rent = MediaShop.calculateRent(ms, sc.nextInt());
System.out.printf("%.2f", rent);
}
}
/* Please fill in the answer here */

 Insert picture description here

class Book extends Media{

String name;
double price;
Book(String name,double price) {

this.name = name;
this.price = price;
}
@Override
public double getDailyRent() {

return price*0.01;
}
}
class Media{

public double getDailyRent() {

return 0;
}
}
class MediaShop{

public static double calculateRent(Media[] medias,int days) {

double sum = 0;
for (int i = 0; i < medias.length; i++) {

sum += medias[i].getDailyRent()*days;
}
return sum;
}
}
class DVD extends Media{

String name;
public DVD(String name) {

this.name = name;
}
@Override
public double getDailyRent() {

return 1;
}
}
  1. To design a Triangle Class inherits from GeometricObject class (10 branch )
    Design a project called Triangle To extend GeometricObject class . This category includes :
    ■ Three are called side1、side2 and side3 Of double The data field represents the three sides of the triangle , Their default value is 1.0.
    ■ A parameterless construction method creates the default triangle .
    ■ One can create a tape formulation side1、side2 and side3 The construction method of triangle .
    ■ Accessor methods for all three data fields .
    ■ A group called getArea() Returns the area of the triangle .
    ■ A group called getPerimeter() Returns the perimeter of the triangle .
    ■ A group called toString() The method of returns the string description of the triangle .
    toString() The method is implemented as follows :
    return “Triangle: side1 = " + side1 + " side2 = " + side2 +” side3 = " + side3;
    Write a test program , Prompt the user to enter the three sides of the triangle 、 Color and a Boolean A value indicates whether the triangle is filled . The program should use input to create one with these edges and set color and filled Attribute triangle . The program should show the area 、 Perimeter and side length .
    The system will automatically submit Main Classes and GeometricObject class .
    Sample referee test procedure :
//Main Test class 
public class Main{

public static void main(String[] args) {

Scanner input = new Scanner(System.in);
double side1 = input.nextDouble();
double side2 = input.nextDouble();
double side3 = input.nextDouble();
Triangle triangle = new Triangle(side1, side2, side3);
String color = input.next();
triangle.setColor(color);
boolean filled = input.nextBoolean();
triangle.setFilled(filled);
System.out.println("The area is " + triangle.getArea());
System.out.println("The perimeter is "
+ triangle.getPerimeter());
System.out.println(triangle);
}
}
//GeometricObject class 
class GeometricObject {

private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
public GeometricObject() {

dateCreated = new java.util.Date();
}
public GeometricObject(String color, boolean filled) {

dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
public String getColor() {

return color;
}
public void setColor(String color) {

this.color = color;
}
public boolean isFilled() {

return filled;
}
public void setFilled(boolean filled) {

this.filled = filled;
}
public java.util.Date getDateCreated() {

return dateCreated;
}
@Override
public String toString() {

return "created on " + dateCreated + "\ncolor: " + color + "and filled: " + filled;
}
}
/* Please fill in the answer here */

 Insert picture description here

class Triangle extends GeometricObject{

private double side1 = 1.0,side2 = 1.0,side3 = 1.0;
public Triangle() {

}
public Triangle(double side1, double side2, double side3) {

this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public double getSide1() {

return side1;
}
public double getSide2() {

return side2;
}
public double getSide3() {

return side3;
}
public void setSide1(double side1) {

this.side1 = side1;
}
public void setSide2(double side2) {

this.side2 = side2;
}
public void setSide3(double side3) {

this.side3 = side3;
}
public double getArea() {

double p = (side1 + side2 + side3) / 2;
return Math.sqrt(p*(p-side1)*(p-side2)*(p-side3));
}
public double getPerimeter() {

return side1 + side2 + side3;
}
@Override
public String toString() {

return "Triangle: side1=" + side1 + " side2=" + side2 +" side3=" + side3;
}
}
copyright:author[Bald bald bald bald],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130715255976.html