Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Date;
- import java.text.SimpleDateFormat;
- import java.text.DateFormat;
- import java.time.LocalDate;
- import java.text.ParseException;
- import java.util.LinkedList;
- interface SomeInterface {
- public void input();
- public void output();
- public double method();
- }
- class Person implements SomeInterface{
- private String nameP, surname;
- private Date date;
- private double heigh, weight;
- public Person(String nameP, String surname, String date, double heigh, double weight) throws Exception {
- this.nameP = nameP;
- this.surname = surname;
- this.date = new SimpleDateFormat("dd/MM/yyyy").parse(date);
- this.heigh = heigh;
- this.weight = weight;
- input();
- }
- public void input() {
- if (heigh < 0) {
- throw new IllegalArgumentException("The heigh can not be a negative number");
- }
- if (weight < 0) {
- throw new IllegalArgumentException("The weight can not be a negative number");
- }
- }
- public void output() {
- System.out.print( String.format("\nPerson object\nName: %s\nSurname: %s\nBirthday Date: %s\nHeigh: %.2f\nWeight: %.2f", nameP, surname, date, heigh, weight) );
- }
- @Override
- public String toString() {
- return String.format("\nPerson object\nName: %s\nSurname: %s\nBirthday Date: %s\nHeigh: %.2f\nWeight: %.2f", nameP, surname, date, heigh, weight);
- }
- @Override
- public int hashCode() {
- return (int)heigh * (int)heigh;
- }
- public double method() {
- return heigh+weight;
- }
- }
- class State implements SomeInterface{
- private String name, capital;
- private double population,square;
- public State(String name, String capital, double population, double square) {
- this.name = name;
- this.capital = capital;
- this.population = population;
- this.square = square;
- input();
- }
- public void input() {
- if (population < 0) {
- throw new IllegalArgumentException("The population of the creation can not be a negative number");
- }
- if (square < 0) {
- throw new IllegalArgumentException("The square of the creation can not be a negative number");
- }
- }
- public void output() {
- System.out.print( String.format("\nState object\nName: %s\nCapital: %s\nPopulation: %.2f\nSquare: %.2f", name, capital, population, square) );
- }
- public double method() {
- return population/square;
- }
- @Override
- public String toString() {
- return String.format("\nState object\nName: %s\nCapital: %s\nPopulation: %.2f\nSquare: %.2f", name, capital, population, square);
- }
- @Override
- public int hashCode() {
- return (int)square * (int)square;
- }
- }
- class Film implements SomeInterface{
- private String title, director;
- private int releaseYear;
- private double cost, profit;
- public Film(String title, String director, int releaseYear, double cost, double profit) {
- this.title = title;
- this.director = director;
- this.releaseYear = releaseYear;
- this.cost = cost;
- this.profit = profit;
- input();
- }
- public void input() {
- if (releaseYear < 0) {
- throw new IllegalArgumentException("The year of the creation can not be a negative number");
- }
- if (cost < 0) {
- throw new IllegalArgumentException("The cost can not be a negative number");
- }
- if (profit < 0) {
- throw new IllegalArgumentException("The profit can not be a negative number");
- }
- }
- public void output() {
- System.out.print( String.format("\nFilm object\nTitle: %s\nDirector: %s\nYear of the release: %d\nCost: %.2f\nProfit: %.2f", title, director, releaseYear, cost, profit) );
- }
- public double method() {
- return profit - cost;
- }
- @Override
- public String toString() {
- return String.format("\nFilm object\nTitle: %s\nDirector: %s\nYear of the release: %d\nCost: %.2f\nProfit: %.2f", title, director, releaseYear, cost, profit);
- }
- @Override
- public int hashCode() {
- return (int)profit * (int)profit;
- }
- }
- public class lab9 {
- public static void main(String[] args) throws Exception {
- Scanner sc = new Scanner(System.in);
- System.out.print("Enter your name: ");
- String nameP = sc.nextLine();
- System.out.print("Enter surname: ");
- String surname = sc.nextLine();
- System.out.print("Enter born date (dd/mm/yyyy): ");
- String date = sc.nextLine();
- System.out.print("Enter the heigh: ");
- double heigh = Double.parseDouble(sc.nextLine());
- System.out.print("Enter the weight: ");
- double weight = Double.parseDouble(sc.nextLine());
- Person newPerson;
- newPerson = new Person(nameP, surname, date, heigh, weight);
- System.out.print("\n\nEnter country name: ");
- String name = sc.nextLine();
- System.out.print("Enter the capital: ");
- String capital = sc.nextLine();
- System.out.print("Enter the populatin: ");
- double population = Double.parseDouble(sc.nextLine());
- System.out.print("Enter the square: ");
- double square = Double.parseDouble(sc.nextLine());
- State newState;
- newState = new State(name, capital, population, square);
- System.out.print("Enter the film title: ");
- String title = sc.nextLine();
- System.out.print("Enter the director: ");
- String director = sc.nextLine();
- System.out.print("Enter the year of the release: ");
- int releaseYear = Integer.parseInt(sc.nextLine());
- System.out.print("Enter the cost: ");
- double cost = Double.parseDouble(sc.nextLine());
- System.out.print("Enter the profit: ");
- double profit = Double.parseDouble(sc.nextLine());
- Film newFilm;
- newFilm = new Film(title, director, releaseYear, cost, profit);
- Byte byteVar = 121;
- Integer intVar = 24;
- Float floatVar = 15.5f;
- LinkedList<Object> linkList = new LinkedList<Object>();
- linkList.add(newPerson);
- linkList.add(newState);
- linkList.add(newFilm);
- linkList.add(byteVar);
- linkList.add(intVar);
- linkList.add(floatVar);
- SomeInterface[] array = new SomeInterface[] {newPerson, newState, newFilm};
- for (Object e : linkList) {
- System.out.println( "\n" + e.toString() );
- System.out.println( "Hash code: " + e.hashCode() );
- }
- sc.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement