Question

In: Computer Science

Constructor: -empty body Class method 1: goodFriend - return string value, "I like you!" Class method...

Constructor:

-empty body

Class method 1: goodFriend

- return string value, "I like you!"

Class method 2: badFriend

- return string value, "I don't like you!"

Main Method:

- create new Friend object

- call goodFriend and badFriend using Friend object

/* class header */ {

/* constructor method header */ { }

public static void main (String [] args) { Friend f = /* new Friend object */

// call goodFriend using Friend object

// call badFriend using Friend object

}

public /* return type */ goodFriend() {

return "I like you!";

}

public /* return type */ badFriend() {

return "I don't like you!";

}

}

Solutions

Expert Solution

So In this program I will show you how to call the method using object as per given requirements

In this Program I use two diffrent Method which Returning some string value

1>public String goodFriend()

which is returning "I like you!" String

2>public String badFriend()

which is returning "I don't like you!" String

Code:



public class Friend {
    
    public Friend()
    {
        //Default constructor invoked when the object is created....
    }
    public String goodFriend()
    {
        return "I like you!";
    }
    public String badFriend()
    {
        return "I don't like you!";
    }
    
    public static void main(String[] argv)
    {
        Friend f=new Friend();//this statement invoke constructor which is basically used to assign the memory to the recently created object
        
        
        //f.goodFriend() this statment is returning a string value that's why I directly write it into the Print Statement 
        System.out.println(f.goodFriend());
        
        
        // You can also use the following approach where you have to first store the returning value into the variable and then print it
        String data=f.badFriend();
        System.out.println(data);
    }
    
    
}

Output

I hope you will undestand how to call the function and using Object

Do you feel needful and useful then please upvote me

Thank You


Related Solutions

import java.util.Scanner; public class LetterGrade { // The method you write will return a String representing...
import java.util.Scanner; public class LetterGrade { // The method you write will return a String representing a letter // grade (e.g., "A", "A-", "B+", etc.). // The letter grade is determined by the given percentage, according // to the scale specified on page 2 of the class syllabus (available // here: https://kyledewey.github.io/comp110-spring18/syllabus.pdf). // You may assume that the given percentage is between 0.0 and 100.0 // TODO - write your code below this comment.    public static String letterGrade(double percentage){...
Lab11B:Understanding the constructor. Remember that the constructor is just a special method (with no return type)...
Lab11B:Understanding the constructor. Remember that the constructor is just a special method (with no return type) that has the same name as the class name. Its job is to initialize all of the attributes. You can actually have more than one constructor, so long as the parameters are different. Create a class called Turtle that has two attributes: 1) speed and 2) color. Then, create a constructor that has no parameters, setting the default speed to 0 and the color...
Please implement Sample string toString()method for each class and return itself a string, not the output....
Please implement Sample string toString()method for each class and return itself a string, not the output. import java.util.ArrayList; public class Customer extends User{ private ArrayList orders; public Customer(String display_name, String password, String email) { super(display_name, password, email); } @Override public String getPermissionLevel() { return "CUSTOMER"; } public void addOrder(Order order){ this.orders.add(order); } public ArrayList listOrders(){ return this.orders; }; } ---------------- public class ElectronicProduct extends Product{ private long SNo; private String warranty_period; public ElectronicProduct(long SNo, String warranty_period, String productId, String productName,...
1. From the constructor, you infer that the name of the class containing the constructor must be_______ .
1. From the constructor, you infer that the name of the class containing the constructor must be_______ .2. Fill in the blanks so that the statement will instantiate a JButton object and assigns it to variable of JButton type:JButton btn= _______ ("OK");3. Fill in the blanks to complete the description of the constructor in the corresponding UML class diagram:+<>______( _____ : ______)
1. A constructor is a special Class member method. It is automatically called when an object...
1. A constructor is a special Class member method. It is automatically called when an object of the class is created. It can also be called more than once after the object is created. 2. In Java, the new operator is used to create an instance/object of a class in the heap. 3. A reference variable is a memory location contains an address of an object stored in the stack. 4. Encapsulation and abstraction are both the pillars of Object-Oriented...
Please write code in java and comment . thanks Item class A constructor, with a String...
Please write code in java and comment . thanks Item class A constructor, with a String parameter representing the name of the item. A name() method and a toString() method, both of which are identical and which return the name of the item. BadAmountException Class It must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it. It must have a default...
Complete the definitions for the following prototypes in the Time class: Time(std::string) – a constructor that...
Complete the definitions for the following prototypes in the Time class: Time(std::string) – a constructor that will take in a string with the format h:m:s and parse the h, m and s into separate int values that can be used to set the time of a new time object. int getSecond() const - return the value of the second void setSecond() - set the value of the second, making sure the new second value is valid before changing it. std::string...
Create a class named BankAccount, containing: a constructor accepting a String corresponding to the name of...
Create a class named BankAccount, containing: a constructor accepting a String corresponding to the name of the account holder. a method, getBalance, that returns a double corresponding to the account balance. a method withdraw that accepts a double, and deducts the amount from the account balance. Write a class definition for a subclass, CheckingAccount, that contains: a boolean instance variable, overdraft. (Having overdraft for a checking account allows one to write checks larger than the current balance). a constructor that...
JAVA The class will have a constructor BMI(String name, double height, double weight). The class should...
JAVA The class will have a constructor BMI(String name, double height, double weight). The class should have a public instance method, getBMI() that returns a double reflecting the person's BMI (Body Mass Index = weight (kg) / height2 (m2) ). The class should have a public toString() method that returns a String like Fred is 1.9m tall and is 87.0Kg and has a BMI of 24.099722991689752Kg/m^2 (just print the doubles without special formatting). Implement this class (if you wish you...
Project 1 Fractional Number Class Part-1 for Five groups of member functions: Constructors (inc. string constructor)...
Project 1 Fractional Number Class Part-1 for Five groups of member functions: Constructors (inc. string constructor) Getter/setters Math Type cast Friend << and >> Part-2 for the explanation for the behavior observed by running the provided test pattern: six (6) operations in one statement vs. six (6) operations in six (6) statements. PROJECT 1 Frac Class The following information, located on Github m03, are provided as a starter. Frac.h, a complete Frac Class Declaration ( the definition is to be...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT