Question
*JAVA Program* I need help fixing the errors in the code. //Driver Class: public class Main{ public static void main(String[] args) { SmartPhone phone =
*JAVA Program* I need help fixing the errors in the code.
//Driver Class:
public class Main{
public static void main(String[] args) {
SmartPhone phone = new SmartPhone ("Sample", "40GB");
System.out.println("Total Playtime:" + phone.totalPlayTime());
System.out.println("To String Method: ");
System.out.println("To String Method after deleteALLSongs Method Invoked: ");
phone.deleteAllSongs ();
System.out.println(phone.toString( ));
}//end class
}//end main
//SmartPhone Class:
public class SmartPhone {
private String brand;
private String capacity;
private SongUpgrade [ ] songLibrary;
public SmartPhone (String brand, String capacity) {
this.brand = brand;
this.capacity = capacity;
this.songLibrary = new SongUpgrade[4];
songLibrary[0] = new SongUpgrade ("Song 1", 90);
songLibrary[1] = new SongUpgrade ("Song 2", 60);
songLibrary[2] = new SongUpgrade ("Song 3", 70);
songLibrary[3] = new SongUpgrade ("Song 4", 85);
}// smartphone
public boolean totalPlayTime(){
int total = 0;
for (SongUpgrade song : songLibrary) {
total = total + song.getLengthInSeconds();
}//end for
static void deleteAllSongs() {
this.songLibrary [0] = null;
this.songLibrary [1] = null;
this.songLibrary [2] = null;
this.songLibrary [3] = null;
}//end deleteAllSongs
public String toString() {
SongUpgrade song1=this.songLibrary[0];
SongUpgrade song2=this.songLibrary[1];
SongUpgrade song3=this.songLibrary[2];
SongUpgrade song4=this.songLibrary[3];
return "SmartPhone " + " Brand= " + brand + '\'' +
", Memory Capacity In GB= " + capacity + '\'' +
", Song Library= " + '\'' +
"Song 1='" + song1.toString() + '\'' +
", Song 2= " + song2.toString() + '\'' +
", Song 3= " + song3.toString() + '\'' +
", Song 4= " + song4.toString() + '\'' +
'}';
}//end toString
public class SongUpgrade{
private String title;
private int lengthInSeconds;
public SongUpgrade(String title, int lengthInSeconds){
this.title = title;
this.lengthInSeconds = lengthInSeconds;
}
public int getLengthInSeconds(){
return this.lengthInSeconds;
}
public String toString() {
return "SongUpgrade " +
"title=''" + title + '\'' +
", lengthInSeconds='" + lengthInSeconds + '\'' +
'}';
}//end string
}//end class
}//end class
//SongUpgrade Class:
class SongUpgrade{
public int totalPlayTime() {
return 1;
}
public class SmartPhone {
private String brand;
private int capacity;
private SongUpgrade[ ] songLibrary;
//two-args constructor
public SmartPhone (String brand, int capacity) {
this.brand=brand;
this.capacity=capacity;
// add songs
//creates 4 different objects of SongUpgrade class.
SongUpgrade song1= new SongUpgrade();
SongUpgrade song2= new SongUpgrade();
SongUpgrade song3= new SongUpgrade();
SongUpgrade song4= new SongUpgrade();
// add these four objects into songLibrary array
songLibrary=new SongUpgrade [4];
songLibrary[0]= song1;
songLibrary[1]= song2;
songLibrary[2]= song3;
songLibrary[3]= song4;
}
// this is a method-- it will delete all songs from the songLibrary (line 24-28)
public void deleteAllSongs() {
//iterate over songLibrary
for (int i=0; i // set songLibrary data to null. songLibrary[i]= null; }//end for }//end deleteAllSongs } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started