Computer 2019-1 / 2 / 3 / 4 - Experiment 5

Posted by billf2007 on Sun, 03 Oct 2021 03:30:05 +0200

1. Customizable sorting rectangle

Enter the number n representing the number of rectangles from the keyboard, then enter the length and width of N rectangles, then sort the N rectangles according to the area from large to small, and output the area of each rectangle after sorting.
Requirements: please design the Rectangle class, including the corresponding constructor and member function, and implement the Comparable interface

Enter Description:

Number of rectangles, length and width of each rectangle

Output Description:

The area of each rectangle sorted from large to small

Example of referee test procedure:

import java.util.Comparator;
import java.util.Arrays;
import java.util.Scanner;

/*Your code is embedded here*/

public class Main {
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        //Enter the number of rectangles
        int num_rectangle = scan.nextInt();
        Rectangle[]  recs = new Rectangle[num_rectangle];
        //Enter the length and width of each rectangle
        for(int i=0;i<num_rectangle;i++){
            int length = scan.nextInt();
            int width = scan.nextInt();
            Rectangle rec = new Rectangle(length,width);
            recs[i] = rec;
        }
        //Sort by area from large to small
        Arrays.sort(recs);
        //Area of the first n-1 rectangle before printing
        for(int i=0;i<recs.length-1;i++){
            System.out.print(recs[i].getArea()+",");
        }
        //Prints the area of the last rectangle
        System.out.print(recs[recs.length-1].getArea());
        scan.close();
    }
}

Input example:

A set of inputs is given here. For example:

3 1 2 3 4 2 3

Output example:

The corresponding output is given here. For example:

12,6,2

Own code:

class Rectangle implements Comparable<Rectangle>{
	
	int width;
	int height;
	Rectangle(int w,int h){
		width=w;
		height=h;
	}
	int getArea() {return width*height;}
	
	
	@Override
	public int compareTo(Rectangle o) {

		return -(this.getArea()-o.getArea());
	}
}






2. Books

Build a Book class book, including

  • Name (string), price (integer), author (string, multiple authors are treated as one string), version number (integer),
  • Provide the constructor Book(String name, int price, String author, int edition) with parameters
  • toString() and equals() methods of this class are provided. toString method returns the string form of the values of all member properties, such as "name: xxx, price: xxx, author: xxx, edition: xxx"
  • When two Book objects have the same name (regardless of case, no space), author (regardless of case, no space) and version number, they are considered to represent the same Book.

In the Main function, read in two books, output whether they are equal, and print the information of the two books.

Enter Description:

Two book information

Output Description:

Are the printed information of the two books equal

Example of referee test procedure:

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));

    }

}

/* Your code is embedded here */

Input example:

A set of inputs is given here. For example:

ThinkingInJava
86
BruceEckel
4
CoreJava
95
CayS.Horstmann
10

Output example:

The corresponding output is given here. For example:

name: ThinkingInJava, price: 86, author: BruceEckel, edition: 4
name: CoreJava, price: 95, author: CayS.Horstmann, edition: 10
false

Own code:

class Book{
	String name;
	int price;
	String author;
	int edition;
	Book(String name, int price, String author, int edition){
		this.name=name;
		this.price=price;
		this.author=author;
		this.edition=edition;
	}
	
	public String toString() {
		String str=new String();
		
		return "name: "+name+", price: "+price+", author: "+author+", edition: "+edition;
	}
	
	public boolean equals(Object o) {
		Book b=(Book)o;
		if(b.name.equalsIgnoreCase(this.name) && b.author.equalsIgnoreCase(this.author) && b.edition==this.edition)
			return true;
		else 
			return false;
	}
}






3.jmu-Java-04 advanced object oriented - 02 interface Comparator

Arrays.sort can sort all objects that implement Comparable. However, if there are multiple sorting requirements, for example, sometimes the name needs to be sorted in descending order, and sometimes only the age needs to be sorted. Using Comparable cannot meet such requirements. Different comparators can be written to meet various sorting requirements.

#1. Write the PersonSortable2 class

  • Attributes: private name(String), private age(int)
  • Parameterized constructor: the parameters are name and age
  • toString method: returns the format name age

#2 write Comparator class

  • Write the NameComparator class to sort names in ascending order
  • Write the AgeComparator class to sort the ages in ascending order

#3. In the main method

Enter n
Enter n rows of name and age, and create n objects into the array
Sort the array in ascending order by name and output it.
On the basis of 3, the array is sorted in ascending order according to age and then output.
The last two lines use the following code to output all the interfaces implemented by NameComparator and AgeComparator.

System.out.println(Arrays.toString(NameComparator.class.getInterfaces()));
System.out.println(Arrays.toString(AgeComparator.class.getInterfaces()));

Input example:

A set of inputs is given here. For example:

5
zhang 15
zhang 12
wang 14
Wang 17
li 17

Output example:

The corresponding output is given here. For example:

NameComparator:sort
Wang-17
li-17
wang-14
zhang-15
zhang-12
AgeComparator:sort
zhang-12
wang-14
zhang-15
Wang-17
li-17
//The last two lines are identification information

Own code:

import java.util.Comparator;
import java.util.Arrays;
import java.util.Scanner;

class PersonSortable2{
	private String name;
	private int age;
	PersonSortable2(String name,int age){
		this.name=name;
		this.age=age;
	}
	
	public String getName() {
		return name;
	}

	public int getAge() {
		return age;
	}

	public String toString() {
		return this.name+"-"+this.age;
	}

}
class NameComparator implements Comparator<PersonSortable2>{

	@Override
	public int compare(PersonSortable2 a,PersonSortable2 b) {
		if(a.getName().compareTo(b.getName())>0)return 1;
		else if(a.getName().compareTo(b.getName())==0)return 0;
		else return -1;
		
	}
}

class AgeComparator implements Comparator<PersonSortable2>{

	@Override
	public int compare(PersonSortable2 a, PersonSortable2 b) {
		if(a.getAge()-b.getAge()>0)return 1;
		else if(a.getAge()-b.getAge()==0)return 0;
		else return -1;
	}

	
	
}



public class Main {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        int size=in.nextInt();
        PersonSortable2[] test=new PersonSortable2[size];
        for(int i=0;i<size;i++) {
        	test[i]=new PersonSortable2(in.next(),in.nextInt());
        }
        
        System.out.println("NameComparator:sort");
        Arrays.sort(test,new NameComparator());
        for(int i=0;i<size;i++) {
        	System.out.println(test[i].toString());
        }
        
        System.out.println("AgeComparator:sort");
        Arrays.sort(test,new AgeComparator());
        for(int i=0;i<size;i++) {
        	System.out.println(test[i].toString());
        }
        
        System.out.println(Arrays.toString(NameComparator.class.getInterfaces()));
        System.out.println(Arrays.toString(AgeComparator.class.getInterfaces()));

       
    }
}






Topics: Java