0% found this document useful (0 votes)
9 views72 pages

java

The document contains Java code defining various classes such as Rectangle, Square, SinhVien, and Candidate, along with their respective methods for handling geometric shapes and student information. It includes functionality for calculating areas, managing student data, and performing operations on fractions and candidates. The main classes also feature methods for user input and output to display the results.

Uploaded by

minh102264
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views72 pages

java

The document contains Java code defining various classes such as Rectangle, Square, SinhVien, and Candidate, along with their respective methods for handling geometric shapes and student information. It includes functionality for calculating areas, managing student data, and performing operations on fractions and candidates. The main classes also feature methods for user input and output to display the results.

Uploaded by

minh102264
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 72

import java.util.

*;

class Rectangle implements Comparable<Rectangle> {


protected int length;

protected int width;

public Rectangle(int length, int width) {

this.length = length;

this.width = width;

public int area() {

return length * width;

public String toString() {

return "Rectangle(" + length + ", " + width + ")";

public int compareTo(Rectangle that) {

return Integer.compare(this.area(), that.area());

class Square extends Rectangle {

private double diagonal;

public Square(int side) {

super(side, side);

this.diagonal = side * 1.4142;

public int getSide() {

return length;
}

public String toString() {

return "Square(" + length + ")";

public class ShapeTest {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int n = Integer.parseInt(scanner.nextLine());

List<Rectangle> shapes = new ArrayList<>();

for (int i = 0; i < n; i++) {

String[] input = scanner.nextLine().split(" ");

int l = Integer.parseInt(input[0]);

int w = Integer.parseInt(input[1]);

if (l == w) {

shapes.add(new Square(l));

} else {

shapes.add(new Rectangle(l, w));

System.out.println("Shape\tArea");

for (Rectangle shape : shapes) {

System.out.println(shape + "\t" + shape.area());


}

scanner.close();

}
import java.util.*;

class SinhVien implements Comparable<SinhVien> {


protected String maSv;

protected String hoTen;

protected String chuyenNganh;

public SinhVien(String maSv, String hoTen, String chuyenNganh) {

this.maSv = maSv;

this.hoTen = hoTen;

this.chuyenNganh = chuyenNganh;

public String getMaSv() {

return maSv;

public void setMaSv(String maSv) {

this.maSv = maSv;

public String getHoTen() {

return hoTen;

public void setHoTen(String hoTen) {

this.hoTen = hoTen;

public String getChuyenNganh() {

return chuyenNganh;

public void setChuyenNganh(String chuyenNganh) {


this.chuyenNganh = chuyenNganh;

public void inThongTin() {

System.out.println(this.toString());

public String toString() {

return String.format("SinhVien(%s, %s, %s)", maSv, hoTen, chuyenNganh);

public int compareTo(SinhVien that) {

return this.maSv.compareTo(that.maSv);

class SinhVienVMU extends SinhVien {

private int mosWord;

private int mosExcel;

public SinhVienVMU(String maSv, String hoTen, String chuyenNganh, int mosWord, int mosExcel) {

super(maSv, hoTen, chuyenNganh);

this.mosWord = mosWord;

this.mosExcel = mosExcel;

public int getMosWord() {

return mosWord;

public void setMosWord(int mosWord) {


this.mosWord = mosWord;

public int getMosExcel() {

return mosExcel;

public void setMosExcel(int mosExcel) {

this.mosExcel = mosExcel;

public String toString() {

return String.format("SinhVienVMU(%s, %s, %s, %d, %d)", maSv, hoTen, chuyenNganh,


mosWord, mosExcel);

public class SinhVienTest {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int n = Integer.parseInt(scanner.nextLine());

List<SinhVien> sinhVienList = new ArrayList<>();

for (int i = 0; i < n; i++) {

String line = scanner.nextLine();

String[] parts = line.split("\t");

if (parts.length == 3) {

String maSv = parts[0];

String hoTen = parts[1];

String chuyenNganh = parts[2];


sinhVienList.add(new SinhVien(maSv, hoTen, chuyenNganh));

} else if (parts.length == 5) {

String maSv = parts[0];

String hoTen = parts[1];

String chuyenNganh = parts[2];

int mosWord = Integer.parseInt(parts[3]);

int mosExcel = Integer.parseInt(parts[4]);

sinhVienList.add(new SinhVienVMU(maSv, hoTen, chuyenNganh, mosWord, mosExcel));

for (SinhVien sv : sinhVienList) {

System.out.println(sv);

scanner.close();

public static int intReverse(int number) {


int reversed = 0;

while (number != 0) {

int digit = number % 10;

reversed = reversed * 10 + digit;

number /= 10;

return reversed;

}
public class PhanSo {

private int tu;

private int mau;

public PhanSo() {

this.tu = 0;

this.mau = 1;

public PhanSo(int tu, int mau) {

if (mau == 0) {

throw new IllegalArgumentException("Mau so phai khac 0.");

this.tu = tu;

this.mau = mau;
rutGon();

public void inPhanSo() {

System.out.println(this.toString());

public String toString() {

return tu + "/" + mau;

public void daoDau() {

this.tu = -this.tu;

public void nghichDao() {

if (this.tu == 0) {

throw new IllegalArgumentException("Khong the nghich dao khi tu so bang 0.");

int temp = this.tu;

this.tu = this.mau;

this.mau = temp;

rutGon();

public double toDouble() {

return (double) this.tu / this.mau;

public void rutGon() {

int gcd = ucln(Math.abs(tu), Math.abs(mau));


this.tu /= gcd;

this.mau /= gcd;

if (this.mau < 0) {

this.tu = -this.tu;

this.mau = -this.mau;

private int ucln(int a, int b) {

while (b != 0) {

int temp = b;

b = a % b;

a = temp;

return a;

public PhanSo cong(PhanSo that) {

int newTu = this.tu * that.mau + that.tu * this.mau;

int newMau = this.mau * that.mau;

return new PhanSo(newTu, newMau);

}
import java.util.ArrayList;

class Candidate {

private int id;

private String name;

private String gender;

private String birthday;

private String email;

private double gpa;

public Candidate(int id, String name, String gender, String birthday, String email, double gpa) {
this.id = id;

this.name = name;

this.gender = gender;

this.birthday = birthday;

this.email = email;

this.gpa = gpa;

public int getId() {

return id;

public void setId(int id) {

this.id = id;

public String getName() {

return name;

public void setName(String name) {

this.name = name;

public String getGender() {

return gender;

public void setGender(String gender) {

this.gender = gender;

}
public String getBirthday() {

return birthday;

public void setBirthday(String birthday) {

this.birthday = birthday;

public String getEmail() {

return email;

public void setEmail(String email) {

this.email = email;

public double getGpa() {

return gpa;

public void setGpa(double gpa) {

this.gpa = gpa;

public String toString() {

return String.format("%d, %s, %s, %s, %s, %.2f", id, name, gender, birthday, email, gpa);

class CandidateManagement {
private ArrayList<Candidate> candidateList;

public CandidateManagement() {

candidateList = new ArrayList<>();

public void addCandidate(Candidate candidate) {

candidateList.add(candidate);

public void removeCandidateById(int id) {

for (int i = 0; i < candidateList.size(); i++) {

if (candidateList.get(i).getId() == id) {

candidateList.remove(i);

break;

public void display() {

for (Candidate candidate : candidateList) {

System.out.println(candidate);

public class Test {

public static void main(String[] args) {

Candidate c1 = new Candidate(1, "Bill", "Male", "20-May-1980", "[email protected]", 4.9);

System.out.println(c1);
Candidate c2 = new Candidate(1, "Bill", "Male", "20-May-1980", "[email protected]", 3.9);

System.out.println(c2);

c2.setId(2);

c2.setName("Mary");

c2.setGender("Female");

c2.setBirthday("21-Jun-1981");

c2.setEmail("[email protected]");

c2.setGpa(4.0);

System.out.println(c2);

Candidate c3 = new Candidate(1, "Mark", "Male", "20-May-1999", "[email protected]", 2.9);

Candidate c4 = new Candidate(2, "Mary", "Female", "20-Dec-2000", "[email protected]", 3.0);

Candidate c5 = new Candidate(3, "Bill", "Male", "31-May-1980", "[email protected]", 3.9);

Candidate c6 = new Candidate(4, "Rose", "Female", "01-Jan-2009", "[email protected]", 4.0);

CandidateManagement cm = new CandidateManagement();

cm.addCandidate(c3);

cm.addCandidate(c4);

cm.addCandidate(c5);

cm.addCandidate(c6);

cm.addCandidate(new Candidate(5, "Thuong", "Male", "20-May-1980",


"[email protected]", 3.9));

cm.display();

}
import java.util.*;

class Person implements Comparable<Person> {

private String firstName;

private String lastName;

private String email;

private int age;

public Person(String firstName, String lastName, String email, int age) {

this.firstName = firstName;

this.lastName = lastName;

this.email = email;
this.age = age;

public String getFirstName() {

return firstName;

public void setFirstName(String firstName) {

this.firstName = firstName;

public String getLastName() {

return lastName;

public void setLastName(String lastName) {

this.lastName = lastName;

public String getEmail() {

return email;

public void setEmail(String email) {

this.email = email;

public int getAge() {

return age;

}
public void setAge(int age) {

this.age = age;

public String toString() {

return String.format("%s, %s, %s, %d", firstName, lastName, email, age);

public boolean equals(Object obj) {

if (this == obj) return true;

if (obj == null || getClass() != obj.getClass()) return false;

Person person = (Person) obj;

return age == person.age &&

Objects.equals(firstName, person.firstName) &&

Objects.equals(lastName, person.lastName) &&

Objects.equals(email, person.email);

public int compareTo(Person other) {

return Integer.compare(this.age, other.age);

public class PersonTest {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int n = Integer.parseInt(sc.nextLine());

List<Person> personList = new ArrayList<>();

for (int i = 0; i < n; i++) {


String input = sc.nextLine();

String[] tokens = input.split(" ");

String firstName = tokens[0];

String lastName = tokens[1];

String email = tokens[2];

int age = Integer.parseInt(tokens[3]);

personList.add(new Person(firstName, lastName, email, age));

personList.sort(Collections.reverseOrder());

for (Person p : personList) {

System.out.println(p);

sc.close();

}
import java.util.*;

class Person implements Comparable<Person> {

private String firstName;

private String lastName;

private String email;

private int age;

public Person(String firstName, String lastName, String email, int age) {

this.firstName = firstName;

this.lastName = lastName;

this.email = email;

this.age = age;

public String getFirstName() {

return firstName;

public void setFirstName(String firstName) {


this.firstName = firstName;

public String getLastName() {

return lastName;

public void setLastName(String lastName) {

this.lastName = lastName;

public String getEmail() {

return email;

public void setEmail(String email) {

this.email = email;

public int getAge() {

return age;

public void setAge(int age) {

this.age = age;

public String toString() {

return String.format("%s, %s, %s, %d", firstName, lastName, email, age);

}
public boolean equals(Person that) {

return this.firstName.equals(that.firstName) &&

this.lastName.equals(that.lastName) &&

this.email.equals(that.email) &&

this.age == that.age;

public int compareTo(Person obj) {

if (obj != null) {

if (this.age < obj.age)

return -1;

else if (this.age > obj.age)

return 1;

else

return 0;

return -1;

public class PersonTest {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int n = Integer.parseInt(sc.nextLine());

List<Person> personList = new ArrayList<>();

for (int i = 0; i < n; i++) {

String input = sc.nextLine();

String[] tokens = input.split(" ");

String firstName = tokens[0];


String lastName = tokens[1];

String email = tokens[2];

int age = Integer.parseInt(tokens[3]);

personList.add(new Person(firstName, lastName, email, age));

Collections.sort(personList);

for (Person p : personList) {

System.out.println(p);

sc.close();

}
import java.util.Scanner;

class Point

private int x;

private int y;

public Point()

x = 0;

y = 0;

public Point(int x, int y)

this.x = x; this.y = y;

public int getX()


{

return x;

public void setX(int x)

this.x = x;

public int getY()

return y;

public void setY(int y)

this.y = y;

public void shift(int dx, int dy)

this.x += dx; this.y += dy;

public double distance(Point otherPoint)

return Math.sqrt(Math.pow((x-otherPoint.x), 2) + Math.pow((y-otherPoint.y), 2));

@Override

public String toString()

return String.format("%d %d", x, y);

class Line

{
private Point pointA;

private Point pointB;

public Line()

pointA = new Point(0,0);

pointB = new Point(0,0);

public Line(Point pointA, Point pointB)

this.pointA = pointA;

this.pointB = pointB;

public Point endPointA()

return pointA;

public Point endPointB()

return pointB;

public double length()

return pointA.distance(pointB);

public boolean isVertical()

return (pointA.getX() == pointB.getX());

public class Test1

{
public static void main(String[] args)

Scanner sc = new Scanner(System.in);

int n = sc.nextInt();

Point p1, p2, p3;

Line l1, l2, l3;

int x1, y1, x2, y2, x3, y3;

for (int i=0; i<n; i++)

x1 = sc.nextInt(); y1 = sc.nextInt();

x2 = sc.nextInt(); y2 = sc.nextInt();

x3 = sc.nextInt(); y3 = sc.nextInt();

p1 = new Point(x1, y1);

p2 = new Point(x2, y2);

p3 = new Point(x3, y3);

l1 = new Line(p1,p2);

l2 = new Line(p1,p3);

l3 = new Line(p2,p3);

if( l1.length()+l2.length()>l3.length() && l2.length()+l3.length()>l1.length() && l1.length()


+l3.length()>l2.length() )

System.out.println(p1 + " " + p2 + " " + p3 );

}
import java.util.Scanner;

import java.util.ArrayList;

import java.util.List;

abstract class Person {

protected String fullName;

protected String gender;

protected String phone;

protected String email;

public Person(String fullName, String gender, String phone, String email) {

this.fullName = fullName;

this.gender = gender;

this.phone = phone;

this.email = email;
}

public abstract void purchaseParkingPass();

public abstract String toString();

class Student extends Person {

private String studentId;

private double theory;

private double practice;

public Student(String fullName, String gender, String phone, String email, String studentId, double
theory, double practice) {

super(fullName, gender, phone, email);

if (theory < 0 || theory > 10 || practice < 0 || practice > 10) {

throw new IllegalArgumentException("Điểm phải nằm trong khoảng 0 đến 10.");

this.studentId = studentId;

this.theory = theory;

this.practice = practice;

public double calculateFinalMark() {

return (theory + practice) / 2.0;

public String toString() {

return String.format("Student(%s, %s, %s, %s, %s, %.2f, %.2f)",

fullName, gender, phone, email, studentId, theory, practice);

}
public void purchaseParkingPass() {

class Teacher extends Person {

private double basicSalary;

private double subsidy;

public Teacher(String fullName, String gender, String phone, String email, double basicSalary,
double subsidy) {

super(fullName, gender, phone, email);

this.basicSalary = basicSalary;

this.subsidy = subsidy;

public double calculateSalary() {

return basicSalary + subsidy;

public String toString() {

return String.format("Teacher(%s, %s, %s, %s, %.2f, %.2f)",

fullName, gender, phone, email, basicSalary, subsidy);

public void purchaseParkingPass() {

public class PersonStudentTeacherTest {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);


List<Person> list = new ArrayList<>();

int n = Integer.parseInt(scanner.nextLine());

for (int i = 0; i < n; i++) {

String[] input = scanner.nextLine().split("\t");

if (input[0].equals("Student")) {

String fullName = input[1];

String gender = input[2];

String phone = input[3];

String email = input[4];

String studentId = input[5];

double theory = Double.parseDouble(input[6]);

double practice = Double.parseDouble(input[7]);

list.add(new Student(fullName, gender, phone, email, studentId, theory, practice));

} else if (input[0].equals("Teacher")) {

String fullName = input[1];

String gender = input[2];

String phone = input[3];

String email = input[4];

double basicSalary = Double.parseDouble(input[5]);

double subsidy = Double.parseDouble(input[6]);

list.add(new Teacher(fullName, gender, phone, email, basicSalary, subsidy));

for (Person person : list) {

if (person instanceof Teacher) {

Teacher teacher = (Teacher) person;

if (teacher.calculateSalary() > 10000000) {

System.out.println(teacher);

}
}

public static int centuryFromYear(int year) {

return (year + 99) / 100;

import java.util.Scanner;
import java.util.ArrayList;

public class cau2 {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int n = scanner.nextInt();

int[] arr = new int[n];

for (int i = 0; i < n; i++) {

arr[i] = scanner.nextInt();

int minValue = arr[0];

int minIndex = 0;

for (int i = 1; i < n; i++) {

if (arr[i] < minValue) {

minValue = arr[i];

minIndex = i;

System.out.println((minIndex) + " " + minValue);

}
import java.util.Scanner;
class Animal {

private String name;

private int numOfLegs;

public Animal(String name, int numOfLegs) {

this.name = name;

this.numOfLegs = numOfLegs;

public void makeSound() {

public String getName() {

return name;

public void setName(String name) {

this.name = name;

public int getNumOfLegs() {

return numOfLegs;

public void setNumOfLegs(int numOfLegs) {

this.numOfLegs = numOfLegs;

public String toString() {

return "Animals(" + name + ", " + numOfLegs + ")";


}

class Bird extends Animal {

public Bird(String name, int numOfLegs) {

super(name, numOfLegs);

public void makeSound() {

System.out.println("TWEET");

public String toString() {

return "Bird(" + getName() + ", " + getNumOfLegs() + ")";

class Cow extends Animal {

public Cow(String name, int numOfLegs) {

super(name, numOfLegs);

public void makeSound() {

System.out.println("MOO");

public String toString() {

return "Cow(" + getName() + ", " + getNumOfLegs() + ")";

}
class Dog extends Animal {

public Dog(String name, int numOfLegs) {

super(name, numOfLegs);

public void makeSound() {

System.out.println("WOOF");

public String toString() {

return "Dog(" + getName() + ", " + getNumOfLegs() + ")";

public class Animals {

public static void main(String[] args) {

Animal a1 = new Bird("Barry", 2);

System.out.println(a1);

a1.makeSound();

Animal a2 = new Cow("Bessie", 4);

System.out.println(a2);

a2.makeSound();

Animal a3 = new Dog("Charlie", 4);

System.out.println(a3);

a3.makeSound();

a1.setName("Clive");
a1.setNumOfLegs(a1.getNumOfLegs() + 1);

System.out.println(a1);

a2.setName("Cinnamon");

a2.setNumOfLegs(a2.getNumOfLegs() + 2);

System.out.println(a2);

a3.setName("Sadie");

a3.setNumOfLegs(a3.getNumOfLegs() + 3);

System.out.println(a3);

Scanner scanner = new Scanner(System.in);

int n = scanner.nextInt();

scanner.nextLine();

Animal[] arr = new Animal[n];

for (int i = 0; i < n; i++) {

String type = scanner.next();

String name = scanner.next();

int legs = scanner.nextInt();

scanner.nextLine();

if (type.equals("Bird")) {

arr[i] = new Bird(name, legs);

} else if (type.equals("Cow")) {

arr[i] = new Cow(name, legs);

} else if (type.equals("Dog")) {

arr[i] = new Dog(name, legs);

}
for (Animal animal : arr) {

System.out.println(animal);

animal.makeSound();

}
import java.util.ArrayList;

import java.util.Scanner;

class Point {

private int x, y;

public Point() {

this.x = 0;

this.y = 0;

public Point(int x, int y) {

this.x = x;

this.y = y;

}
public int getX() {

return x;

public void setX(int x) {

this.x = x;

public int getY() {

return y;

public void setY(int y) {

this.y = y;

public void shift(int dx, int dy) {

this.x += dx;

this.y += dy;

public double distance(Point that) {

return Math.sqrt(Math.pow(this.x - that.x, 2) + Math.pow(this.y - that.y, 2));

@Override

public String toString() {

return "Point(" + x + ", " + y + ")";

}
class Line {

private Point pointA, pointB;

public Line(Point pointA, Point pointB) {

this.pointA = pointA;

this.pointB = pointB;

public Point endPointA() {

return pointA;

public Point endPointB() {

return pointB;

public double length() {

return pointA.distance(pointB);

public boolean isVertical() {

return pointA.getX() == pointB.getX();

public boolean isHorizontal() {

return pointA.getY() == pointB.getY();

@Override
public String toString() {

return pointA.toString() + "-->" + pointB.toString();

public class point {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int n = scanner.nextInt();

scanner.nextLine();

ArrayList<Line> lines = new ArrayList<>();

for (int i = 0; i < n; i++) {

String[] data = scanner.nextLine().split(" ");

int x1 = Integer.parseInt(data[0]);

int y1 = Integer.parseInt(data[1]);

int x2 = Integer.parseInt(data[2]);

int y2 = Integer.parseInt(data[3]);

Point pointA = new Point(x1, y1);

Point pointB = new Point(x2, y2);

Line line = new Line(pointA, pointB);

lines.add(line);

for (Line line : lines) {

if (line.isHorizontal()) {
System.out.println(line);

}
import java.util.*;

import java.util.Objects;

import java.util.Scanner;

class Book implements Comparable<Book> {

private String name;

private int numOfPages;

private double price;

public Book() {
this.name = null;

this.numOfPages = 0;

this.price = 0.0;

public Book(String name, int numOfPages, double price) {

this.name = name;

this.numOfPages = numOfPages;

this.price = price;

public String getName() {

return name;

public void setName(String name) {

this.name = name;

public int getNumOfPages() {

return numOfPages;

public void setNumOfPages(int numOfPages) {

this.numOfPages = numOfPages;

public double getPrice() {

return price;

}
public void setPrice(double price) {

this.price = price;

@Override

public String toString() {

return name + "\t" + numOfPages + "\t" + String.format("%.2f", price);

@Override

public int compareTo(Book that) {

return Double.compare(this.price, that.price);

class BookReaderWriter {

private Scanner scanner;

public BookReaderWriter(Scanner scanner) {

this.scanner = scanner;

public Book readRecord() {

String line = scanner.nextLine();

String[] data = line.split(" ");

String name = data[0];

int numOfPages = Integer.parseInt(data[1]);

double price = Double.parseDouble(data[2]);

Book book = new Book(name, numOfPages, price);

return book;
}

public void writeRecord(Book book) {

System.out.println(book.toString());

public class BookTest {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int n = scanner.nextInt();

int b = scanner.nextInt();

scanner.nextLine();

List<Book> books = new ArrayList<>();

BookReaderWriter bookReaderWriter = new BookReaderWriter(scanner);

for (int i = 0; i < n; i++) {

Book book = bookReaderWriter.readRecord();

books.add(book);

Collections.sort(books, Collections.reverseOrder());

for (int i = 0; i < b; i++) {

bookReaderWriter.writeRecord(books.get(i));

}
}

public static void printHexa(int n) {

if (n == 0) {

return;

printHexa(n / 16);

int reminder = n % 16;

if (reminder >= 10) {

System.out.print((char) ('A' + (reminder - 10)));

} else {

System.out.print(reminder);

}
import java.util.*;

public class TachChu {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

String line = scanner.nextLine();

String[] words = line.split(" ");

List<String> wordList = new ArrayList<>(Arrays.asList(words));

Collections.sort(wordList, Collections.reverseOrder());

for (int i = 0; i < wordList.size(); i++) {

System.out.print(wordList.get(i));

if (i != wordList.size() - 1) {
System.out.print(" ");

} else {

System.out.print(" ");

}
class Rational {

private int numerator;

private int denominator;


public Rational(int numerator, int denominator) {

if (denominator == 0) {

throw new IllegalArgumentException("Mẫu số không thể bằng 0");

int gcd = gcd(Math.abs(numerator), Math.abs(denominator));

this.numerator = numerator / gcd;

this.denominator = denominator / gcd;

if (this.denominator < 0) {

this.numerator = -this.numerator;

this.denominator = -this.denominator;

public Rational() {

this.numerator = 1;

this.denominator = 1;

private int gcd(int a, int b) {

while (b != 0) {

int temp = b;

b = a % b;

a = temp;

return a;

public static Rational addRational(Rational r1, Rational r2) {

int numerator = r1.numerator * r2.denominator + r2.numerator * r1.denominator;

int denominator = r1.denominator * r2.denominator;


return new Rational(numerator, denominator);

public static Rational substractRational(Rational r1, Rational r2) {

int numerator = r1.numerator * r2.denominator - r2.numerator * r1.denominator;

int denominator = r1.denominator * r2.denominator;

return new Rational(numerator, denominator);

public static Rational multiplyRational(Rational r1, Rational r2) {

int numerator = r1.numerator * r2.numerator;

int denominator = r1.denominator * r2.denominator;

return new Rational(numerator, denominator);

public static Rational devideRational(Rational r1, Rational r2) {

if (r2.numerator == 0) {

throw new IllegalArgumentException("Không thể chia cho phân số có tử số bằng 0");

int numerator = r1.numerator * r2.denominator;

int denominator = r1.denominator * r2.numerator;

return new Rational(numerator, denominator);

@Override

public String toString() {

return numerator + "/" + denominator;

public String toFloatString(int precision) {

double result = (double) numerator / denominator;


return String.format("%." + precision + "f", result);

}
import java.util.*;

class SinhVien implements Comparable<SinhVien> {

protected String maSv;

protected String hoTen;

protected String chuyenNganh;

public SinhVien(String maSv, String hoTen, String chuyenNganh) {

this.maSv = maSv;

this.hoTen = hoTen;

this.chuyenNganh = chuyenNganh;

public String getMaSv() {

return maSv;
}

public void setMaSv(String maSv) {

this.maSv = maSv;

public String getHoTen() {

return hoTen;

public void setHoTen(String hoTen) {

this.hoTen = hoTen;

public String getChuyenNganh() {

return chuyenNganh;

public void setChuyenNganh(String chuyenNganh) {

this.chuyenNganh = chuyenNganh;

public void inThongTin() {

System.out.println(this.toString());

@Override

public String toString() {

return "SinhVien(" + maSv + ", " + hoTen + ", " + chuyenNganh + ")";

}
@Override

public int compareTo(SinhVien that) {

return this.maSv.compareTo(that.maSv);

class SinhVienVMU extends SinhVien {

private int mosWord;

private int mosExcel;

public SinhVienVMU(String maSv, String hoTen, String chuyenNganh, int mosWord, int mosExcel) {

super(maSv, hoTen, chuyenNganh);

this.mosWord = mosWord;

this.mosExcel = mosExcel;

public int getMosWord() {

return mosWord;

public void setMosWord(int mosWord) {

this.mosWord = mosWord;

public int getMosExcel() {

return mosExcel;

public void setMosExcel(int mosExcel) {

this.mosExcel = mosExcel;

}
@Override

public String toString() {

return "SinhVienVMU(" + maSv + ", " + hoTen + ", " + chuyenNganh + ", " + mosWord + ", " +
mosExcel + ")";

public class SinhVienTest {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int n = Integer.parseInt(sc.nextLine());

List<SinhVien> sinhVienList = new ArrayList<>();

for (int i = 0; i < n; i++) {

String line = sc.nextLine();

String[] parts = line.split("\t");

if (parts.length == 3) {

String maSv = parts[0];

String hoTen = parts[1];

String chuyenNganh = parts[2];

SinhVien sinhVien = new SinhVien(maSv, hoTen, chuyenNganh);

sinhVienList.add(sinhVien);

} else if (parts.length == 5) {

String maSv = parts[0];

String hoTen = parts[1];

String chuyenNganh = parts[2];

int mosWord = Integer.parseInt(parts[3]);


int mosExcel = Integer.parseInt(parts[4]);

SinhVienVMU sinhVienVMU = new SinhVienVMU(maSv, hoTen, chuyenNganh, mosWord,


mosExcel);

sinhVienList.add(sinhVienVMU);

for (SinhVien sinhVien : sinhVienList) {

sinhVien.inThongTin();

sc.close();

Câu

For example:

Test Input Result

#2 21 Dac-nhan-tam 180 250000.00

Toi-ac-va-trung-phat 99 190000

Dac-nhan-tam 180 250000

#3 52 Nha-gia-kim 210 250000.00

Toi-tai-gioi-ban-cung-the 150 200000.00 Toi-tai-gioi-ban-cung-the 150


200000.00
Dac-nhan-tam 99 150000

Toi-ac-va-trung-phat 200 190000

Nha-gia-kim 210 250000

Bat-tre-dong-xanh 140 99000

import java.util.*;

import java.util.Objects;

import java.util.Scanner;
class Book implements Comparable<Book> {

private String name;

private int numOfPages;

private double price;

public Book() {

this.name = null;

this.numOfPages = 0;

this.price = 0.0;

public Book(String name, int numOfPages, double price) {

this.name = name;

this.numOfPages = numOfPages;

this.price = price;

public String getName() {

return name;

public void setName(String name) {

this.name = name;

public int getNumOfPages() {

return numOfPages;

public void setNumOfPages(int numOfPages) {


this.numOfPages = numOfPages;

public double getPrice() {

return price;

public void setPrice(double price) {

this.price = price;

@Override

public String toString() {

return name + "\t" + numOfPages + "\t" + String.format("%.2f", price);

@Override

public int compareTo(Book that) {

return Double.compare(this.price, that.price);

class BookReaderWriter {

private Scanner scanner;

public BookReaderWriter(Scanner scanner) {

this.scanner = scanner;

public Book readRecord() {

String line = scanner.nextLine();


String[] data = line.split(" ");

String name = data[0];

int numOfPages = Integer.parseInt(data[1]);

double price = Double.parseDouble(data[2]);

Book book = new Book(name, numOfPages, price);

return book;

public void writeRecord(Book book) {

System.out.println(book.toString());

public class BookTest {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int n = scanner.nextInt();

int b = scanner.nextInt();

scanner.nextLine();

List<Book> books = new ArrayList<>();

BookReaderWriter bookReaderWriter = new BookReaderWriter(scanner);

for (int i = 0; i < n; i++) {

Book book = bookReaderWriter.readRecord();

books.add(book);
}

Collections.sort(books, Collections.reverseOrder());

for (int i = 0; i < b; i++) {

bookReaderWriter.writeRecord(books.get(i));

You might also like