Martynas Žukovas
Martynas Žukovas

Reputation: 149

Java wrong value initialization

I have training object programming and I stucked at testing. My programm scan pet info and print it at screen(basic of basics). When I enter pet info:

name - Jimbo (min 3 max 16 letters)
type - 2 ( dog)
gender - 1 (male)

I got this output:

Pet info:
Your pet name is Jimbo, pet type is dog, and gender is male.

it write everything normaly, but when I enter this:

name - Ji
type - 8 (out of choices)
gender - 8 (out of choices)

i got this output:

Pet info:
Your pet name is ji, pet type is unknown type, and gender is unknown gender.

Instead of ji i should get words "wrong name length" but it still prints ji. Im confused. I have started java object programming and want to understand it fully.

Main.java

import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner key_scan = new Scanner (System.in); 


        System.out.println("Enter your pet name(min 3 letters, max 16 letters): ");// select your pet name
        String pet_name = key_scan.nextLine();

        System.out.println("Enter your pet type number 1(cat), 2(dog), 3(mouse), 4(fish): ");// select your pet type
        int pet_type = key_scan.nextInt();

        System.out.println("Enter your pet gender 1(male), 2(female): ");// select your pet gender
        int pet_gender = key_scan.nextInt();

        petInfo obj_petInfo = new petInfo(pet_name, pet_type, pet_gender);

        System.out.printf("Pet info:\n%s", obj_petInfo.formatPetInfo());

        key_scan.close(); // close scanner key_scan
    }
}

petInfo.java

public class petInfo {
    private String pet_name;
    private int pet_type;
    private int pet_gender;

    public petInfo(){
        this("Unknown", 0, 0);  
    }

    public petInfo(String pet_name_t, int pet_type_t, int pet_gender_t){
        pet_name = pet_name_t;// string name
        pet_type = pet_type_t;// int type
        pet_gender = pet_gender_t;// int gender
    }
////////////////////////////////set methods 
    public void setPet_name(String pet_name){
        pet_name = ((pet_name.length() >= 3 && pet_name.length() <= 16)? pet_name : "wrong name length");
    }
///////////////////////////////// pet type  
    public void makePet_type_cat(int pet_type){
        pet_type = 1;
    }

    public void makePet_type_dog(int pet_type){
        pet_type = 2;
    }

    public void makePet_type_mouse(int pet_type){
        pet_type = 3;
    }

    public void makePet_type_fish(int pet_type){
        pet_type = 4;
    }
////////////////////////////// pet gender   
    public void makePet_gender_male(int pet_gender){
        pet_gender = 1;
    }

    public void makePet_gender_female(int pet_gender){
        pet_gender = 2;
    }

    boolean pet_is_male(){
        return this.pet_gender == 1;
    }

    boolean pet_is_female(){
        return this.pet_gender == 2;
    }

//////////////////////////////// pet type check
    boolean pet_is_cat(){
        return this.pet_type == 1;
    }

    boolean pet_is_dog(){
        return this.pet_type == 2;
    }

    boolean pet_is_mouse(){
        return this.pet_type == 3;
    }

    boolean pet_is_fish(){
        return this.pet_type == 4;
    }
/////////////////////////////////////get methods
    public String getPet_name(){
        return pet_name;
    }

    String getPet_type(){
    if (this.pet_is_cat())
            return "cat";
        else if(this.pet_is_dog())
            return "dog";
        else if(this.pet_is_mouse())
            return "mouse";
        else if(this.pet_is_fish())
            return "fish";
        else
            return "unknown type";  
    }   

    String getPet_gender(){
        if(this.pet_is_male())
            return "male";
        else if (this.pet_is_female())
            return "female";
        return "unknown gender";
    }

    String formatPetInfo(){
        return String.format("Your pet name is %s, pet type is %s, and gender is %s.", getPet_name(), this.getPet_type(), this.getPet_gender());
    }
}

Upvotes: 1

Views: 124

Answers (3)

Sachin Verma
Sachin Verma

Reputation: 3802

if you want to use your conventions that is still possible, Java has a keyword this that refers to current object so you may use this like:

public petInfo(String pet_name_t, int pet_type_t, int pet_gender_t){
    this.pet_name = pet_name_t;// pet_name_t refers to local variable get from parameterlist but this.pet_name_t refers to global variable of class what you defined as "private String pet_name;"
    this.pet_type = pet_type_t;// int type
    this.pet_gender = pet_gender_t;// int gender
}

Upvotes: 0

Juned Ahsan
Juned Ahsan

Reputation: 68715

You have not called your setPet_name, anywhere which will basically do the validation. The values of name is assigned through the following constructor call:

    petInfo obj_petInfo = new petInfo(pet_name, pet_type, pet_gender);

which calls the below constructor and assigns the name without any validation:

public petInfo(String pet_name_t, int pet_type_t, int pet_gender_t){
    pet_name = pet_name_t;// string name
    pet_type = pet_type_t;// int type
    pet_gender = pet_gender_t;// int gender
}

You can call your setter in the constructor to assign the values instead of direct assignment:

public petInfo(String pet_name_t, int pet_type_t, int pet_gender_t){
    setPet_name(pet_name_t);// string name
    pet_type = pet_type_t;// int type
    pet_gender = pet_gender_t;// int gender
}

Upvotes: 2

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

The problem is that although setPetName does validation of the name, the constructor performs a "straight" assignment without validation.

Change the constructor as follows:

public petInfo(String pet_name_t, int pet_type_t, int pet_gender_t){
    setPet_name(pet_name_t);// string name
    pet_type = pet_type_t;// int type
    pet_gender = pet_gender_t;// int gender
}

This will fix the problem.

Note that your code goes against Java naming conventions: rather than calling the setter method setPet_name, naming conventions suggest setPetName. Following naming conventions is very important to ensure code readability, so other method names should be changed in a similar way.

Upvotes: 3

Related Questions