Kod strukturalny na obiektowy

0

Witam dopiero zaczynam zabawę z Java i mam już mały problem nie wiem jak ,, przerobić,, podany kod na obiektowy. Prosiłbym o jakąś mała pomoc ;) lub wskazówkę .

import java.util.*;
import java.lang.*;
import java.io.*;
 
class Application
{
	public static void main (String[] args) throws java.lang.Exception
	{
		String name = "Adam";
		double age = 40.5;
		double height = 178;
 
		if(name != null) {
			if(age > 30 && height > 160) {
				System.out.println("User is older than 30 and higher then 160cm");
			} else {
				System.out.println("User is younger than 30 or lower than 160cm");
			}
		}
	}
}
0

twoj kod sprowadza sie do

class Application
{
    public static void main (String[] args)
    {
        System.out.println("User is older than 30 and higher then 160cm");
    }
}

i nie ma sensu tu zadnych dodatkowych obiektow robic

1
class Person
{
	public String name;
	public double age;
	public double height;

	public Person(String name, double age, double height)
	{
		this.name = name;
		this.age = age;
		this.height = height
	}
}
class Application
{
	public static void main(String[] args)
	{
		Person adam = new Person("Adam", 40.5, 178);
		if(adam.age > 30 && height > 160)
		{
			...
		}
		else
		{
			...
		}
	}
}

Najprościej chyba coś takiego jak wyżej.
this oznacza obecny obiekt, używając tego możemy sprecyzować że chodzi nam o pole obiektu a nie argument.

Tutaj masz konstruktor:

public Person(String name, double age, double height)
{
	this.name = name;
	this.age = age;
	this.height = height
}

Dzięki niemu możesz w jednej linijce wpisać wartości pól podczas tworzenia obiektu, tak jak robię to tu:

Person adam = new Person("Adam", 40.5, 178);

Warto zaznaczyć że nazwa obiektu nie ma znaczenia i może być dowolna.

EDIT: @owczar92 - Jak potrzebujesz metod w klasie to możesz pójść o krok dalej i dodać akcesory i mutatory:

public class Person {
    private String name;
    private double age;
    private double height;

    public Person(String name, double age, double height) {
        this.name = name;
        this.age = age;
        this.height = height;
    }

    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }

    public double getAge() {
        return age;
    }

    public void setAge(double age) {
        this.age = age;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }
}

I wtedy używasz akcesorów aby pobrać wartość pól.

3

Wersja obiektowa:

import java.util.*;
import java.lang.*;
import java.io.*;
 
class Application
{
    public static void main (String[] args) throws java.lang.Exception
    {
        String name = "Adam";
        Double age = Double.valueOf(40.5);
        Double height = Double.valueOf(178);
 
        if(name != null) {
            if(age > 30 && height > 160) {
                System.out.println("User is older than 30 and higher then 160cm");
            } else {
                System.out.println("User is younger than 30 or lower than 160cm");
            }
        }
   }
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1