Need help with the following java coding questions. Write a second constructor a
ID: 3787562 • Letter: N
Question
Need help with the following java coding questions. Write a second constructor as indicated. Sample output: User1: Minutes: 0, Messages: 0 User2: 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 =====
Explanation / Answer
// PhonePla.java
public class PhonePlan
{
private int freeMinutes;
private int freeMessages;
public PhonePlan()
{
freeMinutes = 0;
freeMessages = 0;
}
public PhonePlan(int freeMinutes, int freeMessages)
{
this.freeMinutes = freeMinutes;
this.freeMessages = freeMessages;
}
public void print()
{
System.out.println("Minutes: " + freeMinutes + ", Messages: " + freeMessages);
}
} // ===== end =====
// 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();
}
} // ===== end =====
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.