public class Contributor { private String name; private String city; private Str
ID: 3843263 • Letter: P
Question
public class Contributor {
private String name;
private String city;
private String country;
private String phone;
private double contribution;
private int id;
public Contributor(String name, String city, String country, String phone, double contribution, int id) {
//initialize each value in the Contributor object
this.name = name;
this.city = city;
this.country = country;
this.phone = phone;
this.contribution = contribution;
this.id = id;
}
public void printContributor() {
//display the contents of the Contributor object
System.out.println("Name: " + name);
System.out.println("City: " + city);
System.out.println("Country: " + country);
System.out.println("Phone: " + phone);
System.out.println("Contribution: " + contribution);
System.out.println("ID: " + id);
System.out.println();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Node.java:
public class Node {
Contributor c;
Node next;
public Node(Contributor data){
//initialize member variables
c=data;
next=null;
}
public void displayNode() {
//display the contents of this node
c.printContributor();
}
void setNext(Node next)
{
this.next=next;
}
Node getNext()
{
return this.next;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Stack.java:
public class Stack {
Node first;
public Stack(){
//initialize the empty stack
first=null;
}
public void push(Node newNode){
//if the stack is empty, make first point to new Node.
if(first==null)
first=newNode;
//if the stack is not empty, loop until we get to the end of the list,
//then make the last Node point to new Node
else
{
newNode.setNext(first);
first=newNode;
}
}
public Node pop() {
//if the stack is empty, return null
if(first==null)
return null;
//Handle the case where there is only one Node in the stack
else if(first.getNext()==null)
{
Node t=first;
first=null;
return t;
}
//Handle the case where there are at Least two elements in the stack
else
{
Node t=first;
first=first.getNext();
return t;
}
}
public void print() {
//display the entire stack
Node tempDisplay = first; // start at the beginning of linkedList
while (tempDisplay != null){ // Executes until we don't find end of list.
tempDisplay.displayNode();
tempDisplay = tempDisplay.next; // move to next Node
}
System.out.println();
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ContributorManager.java:
import java.util.Scanner;
import java.io.File;
import java.util.regex.Pattern;
public class ContributorManager {
public static void main(String [] args) {
Scanner inputFile = null;
String name = null;
String city = null;
String country = null;
String phone = null;
double contribution = 0;
int id = 0;
Contributor c = null;
Stack stack = new Stack();
Node node = null;
//open contributors file
try {
inputFile = new Scanner(new File("contributors.csv"));
inputFile.useDelimiter(Pattern.compile("( )|( )|,"));
}
catch (Exception e) {
System.err.println("Error opening file.");
}
//create contributors object for each row, and add to the stack
while (inputFile.hasNext()) {
name = inputFile.next();
city = inputFile.next();
country = inputFile.next();
phone = inputFile.next();
contribution = inputFile.nextDouble();
id = inputFile.nextInt();
inputFile.nextLine(); //advance to the next line
c = new Contributor(name, city, country, phone, contribution, id);
node = new Node(c); //create a node using the contributor object
stack.push(node); //add the node to the top of the stack
}
stack.pop(); //remove the last node from the top of the stack
stack.print(); //print the contents of the entire stack
}
}
Input file: contributors.csv
Tim,Murphy,USA,8285557865,200,25
Gordon,Miner,USA,8285551008,150,32
Jean,Bell,USA,8285557503,225,33
Mike,Prather,USA,8285558497,155,34
George ,Pipps,USA,8285557777,100,35
I need to know where to put the contributors in the code above?????
Explanation / Answer
There can be four different classes:
a) Contributor.java
b) Node.java
c) Stack.java
d) ContributorManager.java
Contributors.csv is to be kept at the same location where you keeping your the above four java files. i.e. C:/Documents or your location.
and if you dont want another class for Contributor, better to keep it in ContributorManager.java , but you can not define public class Contributor then, you have to define class Contributor, else it will show error "class Contributor is public , should be declared in a filename named Contributor.java".
import java.util.Scanner;
import java.io.File;
import java.util.regex.Pattern;
public class ContributorManager {
public static void main(String [] args) {
Scanner inputFile = null;
String name = null;
String city = null;
String country = null;
String phone = null;
double contribution = 0;
int id = 0;
Contributor c = null;
Stack stack = new Stack();
Node node = null;
//open contributors file
try {
inputFile = new Scanner(new File("contributors.csv"));
inputFile.useDelimiter(Pattern.compile("( )|( )|,"));
}
catch (Exception e) {
System.err.println("Error opening file.");
}
//create contributors object for each row, and add to the stack
while (inputFile.hasNext()) {
name = inputFile.nextLine();
city = inputFile.nextLine();
country = inputFile.nextLine();
phone = inputFile.nextLine();
contribution = inputFile.nextDouble();
id = inputFile.nextInt();
inputFile.nextLine(); //advance to the next line
c = new Contributor(name, city, country, phone, contribution, id);
node = new Node(c); //create a node using the contributor object
stack.push(node); //add the node to the top of the stack
}
inputFile.close();
stack.pop(); //remove the last node from the top of the stack
stack.print(); //print the contents of the entire stack
}
}
class Contributor {
private String name;
private String city;
private String country;
private String phone;
private double contribution;
private int id;
public Contributor(String name, String city, String country, String phone, double contribution, int id) {
//initialize each value in the Contributor object
this.name = name;
this.city = city;
this.country = country;
this.phone = phone;
this.contribution = contribution;
this.id = id;
}
public void printContributor() {
//display the contents of the Contributor object
System.out.println("Name: " + name);
System.out.println("City: " + city);
System.out.println("Country: " + country);
System.out.println("Phone: " + phone);
System.out.println("Contribution: " + contribution);
System.out.println("ID: " + id);
System.out.println();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.