In: Computer Science
I'm having difficulty trying to make my code work.
Create a subclass of Phone called SmartPhone. Make this subclass have a no-arg constructor and a constructor that takes a name, email, and phone. Override the toString method to return the required output.
Given Files:
public class Demo1 { public static void test(Phone p) { System.out.println("Getter test:"); System.out.println(p.getName()); System.out.println(p.getNumber()); } public static void main(String[] args) { Phone test1 = new SmartPhone(); Phone test2 = new SmartPhone("Alice", "8059226966", "[email protected]"); System.out.println(test1); System.out.println(test2); System.out.println(test1); test(test1); test(test2); } }
And
public class Phone { protected String name; protected long number; public Phone() { this("None", -1); } public Phone(String name) { this(name, -1); } public Phone(String name, long number) { this.name = name; this.number = number; } public String getName() { return name; } public long getNumber() { return number; } }
This is my code and please give me any tips or suggestions. Thanks!
public class SmartPhone extends Phone { private String email; private String format; public SmartPhone() { } public SmartPhone(String name, String phoneNumber, String email) { super(name, Long.parseLong(phoneNumber)); format = "Name: " + " " + "\n" + "Phone: " + " " + "\n" + "Email: " + " " + "\n" + "\n" + "Name: " + name + "\n" + "Phone: " + phoneNumber + "\n" + "Email: " + email + "\n"; } @Override public String toString() { return format; } }
You can follow this programming style.
/***********************************Phone.java*****************************/
public class Phone {
/*
* private data field, always keep data field to
private and give access through
* getter and setter
*/
private String name;
private long number;
// no argument constructor
public Phone() {
this("None", -1);
}
/**
*
* @param name
*/
public Phone(String name) {
this(name, -1);
}
/**
*
* @param name
* @param number
*/
public Phone(String name, long number) {
this.name = name;
this.number = number;
}
// return name
public String getName() {
return name;
}
// return number
public long getNumber() {
return number;
}
@Override
public String toString() {
return "Name: " + name + "\n" +
"Phone: " + number;
}
}
/**********************************SmartPhone.java*****************************/
public class SmartPhone extends Phone {
// private data field
private String email;
// no argument constructor
public SmartPhone() {
this.email = "None";
}
/**
*
* @param name
* @param phoneNumber
* @param email
*/
public SmartPhone(String name, String phoneNumber,
String email) {
super(name,
Long.parseLong(phoneNumber));
this.email = email;
}
// return email
public String getEmail() {
return email;
}
// no need to declare extra global variable for set
the format
@Override
public String toString() {
return super.toString() + "\n" +
"Email: " + email + "\n";
}
}
/*********************************Demo1.java****************************/
public class Demo1 {
//method test
public static void test(Phone p) {
System.out.println("Getter
test:");
System.out.println(p.getName());
System.out.println(p.getNumber());
}
public static void main(String[] args) {
//creating object
Phone test1 = new
SmartPhone();
Phone test2 = new
SmartPhone("Alice", "8059226966", "[email protected]");
//display toString
System.out.println(test1.toString());
System.out.println(test2.toString());
//test getters
test(test1);
test(test2);
}
}
/***********************output********************************/
Name: None
Phone: -1
Email: None
Name: Alice
Phone: 8059226966
Email: [email protected]
Getter test:
None
-1
Getter test:
Alice
8059226966
Please let me know if you have any doubt or modify the answer. Thanks :)