S
S
Sawayadi2020-09-24 16:32:40
Java
Sawayadi, 2020-09-24 16:32:40

How to turn the code into a class with "features"?

There is a code that asks you to enter a person's first and last name and his birthday (in three digits) and then determines the zodiac sign

import java.util.Scanner; 

public class Main
{
 public static void main(String[] args) {
          Scanner in = new Scanner(System.in);       
                int m, d, y, n, f;
                String z=" ";        
                System.out.print("Ведите имя и фамилию: ");
                String name = in.nextLine();
                System.out.println("Скажите день рождения");       
                d=in.nextInt();
                System.out.println("Скажите месяц рождения");       
                m=in.nextInt();   
                System.out.println("Скажите год рождения");       
                y=in.nextInt(); 
            switch (m) {
    case 1:
        if (d <= 19)
            z = "Козерог";
        else
            z = "Водолей";
        break;
    case 2:
        if (d <= 18)
            z = "Водолей";
        else
            z = "Рыбы";
        break;
    case 3:
        if (d <= 20)
            z = "Рыбы";
        else
            z = "Овен";
        break;
    case 4:
        if (d <= 19)
            z = "Овен";
        else
            z = "Телец";
        break;
    case 5:
        if (d <= 20)
            z = "Телец";
        else
            z = "Близнецы";
        break;
    case 6:
        if (d <= 21)
            z = "Близнецы";
        else
            z = "Рак";
        break;
    case 7:
        if (d <= 22)
            z = "Рак";
        else
            z = "Лев";
        break;
    case 8:
        if (d <= 22)
            z = "Лев";
        else
            z = "Дева";
        break;
    case 9:
        if (d <= 22)
            z = "Дева";
        else
            z = "Весы";
        break;
    case 10:
        if (d <= 22)
            z = "Весы";
        else
            z = "Скорпион";
        break;
    case 11:
        if (d <= 22)
            z = "Скорпион";
        else
            z = "Стрелец";
        break;
    case 12:
        if (d <= 21)
            z = "Стрелец";
        else
            z = "Козерог";
        break;
        }
        System.out.println(name+" дата рождения: "+d+"."+m+"."+y);
        System.out.println("Знак зодиака: "+z);
 }
}

It is necessary to turn it into a class (named "Zodiac"), which has the following features:
-Code data must be with the "private" specifier
In the public class must be:
-Input();
-Output();
-Properties for accessing class fields;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2020-09-24
Hasanly @azerphoenix

Hello!
I recommend first of all to study the basics of OOP and Java development.

Code data must be with "private" specifier

This is called encapsulation and is one of the foundations of OOP.
It is not recommended to use such constructions, as they complicate the perception of the code. I recommend further study of refactoring. (refactoring.guru)
The code
switch (m) {
    case 1:
        if (d <= 19)
            z = "Козерог";
        else
            z = "Водолей";
        break;
    case 2:
        if (d <= 18)
            z = "Водолей";
        else
            z = "Рыбы";
        break;
    case 3:
        if (d <= 20)
            z = "Рыбы";
        else
            z = "Овен";
        break;
    case 4:
        if (d <= 19)
            z = "Овен";
        else
            z = "Телец";
        break;
    case 5:
        if (d <= 20)
            z = "Телец";
        else
            z = "Близнецы";
        break;
    case 6:
        if (d <= 21)
            z = "Близнецы";
        else
            z = "Рак";
        break;
    case 7:
        if (d <= 22)
            z = "Рак";
        else
            z = "Лев";
        break;
    case 8:
        if (d <= 22)
            z = "Лев";
        else
            z = "Дева";
        break;
    case 9:
        if (d <= 22)
            z = "Дева";
        else
            z = "Весы";
        break;
    case 10:
        if (d <= 22)
            z = "Весы";
        else
            z = "Скорпион";
        break;
    case 11:
        if (d <= 22)
            z = "Скорпион";
        else
            z = "Стрелец";
        break;
    case 12:
        if (d <= 21)
            z = "Стрелец";
        else
            z = "Козерог";
        break;
        }
        System.out.println(name+" дата рождения: "+d+"."+m+"."+y);
        System.out.println("Знак зодиака: "+z);
 }

int m, d, y, n, f;
Always use clear names. For example, day, year, month, etc.
You can, for example, instead of taking the day, month, and year separately, take the date all at once. For example, in the format - dd/mm/yyyy, and then parse this string into Date.
And then check whether the date of birth of a person is included in the date interval corresponding to the zodiac

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question