Advertisement
bobo_bobkata

Untitled

Jun 23rd, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.60 KB | None | 0 0
  1. package Ideas;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Rado {
  6.  
  7.     //global variables
  8.     private int age;
  9.     private static int squareSide;
  10.  
  11.     //setters
  12.     public void setAge(int age) {
  13.         this.age = age;
  14.     }
  15.  
  16.     public static void setSquareSide(int squareSide) {
  17.         Rado.squareSide = squareSide;
  18.     }
  19.  
  20.     // getters
  21.     public int getAge() {
  22.         return this.age;
  23.     }
  24.  
  25.     public static int getSquareSide() {
  26.         return squareSide;
  27.     }
  28.  
  29.     public static void main(String[] args) {
  30.         Scanner scanner = new Scanner(System.in);
  31.         //new instance of Rado
  32.         Rado rado = new Rado();
  33.         String input = "12";//scanner.nextLine();
  34.  
  35.         // validating input
  36.         if (rado.validate(input)) {
  37.             System.out.println(rado.getAge());
  38.         } else {
  39.             System.out.println("Not successfully set");
  40.         }
  41.  
  42.         //Initialize Array
  43.         int[] mas = {2, 5, 1, 7, 4, -1, 2};
  44.  
  45.         //summing Array
  46.         int sum = 0;
  47.         for (int i = 0; i < mas.length; i++) {
  48.             sum += mas[i];
  49.         }
  50.         System.out.println(sum);
  51.  
  52.         //sorting Array\\ Bubble Sort
  53.         for (int i = 0; i < mas.length; i++) {
  54.             for (int j = 0; j < mas.length - i - 1; j++) {
  55.                 if (mas[j] > mas[j + 1]) {
  56.                     int temp = mas[j];
  57.                     mas[j] = mas[j + 1];
  58.                     mas[j + 1] = temp;
  59.                 }
  60.             }
  61.         }
  62.         //printing Array
  63.         print(mas);
  64.  
  65.         input = "20";//scanner.nextLine();
  66.         if (rado.validate(input)) {
  67.  
  68.             //Calculating Square Area
  69.             System.out.println(squareArea());
  70.  
  71.             //Calculating Perimeter
  72.             System.out.println(squarePerimeter());
  73.         }
  74.         //throw Exceptions
  75. //        throw new ArrayIndexOutOfBoundsException("Invalid Index");
  76. //        throw new NullPointerException("null format");
  77. //        throw  new IllegalArgumentException("Illegal");
  78.  
  79.         //variables
  80.         double number = 2.334521;
  81.         int intNum = 3;
  82.         String pesho = "Pesho";
  83.         //precision of double
  84.         //3
  85.         System.out.println(String.format("Int: %d", intNum));
  86.         //2
  87.         System.out.println(String.format("Double: %.0f", number));
  88.         //2.334
  89.         System.out.println(String.format("Precision: 3 - %.3f", number));
  90.         //2.33
  91.         System.out.println(String.format("Precision: 2 - %.2f", number));
  92.         //Pesho
  93.         System.out.println(String.format("String: %s", pesho));
  94.  
  95.  
  96.         //to call non static method needs object
  97.         // static can call static
  98.         // object
  99.         Rado rado1 = new Rado();
  100.         //static
  101.         print(mas);
  102.         // non static
  103.         rado1.validate("asd");
  104.     }
  105.  
  106.     //methods
  107.     private static int squareArea() {
  108.         return getSquareSide() * getSquareSide();
  109.     }
  110.  
  111.     private static int squarePerimeter() {
  112.         return 4 * getSquareSide();
  113.     }
  114.  
  115.     public static void print(int[] mas) {
  116.         for (int i = 0; i < mas.length; i++) {
  117.             System.out.print(mas[i] + " ");
  118.         }
  119.     }
  120.  
  121.     private boolean validate(String input) {
  122.         int number;
  123.         try {
  124.             number = Integer.parseInt(input);
  125.             Double.parseDouble(input);
  126.             if (number >= 0) {
  127.                 this.setAge(number);
  128.                 System.out.println("The age is set");
  129.                 return true;
  130.             }
  131.         } catch (Exception ignored) {
  132.             System.out.println("Error");
  133.         }
  134.         return false;
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement