127k views
2 votes
Write a second constructor as indicated. Sample output:User1: Minutes: 0, Messages: 0User2: Minutes: 1000, Messages: 5000// ===== Code from file PhonePlan.java =====public class PhonePlan { private int freeMinutes; private int freeMessages; public PhonePlan() { freeMinutes = 0; freeMessages = 0; } // FIXME: Create a second constructor with numMinutes and numMessages parameters. /* Your solution goes here */ public void print() { System.out.println("Minutes: " + freeMinutes + ", Messages: " + freeMessages); return; }}// ===== end =====// ===== Code from file CallPhonePlan.java =====public class CallPhonePlan { public static void main (String [] args) { PhonePlan user1Plan = new PhonePlan(); // Calls default constructor PhonePlan user2Plan = new PhonePlan(1000, 5000); // Calls newly-created constructor System.out.print("User1: "); user1Plan.print(); System.out.print("User2: "); user2Plan.print(); return; }}// ===== end =====

2 Answers

5 votes

Answer:

PhonePlan::PhonePlan(int numMinutes, int numMessages) {

freeMinutes = numMinutes;

freeMessages = numMessages;

}

Explanation: For C++

PhonePlan::PhonePlan(int numMinutes, int numMessages) {

^^^^^^^^^^^^

you need that

freeMinutes = numMinutes;

freeMessages = numMessages;

}

User Blazerg
by
5.8k points
2 votes

Answer:

public PhonePlan(int numMinutes, int numMessages) {

freeMinutes = numMinutes;

freeMessages = numMessages;

}

Step-by-step explanation:

Create a constructor that takes two parameters, numMinutes and numMessages

Inside the constructor, set numMinutes to freeMinutes and numMessages to freeMessages.

With this constructor, we are able to create PhonePlan objects that take parameters and different options as seen in the output. We can set any values for minutes and messages in the constructor.

User Paolo Moretti
by
5.2k points