In: Computer Science
Programming Steps: (In Java) (Please screenshot your output)
A. Files required or needed to be created:
1. Artist.java (Given Below)
2. p7artists.java (Input file to read from)
3. out1.txt (Output file to write to)
B. Java programs needed to writeand create:
1. MyArtistList.java:
- Contains the following:
1. list -
public, Arraylist of Artist.
This list will contain all entries
from "p7artists.txt"
2.
Constructor:
A constructor that accepts one
string parameter which is
the Input File Name, and will read
the file
to a ArrayList.
3. Two print()
methods:
i) First one takes two
parameters : A heading(name) and the output
file ID - to print the contents of
list to the output file
ii) No parameter:
Print the list contents to the console
C. Construct Step1.java that contans only the following
code:
public class Step1
{
public Step1()
{
MyArtistList myArtists = new
MyArtistList("C:/Users/patel/Desktop/JavaFiles/p7artists.txt");
myArtists.print("Name",
"C:/Users/patel/Desktop/JavaFiles/exam3out1.txt");
}
}
D. Construct "Main.java" that calls "Step1" which will
produce
an output file "out1.txt" that contains all ArtistID and
ArtistName from "p7artists.txt".
E. Attach the output when answering.
Required files:
p7artists.txt:
1 Acconci
2 Budd
3 Carpenter
4 Dill
5 Edwards
6 Fleming
7 Garber
8 Higgins
9 Ibe
10 Kollasch
11 Lerman
12 Metz
13 Novarre
14 Ortega
15 Parker
16 Penn
17 Pierobon
18 Prinzen
19 Quiroz
20 Rath
Artist.java:
public class Artist implements
java.lang.Comparable<Artist> {
// Instance variables
private int artistID; // id of artist
private String artistName; // name of artist
private Art art;
/**
* Constructor
*
* @param name
* @param number
*/
Artist(String name, int number) {
this.artistID = number;
this.artistName = name;
}
/**
* Constructor
*
* @param artistID
* @param artistName
* @param art
*/
public Artist(int artistID, String artistName, Art art) {
this.artistID = artistID;
this.artistName = artistName;
this.art = art;
}
/**
* return the artistName
*
* @return
*/
public String getArtistName() {
return artistName;
}
/**
* set the artistName
*
* @param artistName
*/
public void setArtistName(String artistName) {
this.artistName = artistName;
}
/**
* return the artistId
*
* @return
*/
public int getArtistID() {
return artistID;
}
/**
* set the artistId
*
* @param artistId
*/
public void setArtistID(int artistId) {
this.artistID = artistId;
}
/**
* @return the art
*/
public Art getArt() {
return art;
}
/**
* @param art
* the art to set
*/
public void setArt(Art art) {
this.art = art;
}
public int compareTo(Artist o) {
return this.getArt().compareTo(o.getArt());
}
/**
* toString method
*/
public String toString() {
return String.format("%-9d %-20s", this.artistID,
this.artistName);
}
}
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class MainArtistsTest {
public static void main ( String[] args )
{
new Step1();
}
}
class Art implements Comparable<Art> {
@Override
public int compareTo(Art o) {
// TODO Auto-generated method
stub
return 0;
}
}
class MyArtistList {
private ArrayList<Artist> artists = null;
public MyArtistList(String filename) {
Scanner input;
try {
input = new
Scanner(new File(filename));
artists = new
ArrayList<Artist>();
while(input !=
null && input.hasNext()) {
String line = input.nextLine();
String tokens[] = line.split(" ");
//System.out.println(tokens.length+""+tokens[1]+" --
"+Integer.parseInt(tokens[0].trim()));
Artist newA = new
Artist(tokens[1],Integer.parseInt(tokens[0].trim()));
artists.add(newA);
}
input.close();
} catch (FileNotFoundException e)
{
// TODO
Auto-generated catch block
e.printStackTrace();
}
}
public void print(String heading, String outFile)
{
//wite to file
BufferedWriter writer;
try {
writer = new
BufferedWriter(new FileWriter(outFile));
writer.write(heading+"\n");
for(Artist a :
artists) {
writer.write(a.toString()+"\n");
}
writer.flush();
writer.close();
} catch (IOException e) {
// TODO
Auto-generated catch block
e.printStackTrace();
}
}
public void print() {
for(Artist a : artists) {
System.out.println(a.toString());
}
}
}
class Step1
{
public Step1()
{
MyArtistList myArtists = new
MyArtistList("D:/ravi/Cheg/p7artists.txt");
myArtists.print();
myArtists.print("Name",
"D:/ravi/Cheg/exam3out1.txt");
}
}
class Artist implements java.lang.Comparable<Artist>
{
// Instance variables
private int artistID; // id of artist
private String artistName; // name of artist
private Art art;
/**
* Constructor
*
* @param name
* @param number
*/
Artist(String name, int number) {
this.artistID = number;
this.artistName = name;
}
/**
* Constructor
*
* @param artistID
* @param artistName
* @param art
*/
public Artist(int artistID, String artistName, Art
art) {
this.artistID = artistID;
this.artistName = artistName;
this.art = art;
}
/**
* return the artistName
*
* @return
*/
public String getArtistName() {
return artistName;
}
/**
* set the artistName
*
* @param artistName
*/
public void setArtistName(String artistName) {
this.artistName = artistName;
}
/**
* return the artistId
*
* @return
*/
public int getArtistID() {
return artistID;
}
/**
* set the artistId
*
* @param artistId
*/
public void setArtistID(int artistId) {
this.artistID = artistId;
}
/**
* @return the art
*/
public Art getArt() {
return art;
}
/**
* @param art
* the art to set
*/
public void setArt(Art art) {
this.art = art;
}
public int compareTo(Artist o) {
return this.getArt().compareTo(o.getArt());
}
/**
* toString method
*/
public String toString() {
return String.format("%-9d %-20s", this.artistID,
this.artistName);
}
}