JAVA PROGRAMMING. Please only answer if you can do the ENTIRE problem. Program M
ID: 3860747 • Letter: J
Question
JAVA PROGRAMMING. Please only answer if you can do the ENTIRE problem. Program MUST compile and have SCREENSHOTS of outputs so I can give you a thumbs up :-)
You’ve been hired by Library Lenders to write a Java console application that shows the borrowing status of several items. The application has the following four classes:
Medium.java
This abstract class implements Serializable and represents a medium in the library inventory and includes:
Fields
- (constant) VERSION – the version of this application.
Abstract methods
String getTitle()
int getDaysOverdue()
double calculateFine()
ElectronicMedium.java
This class extends Medium and represents one electronic item in the library inventory and includes:
Fields
- (static) count – count of all distinct electronic media; initialize to 0 in declaration.
- title
- daysOverdue
A constructor with no parameters that sets the fields, respectively, to these values:
count = count + 1
title = "(not set)"
daysOverdue = 0
A constructor with two parameters that sets the fields, respectively, to these values:
count = count + 1
title – set from parameter
daysOverdue– set from parameter
Getter methods for each field (declare getCount static; prefix the returned title with “(E)” in getTitle).
Setter methods for each field (declare setCount static).
Override calculateFine to return 0 if daysOverdue <= 0, otherwise return daysOverdue * 0.75.
equals method that compares titles from two objects for equality.
toString method for returning instance variable values only.
PrintMedium.java
This class extends Medium and represents one print item in the library inventory and includes:
Fields
- (static) count – count of all distinct print media; initialize to 0 in declaration.
- title
- daysOverdue
A constructor with no parameters that sets the fields, respectively, to these values:
count = count + 1
title = "(not set)"
daysOverdue = 0
A constructor with two parameters that sets the fields, respectively, to these values:
count = count + 1
title – set from parameter
daysOverdue– set from parameter
Getter methods for each field (declare getCount static; prefix the returned title with “(P)” in getTitle).
Setter methods for each field (declare setCount static).
Override calculateFine to return 0 if daysOverdue <= 0, otherwise return daysOverdue * 0.5.
equals method that compares titles from two objects for equality.
toString method for returning instance variable values only.
HW6.java
This class contains the main method and uses the other classes to show the borrowing status. Include the application version from Medium.java in the welcome message in the console output. Create text file MediumStatusIn.txt, paste the following data into it, and place the file in your project folder. It has the following columns:
1) Medium type – E for electronic medium, P for print medium.
2) Medium title
3) Days overdue – negative number means not overdue; positive number means overdue.
MediumStatusIn.txt
P The Atlas of Forgotten Places -10
E The Lost City of Z -9
P Beloved Hope -4
P Hum If You Don't Know the Words 12
E The Client -6
P Ballplayer Jones 5
E A Dog's Purpose 16
E The Great Wall -3
P Mercy Falls -7
E Romeo and Juliet -7
Create and call a method called readTextFile to read the data from file MediumStatusIn.txt into an array list of Medium objects called media. Create and call a method called printMediaStatus to show the following for each medium:
Title
Days overdue
Fine – calculated fine of zero or greater.
Also, show the counts of electronic and print items. Create and call a method called writeObjectFile to write the ten objects to file MediumStatusOut.obj.
Explanation / Answer
public abstract class Medium {
public final String VERSION;
/**
* @param vERSION
*/
public Medium() {
VERSION = "1.0";
}
public abstract String getTitle();
public abstract int getDaysOverdue();
public abstract double calculateFine();
}
public class PrintMedium extends Medium {
public static int count = 0;
public String title;
public int daysOverdue;
public PrintMedium() {
// TODO Auto-generated constructor stub
super();
count = count + 1;
title = "(not set)";
daysOverdue = 0;
}
/**
* @param vERSION
* @param title
* @param daysOverdue
*/
public PrintMedium(String title, int daysOverdue) {
super();
count = count + 1;
this.title = title;
this.daysOverdue = daysOverdue;
}
@Override
public String getTitle() {
// TODO Auto-generated method stub
return "P " + title;
}
@Override
public int getDaysOverdue() {
// TODO Auto-generated method stub
return daysOverdue;
}
/**
* @param count
* the count to set
*/
public static void setCount(int count) {
ElectronicMedium.count = count;
}
/**
* @param title
* the title to set
*/
public void setTitle(String title) {
this.title = title;
}
/**
* @param daysOverdue
* the daysOverdue to set
*/
public void setDaysOverdue(int daysOverdue) {
this.daysOverdue = daysOverdue;
}
@Override
public double calculateFine() {
// TODO Auto-generated method stub
if (this.daysOverdue <= 0)
return 0;
else
return daysOverdue * 0.5;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "PrintMedium [title=" + title + ", daysOverdue=" + daysOverdue
+ ", calculateFine()=" + calculateFine() + "]";
}
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
if (obj instanceof PrintMedium) {
PrintMedium printMedium = (PrintMedium) obj;
if (this.getTitle().equals(printMedium.getTitle()))
return true;
else
return false;
} else
return false;
}
}
public class ElectronicMedium extends Medium {
public static int count = 0;
public String title;
public int daysOverdue;
public ElectronicMedium() {
// TODO Auto-generated constructor stub
super();
count = count + 1;
title = "(not set)";
daysOverdue = 0;
}
/**
* @param vERSION
* @param title
* @param daysOverdue
*/
public ElectronicMedium(String title, int daysOverdue) {
super();
count = count + 1;
this.title = title;
this.daysOverdue = daysOverdue;
}
@Override
public String getTitle() {
// TODO Auto-generated method stub
return "E " + title;
}
@Override
public int getDaysOverdue() {
// TODO Auto-generated method stub
return daysOverdue;
}
/**
* @param count
* the count to set
*/
public static void setCount(int count) {
ElectronicMedium.count = count;
}
/**
* @param title
* the title to set
*/
public void setTitle(String title) {
this.title = title;
}
/**
* @param daysOverdue
* the daysOverdue to set
*/
public void setDaysOverdue(int daysOverdue) {
this.daysOverdue = daysOverdue;
}
@Override
public double calculateFine() {
// TODO Auto-generated method stub
if (this.daysOverdue <= 0)
return 0;
else
return daysOverdue * 0.75;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "ElectronicMedium [title=" + title + ", daysOverdue="
+ daysOverdue + "]";
}
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
if (obj instanceof ElectronicMedium) {
ElectronicMedium electronicMedium = (ElectronicMedium) obj;
if (this.getTitle().equals(electronicMedium.getTitle()))
return true;
else
return false;
} else
return false;
}
}
import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class HW6 {
public static void main(String[] args) {
List<Medium> media = readTextFile("MediumStatusIn.txt");
printMediaStatus(media);
writeObjectFile(media);
}
/**
* @param media
*/
public static void writeObjectFile(List<Medium> media) {
PrintWriter writer = null;
try {
writer = new PrintWriter(new File("MediumStatusOut.obj"));
for (int i = 0; i < media.size(); i++) {
writer.write(media.get(i).toString() + " ");
}
writer.flush();
writer.close();
} catch (Exception e) {
// TODO: handle exception
}
}
/**
* @param media
*/
public static void printMediaStatus(List<Medium> media) {
for (int i = 0; i < media.size(); i++) {
System.out.println("Title: " + media.get(i).getTitle());
System.out
.println("Days overdue: " + media.get(i).getDaysOverdue());
System.out.println("Fine: " + media.get(i).calculateFine());
}
System.out.println("Number of Electronics:" + ElectronicMedium.count);
System.out.println("Number of PrintMedium:" + PrintMedium.count);
}
/**
* @param fileName
* @return
*/
public static List<Medium> readTextFile(String fileName) {
List<Medium> media = new ArrayList<Medium>();
Scanner scanner = null;
try {
scanner = new Scanner(new File(fileName));
while (scanner.hasNext()) {
String line = scanner.nextLine();
char mediumType = line.charAt(0);
String title = line.substring(1, line.lastIndexOf(' ')).trim();
int daysOverdue = Integer.parseInt(line.substring(
line.lastIndexOf(' '), line.length()).trim());
Medium medium = null;
if (mediumType == 'P')
medium = new PrintMedium(title, daysOverdue);
else
medium = new ElectronicMedium(title, daysOverdue);
media.add(medium);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return media;
}
}
MediumStatusIn.txt
P The Atlas of Forgotten Places -10
E The Lost City of Z -9
P Beloved Hope -4
P Hum If You Don't Know the Words 12
E The Client -6
P Ballplayer Jones 5
E A Dog's Purpose 16
E The Great Wall -3
P Mercy Falls -7
E Romeo and Juliet -7
MediumStatusOut.obj
PrintMedium [title=The Atlas of Forgotten Places, daysOverdue=-10, calculateFine()=0.0]
ElectronicMedium [title=The Lost City of Z, daysOverdue=-9]
PrintMedium [title=Beloved Hope, daysOverdue=-4, calculateFine()=0.0]
PrintMedium [title=Hum If You Don't Know the Words, daysOverdue=12, calculateFine()=6.0]
ElectronicMedium [title=The Client, daysOverdue=-6]
PrintMedium [title=Ballplayer Jones, daysOverdue=5, calculateFine()=2.5]
ElectronicMedium [title=A Dog's Purpose, daysOverdue=16]
ElectronicMedium [title=The Great Wall, daysOverdue=-3]
PrintMedium [title=Mercy Falls, daysOverdue=-7, calculateFine()=0.0]
ElectronicMedium [title=Romeo and Juliet, daysOverdue=-7]
OUTPUT:
Title: P The Atlas of Forgotten Places
Days overdue: -10
Fine: 0.0
Title: E The Lost City of Z
Days overdue: -9
Fine: 0.0
Title: P Beloved Hope
Days overdue: -4
Fine: 0.0
Title: P Hum If You Don't Know the Words
Days overdue: 12
Fine: 6.0
Title: E The Client
Days overdue: -6
Fine: 0.0
Title: P Ballplayer Jones
Days overdue: 5
Fine: 2.5
Title: E A Dog's Purpose
Days overdue: 16
Fine: 12.0
Title: E The Great Wall
Days overdue: -3
Fine: 0.0
Title: P Mercy Falls
Days overdue: -7
Fine: 0.0
Title: E Romeo and Juliet
Days overdue: -7
Fine: 0.0
Number of Electronics:5
Number of PrintMedium:5
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.