Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.text.DateFormat;
- import java.util.Date;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Scanner;
- class Zero extends task{
- public static void zero() {
- System.exit(0);
- }
- }
- class One extends task{
- public static void one() {
- System.out.println("Hello World ");
- }
- }
- class Two extends task{
- public static void read_variable(double variable, Scanner sc){
- while (!sc.hasNextDouble()) {
- System.out.println("Ошибка. Введено не число. Введите заново.");
- sc.next();
- }
- }
- public static double less0(double variable, Scanner sc) {
- read_variable(variable, sc);
- variable = sc.nextDouble();
- while (variable < 0) {
- System.out.println("Число должно быть >=0. Введите заново");
- read_variable(variable, sc);
- variable = sc.nextDouble();
- }
- return variable;
- }
- public static double equally0(double variable, Scanner sc) {
- read_variable(variable, sc);
- variable = sc.nextDouble();
- while(variable==0){
- System.out.println("Число не должно равнятся 0. Введите заново");
- read_variable(variable, sc);
- variable = sc.nextDouble();
- }
- return variable;
- }
- public static void two() {
- double dbl_x=1, dbl_y=0, dbl_z=1;
- Scanner sc = new Scanner(System.in);
- System.out.println("введите x ");
- dbl_x= less0(dbl_x, sc);
- System.out.println("введите z ");
- dbl_z=equally0(dbl_z, sc);
- dbl_y=Math.sqrt(dbl_x)-6/(dbl_z);
- System.out.printf("y=%.3f\n", dbl_y);
- }
- }
- class Three extends task{
- public static final DateFormat DATE_FORMAT = new SimpleDateFormat("dd.MM.yyyy");
- private static boolean isValid(String date)
- {
- try {
- return DATE_FORMAT.format(DATE_FORMAT.parse(date)).equals(date);
- } catch (ParseException ex){
- return false;
- }
- }
- public static Date get_date(Scanner sc) {
- String s_original = sc.nextLine();
- s_original=s_original.trim();
- while(!isValid(s_original))
- {
- System.out.println("Неправильный ввод даты,введите дату формата ДД.ММ.ГГГГ");
- s_original = sc.nextLine();
- s_original=s_original.trim();
- }
- try {
- return DATE_FORMAT.parse(s_original);
- } catch (ParseException ex) {
- ex.printStackTrace();
- return new Date();
- }
- }
- public static void ShowNums(int N, int depth) {
- if (depth==0) {
- System.out.print(N);
- return;
- }
- int i_count=N-depth;
- depth=depth-1;
- System.out.print(i_count+" ");
- ShowNums(N, depth);
- }
- public static void three() {
- Scanner sc = new Scanner(System.in);
- System.out.println("Введите первую дату формата ДД.ММ.ГГГГ");
- Date t_date1 = get_date(sc);
- System.out.println("Введите вторую дату формата ДД.ММ.ГГГГ");
- Date t_date2 = get_date(sc);
- while(t_date1.getTime() > t_date2.getTime())
- {
- System.out.println("Первая дата больше второй,введите даты повторно");
- System.out.println("Введите первую дату формата ДД.ММ.ГГГГ");
- t_date1 = get_date(sc);
- System.out.println("Введите вторую дату формата ДД.ММ.ГГГГ");
- t_date2 = get_date(sc);
- }
- while(((t_date2.getTime() - t_date1.getTime())/86400000)>2500)
- {
- System.out.println("Слишком большая разница в днях. Введите повторно.");
- System.out.println("Введите первую дату формата ДД.ММ.ГГГГ");
- t_date1 = get_date(sc);
- System.out.println("Введите вторую дату формата ДД.ММ.ГГГГ");
- t_date2 = get_date(sc);
- }
- System.out.println("Введите третью дату формата ДД.ММ.ГГГГ");
- Date t_date3 = get_date(sc);
- System.out.println("Введите четвертую дату формата ДД.ММ.ГГГГ");
- Date t_date4 = get_date(sc);
- while(t_date3.getTime() > t_date4.getTime())
- {
- System.out.println("Первая дата больше второй,введите даты повторно");
- System.out.println("Введите третью дату формата ДД.ММ.ГГГГ");
- t_date3 = get_date(sc);
- System.out.println("Введите четвертую дату формата ДД.ММ.ГГГГ");
- t_date4 = get_date(sc);
- }
- while(((t_date4.getTime() - t_date3.getTime())/86400000)>2500)
- {
- System.out.println("Слишком большая разница в днях. Введите повторно.");
- System.out.println("Введите третью дату формата ДД.ММ.ГГГГ");
- t_date3 = get_date(sc);
- System.out.println("Введите четвертую дату формата ДД.ММ.ГГГГ");
- t_date2 = get_date(sc);
- }
- long l_1, l_2, l_3, l_4;
- if (t_date3.getTime()<t_date1.getTime()){
- l_3=t_date1.getTime();
- l_1=t_date3.getTime();
- l_2=t_date4.getTime();
- l_4=t_date2.getTime();
- }
- else {
- l_1 = t_date1.getTime();
- l_3 = t_date3.getTime();
- l_4 = t_date4.getTime();
- l_2 = t_date2.getTime();
- }
- if(l_3>l_2){
- System.out.println("Даты не пересекаются");
- }
- long d_length;
- long l_Start = Math.max(l_1, l_3);
- long l_End = Math.min(l_2, l_4);
- d_length=l_End-l_Start;
- int d_days = (int) (d_length / (86400000))+1;
- System.out.println("Пересечение дат в днях: " + d_days);
- System.out.print("все числа от 0 до " + d_days + ": ");
- ShowNums(d_days, d_days);
- }
- }
- class Four extends task {
- public static void check(String str) {
- check_email(str);
- check_IP(str);
- check_number(str);
- }
- public static void check_number(String str) {
- if (str.matches("^[\\+]?[(]?[0-9]{3}[)]?[-\\s\\.]?[0-9]{3}[-\\s\\.]?[0-9]{4,6}$")) {
- System.out.printf("\nявляется номером телефона");
- }
- else System.out.printf("\nне является номером телефона");
- }
- public static void check_email(String str) {
- if (str.matches("[^@ \\t\\r\\n]+@[^@ \\t\\r\\n]+\\.[^@ \\t\\r\\n]+")) {
- System.out.printf("\nявляется email'ом");
- }
- else System.out.printf("\nне является email'ом");
- }
- public static void check_IP(String str) {
- if (str.matches("(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}")) {
- System.out.printf("\nявляется IP-адресом");
- }
- else System.out.printf("\nне является IP-адресом");
- }
- public static String get_string(Scanner in){
- String str = in.nextLine();
- return str;
- }
- public static void string_coincide(String one, String two){
- if (one.equals(two)) {
- System.out.printf("Исходные строки совпадают");
- }
- else System.out.printf("Исходные строки разные");
- }
- public static void string_abs_coincide(String one, String two){
- if (one.equals(two)) {
- System.out.printf("\nСтроки совпадают (в одном регистре, без повторяющихся пробелов)");
- }
- else System.out.printf("\nСтроки разные (в одном регистре, без повторяющихся пробелов)");
- }
- public static void string_shifter(String one, String two){
- if (one.equals(two)) {
- System.out.printf("\nОдна строка является перевертышем другой");}
- else System.out.printf("\nОдна строка не является перевертышем другой");
- }
- public static void four() {
- Scanner in = new Scanner(System.in);
- System.out.print("Введите первую строку: ");
- String one_s = get_string(in);
- System.out.print("Введите вторую строку: ");
- String two_s = get_string(in);
- string_coincide(one_s, two_s);
- String one_s1= ((one_s.trim()).toUpperCase()).replaceAll(" +", " ");
- String two_s1=((two_s.trim()).toUpperCase()).replaceAll(" +", " ");
- string_abs_coincide(one_s1, two_s1);
- String one_s2 = new StringBuffer(one_s1).reverse().toString();
- string_shifter(one_s2, two_s1);
- System.out.printf("\nСтрока 1:");
- check(one_s1);
- System.out.printf("\nСтрока 2:");
- check(two_s1);
- }
- }
- public class task {
- public static void MENU() {
- System.out.println("\n0. Exit ");
- System.out.println("1. Hello World! ");
- System.out.println("2. Calc: math.sqrt(x)-6/(z) ");
- System.out.println("3. Recursion date ");
- System.out.println("4. Strings ");
- System.out.println("Выберите один из вышеперечисленных пунктов: ");
- }
- public static int read_choice(int choice, Scanner sc){
- while (!sc.hasNextInt()) {
- System.out.println("Выберите один из вышеперечисленных пунктов: ");
- sc.next();
- }
- choice = sc.nextInt();
- return choice;
- }
- public static void main(String[] args) throws Exception{
- boolean isExit=false;
- Scanner sc = new Scanner(System.in);
- int i_choice = 0;
- System.out.println("Выберите один из вышеперечисленных пунктов: ");
- while (!isExit){
- MENU();
- i_choice=read_choice(i_choice, sc);
- switch (i_choice){
- case 0:
- Zero zero_o = new Zero();
- zero_o.zero();
- break;
- case 1:
- One one_o = new One();
- one_o.one();
- break;
- case 2:
- Two two_o = new Two();
- two_o. two();
- break;
- case 3:
- Three three_o = new Three();
- three_o. three();
- break;
- case 4:
- Four four_o = new Four();
- four_o. four();
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement