Questions about Java methods. Please tell me how to write the following.

Asked 1 years ago, Updated 1 years ago, 346 views

】List of animals and song br
Verbs such as animal 'sound'
dog bark
cat meow Bird sing (singing)
Sheep bleat
Pig grunt

I would like to see the following on the console:

My name is Taro Yamada!
From now on, I will introduce my pet!
My name is Pochi!
I will be 7 years old on July 27, 2020!
Whoooooooooooooooooooooooops!

The code being created is as follows.

[Main method]

package animal;

public class Main {

    public static void main(String[]args) {

        // After creating the method of question 下部 below question の, set your first and last names in the arguments.
        Main.printMyName("Yamada", "Taro");

        // Q2 Store the appropriate values in the variables below.
        String name = pet;
        intage=7;
    }

    // Q3 Instance any animal class under the following conditions and call the appropriate method.
    // 1: Use the local variable created in question 2 for the method argument.
    void show() {
        System.out.println("My name is ""+name+""!"");
    }

    void showAnimal() {
        System.out.println("Here's my pet!");
        Animal animal = new Animal();
    }

    // Q4 Create a printMyName method that outputs your name and the contents of introducing your pet to the console under the conditions 1-3 below.
    //
    // -1. Access qualifier = private
    // -2. Arguments:
    // -2-1. Last Name (Sei)
    // -2-2 First Name (May)
    // -3. Return value = None
    private static void printMyName(String firstName, String lastName) {
        System.out.println("Name is "+fullName(firstName, lastName)+"");
    }
  }
}

[Any animal class method]

package animal;

// Q6 Create any animal class under the following conditions.
// -1: Please refer to [List of Animals and Calls] in the backlog for the target animals.
// -2—Succession of Animal

public class Any animal class name {

    public void Animal() {
        super();
    }

    public void Animal(String name, intage) {
        super(name,age);
    }

⇩

public class Any animal class name extends Animal {

    public Any animal class name (String name, age) {
        super(name,age);
    }

    public class dog extensions Animal {

        public dog(String name, intage) {
            super(name,age);
        }

    // Q7 Create a method to output the animal's cry to the console that matches the class under the following conditions:
    // -1: Please refer to [List of Animals and Calls] in the backlog for the target animal's song.
    // -2. Access Qualifier = Protected
    // -3. Arguments = None
    // -4. Return value = None
    class Dog extensions Animal {
        public void bark() {
            System.out.println("Wow!");
        }
    }
}

[Animal Method]

package animal;

import java.text.SimpleDateFormat;

public class Animal {

    private String name;
    private age;

    public Animal() {
    }

    public Animal (String name, intage) {
        this.name = name;
        This.age=age;
    }

    protected void setName(String name){
        this.name = name;
    }

    protected void setAge(interage){
        This.age=age;
    }

    protected String getName() {
        return this.name;
    }

    protected int getAge() {
        return this.age;
    }

    protected void introduction() {
        System.out.println("Name is "+this.getName()+"!");

        // Q5 Use Calendar class to obtain birthdays in yyyy/MM/dd format under the following conditions.
        // -1: Calendar class variable name = calendar
        // -2: Display the date "5 months and 15 days after" from the date of execution
        SimpleDateFormat sdf = new SimpleDateFormat("Describe the date format here";
        System.out.println(sdf.format(calendar.getTime())+" to "+this.getAge()+"!");
    }
}

java

2022-10-22 00:01

1 Answers

Run Environment: paiza.io
※It may be written to match paiza.io.

Main.java

import java.util.*;

public class Main {

    public static void main(String[]args) {

        // After creating the method of question の at the bottom, set your first and last names in the arguments.
        // Main.printMyName("Yamada", "Taro");

        // Q2 Store the appropriate values in the variables below.
        String name = "Pochi";
        intage=7;
        
        show(fullName("Yamada", "Taro"));
        showAnimal (name,age);
    }
    
    // Q3 Instance any animal class under the following conditions and call the appropriate method.
    // 1—Use the local variable created in question 2 for method arguments.
    static void show (String name) {
        System.out.println("My name is ""+name+""!"");
    }
    
    static void showAnimal (String name, intage) {
        System.out.println("From now on, let me introduce my pet!");
        Animal animal=new Dog(name,age);
        animal.introduce();
        animal.bark();
    }

    // Q4 Create a printMyName method to output your name and pet introduction to the console under 1-3 conditions below.
    //
    // -1. Access qualifier = private
    // -2. Arguments:
    // -2-1. Last Name (Sei)
    // -2-2 First Name (May)
    // -3. Return value = None
    private static void printMyName(String firstName, String lastName) {
        System.out.println("Name is "+fullName(firstName, lastName)+"");
    }
    
    private static String fullName (String firstName, String lastName) {
        return firstName+"+lastName;
    }
}

Animal.java

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Animal {

    private String name;
    private age;

    public Animal() {
    }

    public Animal (String name, intage) {
        this.name = name;
        This.age=age;
    }

    protected void setName(String name){
        this.name = name;
    }

    protected void setAge(interage){
        This.age=age;
    }

    protected String getName() {
        return this.name;
    }

    protected int getAge() {
        return this.age;
    }

    protected void introduction() {
        System.out.println("Name is "+this.getName()+"!");

        // Q5 Use Calendar class to obtain birthdays in yyyy/MM/dd format under the following conditions.
        // -1: Calendar class variable name = calendar
        // -2: Display the date "5 months and 15 days after" from the date of execution
        Calendar calendar=Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        System.out.println(sdf.format(calendar.getTime())+" to "+this.getAge()+"!");
    }
    
    protected voidbark(){}
}

Dog.java

//Create a method to output animal calls to the console for the class under the following conditions:
// -1: Please refer to [List of Animals and Calls] in the backlog for the target animal's song.
// -2. Access Qualifier = Protected
// -3. Arguments = None
// -4. Return value = None
public class Dog extensions Animal {
    
    public Dog(String name, intage) {
        super(name,age);
    }
    
    protected void bark() {
        System.out.println("Wow!");
    }
}

Output Results

My name is Taro Yamada!
From now on, I will introduce my pet!
My name is Pochi!
I will be 7 years old on October 16, 2022!
Whoops!


2022-10-22 00:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.