Advertisement
FlyChsCake

Jaba 10 - 9

Dec 5th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.40 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Date;
  3. import java.text.SimpleDateFormat;
  4. import java.text.DateFormat;
  5. import java.time.LocalDate;
  6. import java.text.ParseException;
  7. import java.util.LinkedList;
  8.  
  9. interface SomeInterface {
  10.   public void input();
  11.   public void output();
  12.   public double method();
  13. }
  14.  
  15. class Person implements SomeInterface{
  16.   private String nameP, surname;
  17.   private Date date;
  18.   private double heigh, weight;
  19.   public Person(String nameP, String surname, String date, double heigh, double weight) throws Exception {
  20.     this.nameP = nameP;
  21.     this.surname = surname;
  22.     this.date = new SimpleDateFormat("dd/MM/yyyy").parse(date);
  23.     this.heigh = heigh;
  24.     this.weight = weight;
  25.     input();
  26.   }
  27.  
  28.   public void input() {
  29.     if (heigh < 0) {
  30.       throw new IllegalArgumentException("The heigh can not be a negative number");
  31.     }
  32.     if (weight < 0) {
  33.       throw new IllegalArgumentException("The weight can not be a negative number");
  34.     }
  35.   }
  36.  
  37.   public void output() {
  38.     System.out.print( String.format("\nPerson object\nName: %s\nSurname: %s\nBirthday Date: %s\nHeigh: %.2f\nWeight: %.2f", nameP, surname, date, heigh, weight) );
  39.   }
  40.  
  41.       @Override
  42.     public String toString() {
  43.         return  String.format("\nPerson object\nName: %s\nSurname: %s\nBirthday Date: %s\nHeigh: %.2f\nWeight: %.2f", nameP, surname, date, heigh, weight);
  44.     }
  45.    
  46.        @Override
  47.     public int hashCode() {
  48.         return (int)heigh * (int)heigh;
  49.        
  50.     }
  51.  
  52.   public double method() {
  53.     return heigh+weight;
  54.   }
  55. }
  56.  
  57. class State implements SomeInterface{
  58.   private String name, capital;
  59.   private double population,square;
  60.  
  61.   public State(String name, String capital, double population, double square) {
  62.     this.name = name;
  63.     this.capital = capital;
  64.     this.population = population;
  65.     this.square = square;
  66.     input();
  67.   }
  68.  
  69.   public void input() {
  70.     if (population < 0) {
  71.       throw new IllegalArgumentException("The population of the creation can not be a negative number");
  72.     }
  73.     if (square < 0) {
  74.       throw new IllegalArgumentException("The square of the creation can not be a negative number");
  75.   }
  76. }
  77.   public void output() {
  78.     System.out.print( String.format("\nState object\nName: %s\nCapital: %s\nPopulation: %.2f\nSquare: %.2f", name, capital, population, square) );
  79.   }
  80.  
  81.   public double method() {
  82.     return population/square;
  83.   }
  84.  
  85.     @Override
  86.     public String toString() {
  87.         return String.format("\nState object\nName: %s\nCapital: %s\nPopulation: %.2f\nSquare: %.2f", name, capital, population, square);
  88.     }
  89.    
  90.         @Override
  91.     public int hashCode() {
  92.         return (int)square * (int)square;
  93.        
  94.     }
  95.    
  96. }
  97.  
  98. class Film implements SomeInterface{
  99.   private String title, director;
  100.   private int releaseYear;
  101.   private double cost, profit;
  102.  
  103.   public Film(String title, String director, int releaseYear, double cost, double profit) {
  104.     this.title = title;
  105.     this.director = director;
  106.     this.releaseYear = releaseYear;
  107.     this.cost = cost;
  108.     this.profit = profit;
  109.     input();
  110.   }
  111.  
  112.   public void input() {
  113.     if (releaseYear < 0) {
  114.       throw new IllegalArgumentException("The year of the creation can not be a negative number");
  115.     }
  116.     if (cost < 0) {
  117.       throw new IllegalArgumentException("The cost can not be a negative number");
  118.     }
  119.     if (profit < 0) {
  120.       throw new IllegalArgumentException("The profit can not be a negative number");
  121.     }
  122.   }
  123.  
  124.   public void output() {
  125.     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) );
  126.   }
  127.  
  128.   public double method() {
  129.     return profit - cost;
  130.   }
  131.  
  132.      @Override
  133.     public String toString() {
  134.         return String.format("\nFilm object\nTitle: %s\nDirector: %s\nYear of the release: %d\nCost: %.2f\nProfit: %.2f", title, director, releaseYear, cost, profit);
  135.     }
  136.    
  137.         @Override
  138.     public int hashCode() {
  139.         return (int)profit * (int)profit;
  140.        
  141.     }
  142.  
  143. }
  144.  
  145. public class lab9 {
  146.   public static void main(String[] args) throws Exception {
  147.  
  148.     Scanner sc = new Scanner(System.in);
  149.  
  150.  
  151.     System.out.print("Enter your name: ");
  152.     String nameP = sc.nextLine();
  153.    
  154.     System.out.print("Enter surname: ");
  155.     String surname = sc.nextLine();
  156.    
  157.     System.out.print("Enter born date (dd/mm/yyyy): ");
  158.     String date = sc.nextLine();
  159.  
  160.     System.out.print("Enter the heigh: ");
  161.     double heigh = Double.parseDouble(sc.nextLine());
  162.  
  163.     System.out.print("Enter the weight: ");
  164.     double weight = Double.parseDouble(sc.nextLine());
  165.    
  166.     Person newPerson;
  167.     newPerson = new Person(nameP, surname, date, heigh, weight);
  168.  
  169.  
  170.     System.out.print("\n\nEnter country name: ");
  171.     String name = sc.nextLine();
  172.    
  173.     System.out.print("Enter the capital: ");
  174.     String capital = sc.nextLine();
  175.    
  176.     System.out.print("Enter the populatin: ");
  177.     double population = Double.parseDouble(sc.nextLine());
  178.    
  179.     System.out.print("Enter the square: ");
  180.     double square = Double.parseDouble(sc.nextLine());
  181.    
  182.     State newState;
  183.     newState = new State(name, capital, population, square);
  184.  
  185.  
  186.     System.out.print("Enter the film title: ");
  187.     String title = sc.nextLine();
  188.    
  189.     System.out.print("Enter the director: ");
  190.     String director = sc.nextLine();
  191.    
  192.     System.out.print("Enter the year of the release: ");
  193.     int releaseYear = Integer.parseInt(sc.nextLine());
  194.  
  195.     System.out.print("Enter the cost: ");
  196.     double cost = Double.parseDouble(sc.nextLine());
  197.  
  198.     System.out.print("Enter the profit: ");
  199.     double profit = Double.parseDouble(sc.nextLine());
  200.    
  201.     Film newFilm;
  202.     newFilm = new Film(title, director, releaseYear, cost, profit);
  203.  
  204.     Byte byteVar = 121;
  205.     Integer intVar = 24;
  206.     Float floatVar = 15.5f;
  207.  
  208.     LinkedList<Object> linkList = new LinkedList<Object>();
  209.  
  210.     linkList.add(newPerson);
  211.     linkList.add(newState);
  212.     linkList.add(newFilm);
  213.     linkList.add(byteVar);
  214.     linkList.add(intVar);
  215.     linkList.add(floatVar);
  216.  
  217.     SomeInterface[] array = new SomeInterface[] {newPerson, newState, newFilm};
  218.  
  219.     for (Object e : linkList) {
  220.       System.out.println( "\n" + e.toString() );
  221.       System.out.println( "Hash code: " + e.hashCode() );
  222.     }
  223.  
  224.     sc.close();
  225.   }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement