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...
The following code is for a class named Box. The class Box includes a constructor method...
The following code is for a class named Box. The class Box includes a constructor method Box, and a method getVolume(). For your assignment you are to develop a java class named MatchBox. Your class MatchBox must extend the class Box and in addition to the attributes width, height, and depth that are defined in the class Box, MatchBox must add a new attribute weight. The getVolume method must both report the values of width, height, depth, and weight, but...
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...
P-3.40 Implement a class, SubstitutionCipher, with a constructor that takes a string with the 26 uppercase...
P-3.40 Implement a class, SubstitutionCipher, with a constructor that takes a string with the 26 uppercase letters in an arbitrary order and uses that as the encoder for a cipher (that is, A is mapped to the first character of the parameter, B is mapped to the second, and so on.) You should derive the decoding map from the forward version. P-3.41 Redesign the CaesarCipher class as a subclass of the SubstitutionCipher from the previous problem. P-3.42 Design a RandomCipher...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT