Question

WRITE THE PROGRAM IN JAVA.

gram 3: Olympians I C 1 Goals To write your own class in a program To write a constructor with parameters, an method and a toString) method. To use ArrayList To use a for-each loo . p *l . To use text file for output 2 The Context This is a single-class project, the first for which I have not given you code to start from. It is not realistic, or useful, but it gives us a place to start. This does not follow the MVC pattern for simplicitys sake. It is very important that you follow these instructions closely. They will guide you through learning a specific set of skills. Do not try to solve the problem some other way 2.1 The Olympian Class Create a class called Olympian to hold the facts about a member of the 2018 USA Olympic Team name, sport, numMedals and event 1. In this class, provide four private data members: name, sport, numMedals and event. 2. Create a constructor with four parameters (name, sport, numMedals and event) 3. Also provide a toString) method and a computeMedals) method The compute function. In your computeMedals), a static function, do all of the following 1. Take one parameter, an array of Olympian objects 2. Use a for-each loop to walk through the ArrayList 3. For each Olympian in the array: o get the numMedals using an accessor method (getter) o use the numMedals to add to the total number of medals for all the Olympians o return the total number of medals The main function. In your main function, do all of the following Output a welcome message to the console that includes the assignment name, P3: Olympians and your name 4. 5. Allocate an ArrayList of the class Olympian 6. In a loop, read in the name, sport, numMedals and event of each Olympian. Use these to create an Olympian object and add it to the Olympians ArrayList Validate input for NumMedals. It cannot be negative or more than 10 (Michael Phelps has 8) but it can be zero o 7. When input is finished, call the function named computeMedals), described above, and receive a return value that is the total medals received bv all the Olvmpians in vour list. Print this to the console with an appropriate label 8. Use a for-each loop to print the array of Olympians and show their name, sport, numMedals and event. Use proper labels and spacing so it looks like a table Open a file named results.txt for output and declare a Print Writer 9.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

import java.io.File;

import java.io.FileNotFoundException;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.Scanner;

public class Olympian {

//variables

private String name;

private String sport;

private int numMedals;

private String Event;

//constructor

public Olympian(String name, String sport, int numMedals, String event) {

super();

this.name = name;

this.sport = sport;

this.numMedals = numMedals;

Event = event;

}

//setters and getters

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getSport() {

return sport;

}

public void setSport(String sport) {

this.sport = sport;

}

public int getNumMedals() {

return numMedals;

}

public void setNumMedals(int numMedals) {

this.numMedals = numMedals;

}

public String getEvent() {

return Event;

}

public void setEvent(String event) {

Event = event;

}

//toString method

@Override

public String toString() {

return "Olympian [name=" + name + ", sport=" + sport + ", numMedals="

+ numMedals + ", Event=" + Event + "]";

}

///method to compute medals

public static int computeMedals(ArrayList<Olympian> op)

{

int nMedals=0;

for(Olympian o:op)

{

nMedals+=o.getNumMedals();

}

return nMedals;

}

public static void main(String args[]) throws FileNotFoundException

{

//displaying header here

System.out.println("Youe welcome statement goes here");

ArrayList<Olympian> o=new ArrayList<Olympian>();//arraylist to hold olympian objects created

Scanner sc=new Scanner(System.in);

String n,e,s;

int k;

for(int i=0;i<2;i++)//for loop to create olympian objects

{

//reading varaiables to for creation of object

System.out.println("Enter name");

n=sc.next();

System.out.println("Enter Sport");

s=sc.next();

System.out.println("Enter Event");

e=sc.next();

//validation for no of medals

do

{

System.out.println("Enter num of medals");

k=sc.nextInt();

}while(k<0);

o.add(new Olympian(n,s,k,e));//creating object and adding to arraylist

}

//calculating no of medals and displaying it

System.out.println("Total number of medal by all olympians: "+Olympian.computeMedals(o));

System.out.println("Name\tEvent\tSport\tNo of Medlas\t");

//iterating through array list and writing to file

for(Olympian ol:o)

{

System.out.println(ol.getName()+"\t"+ol.getEvent()+"\t"+ol.getSport()+"\t"+ol.getNumMedals());

}

PrintWriter pw=new PrintWriter(new File("F://anand/Chegg/src/results.txt"));

pw.write("Name\tEvent\tSport\tNo of Medlas\t\n");

for(Olympian ol:o)

{

pw.write(ol.getName()+"\t"+ol.getEvent()+"\t"+ol.getSport()+"\t"+ol.getNumMedals()+"\n");

}

pw.write("\t\t\t\t==========\n\t\t\t\t "+Olympian.computeMedals(o));

pw.close();

}

}

Output:

@Javadoc G Declaration 모 Console XI-×荧 테해(EL图1ピ <terminated OI Youe welcome statement goes here Enter name hon Enter Sport cy

Fianand\Chegglsrcresults.txt - Notepad++ e Edit Search View Encoding Language Settings Tools Macro new 12 3new 13X new 1 Name

Add a comment
Know the answer?
Add Answer to:
WRITE THE PROGRAM IN JAVA. gram 3: Olympians I C 1 Goals To write your own...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PLEASE WRITE IN JAVA FOR Q's A & B: Three investors are starting a new startup....

    PLEASE WRITE IN JAVA FOR Q's A & B: Three investors are starting a new startup. Each investor has a first name, last name and phone number all of type String and the amount of investment dollars of type double. A.) Write an Investor class that includes the above information. 1.) Make the instance variable of the investor class private. 2.) Write getter and setter methods for each instance variable. 3.) Add a constructor method to the investor class that...

  • JAVA I. Using the Event Class created previously, create an array of objects;        II. Demonstrate...

    JAVA I. Using the Event Class created previously, create an array of objects;        II. Demonstrate passing array reference to a method;        III. Demonstrate creating array of objects that prints out only x 0.0 for all objects. PROJECT 5C - ARRAYS Create a new file called " EventArray5C". There are some "challenging" directions in this project. . 1.Prepare a document box (tell me that task(s) the application is to accomplish, how it will accomplish the tasks, the author of...

  • Here is a sample run of the tester program: Make sure your program follows the Java...

    Here is a sample run of the tester program: Make sure your program follows the Java Coding Guidelines. Consider the following class: /** * A store superclass with a Name and Sales Tax Rate */ public class Store {      public final double SALES_TAX_RATE = 0.06;      private String name;           /**      * Constructor to create a new store      * @param newName      */      public Store(String newName)      {           name = newName;      }     ...

  • ( Object array + input) Write a Java program to meet the following requirements: 1. Define...

    ( Object array + input) Write a Java program to meet the following requirements: 1. Define a class called Student which contains: 1.1 data fields: a. An integer data field contains student id b. Two String data fields named firstname and lastname c. A String data field contains student’s email address 1.2 methods: a. A no-arg constructor that will create a default student object. b. A constructor that creates a student with the specified student id, firstname, lastname and email_address...

  • In C++ Write a program that contains a class called VideoGame. The class should contain the...

    In C++ Write a program that contains a class called VideoGame. The class should contain the member variables: Name price rating Specifications: Dynamically allocate all member variables. Write get/set methods for all member variables. Write a constructor that takes three parameters and initializes the member variables. Write a destructor. Add code to the destructor. In addition to any other code you may put in the destructor you should also add a cout statement that will print the message “Destructor Called”....

  • This is for Java Programming Please use comments Customer and Employee data (15 pts) Problem Description:...

    This is for Java Programming Please use comments Customer and Employee data (15 pts) Problem Description: Write a program that uses inheritance features of object-oriented programming, including method overriding and polymorphism. Console Welcome to the Person Tester application Create customer or employee? (c/e): c Enter first name: Frank Enter last name: Jones Enter email address: frank44@ hot mail. com Customer number: M10293 You entered: Name: Frank Jones Email: frank44@hot mail . com Customer number: M10293 Continue? (y/n): y Create customer...

  • using C# Write a program which calculates student grades for multiple assignments as well as the...

    using C# Write a program which calculates student grades for multiple assignments as well as the per-assignment average. The user should first input the total number of assignments. Then, the user should enter the name of a student as well as a grade for each assignment. After entering the student the user should be asked to enter "Y" if there are more students to enter or "N" if there is not ("N" by default). The user should be able to...

  • Write the following program in Java. Create an abstract Vehicle class - Vehicle should have a...

    Write the following program in Java. Create an abstract Vehicle class - Vehicle should have a float Speed and string Name, an abstract method Drive, and include a constructor. Create a Tesla class - Tesla inherits from Vehicle. - Tesla has an int Capacity. - Include a constructor with the variables, and use super to set the parent class members. - Override Drive to increase the speed by 1 each time. - Add a toString to print the name, speed,...

  • JAVA Problem:  Coffee do not use ArrayList or Case Design and implement a program that manages coffees....

    JAVA Problem:  Coffee do not use ArrayList or Case Design and implement a program that manages coffees. First, implement an abstract class named Coffee. The Coffee class has three member variables: coffee type, price, and store name. The Coffee class has an abstract method name ingredient. Make sure to add all necessary setters and getters as well as other methods. The class Coffee implements an interface HowToMakeDrink. The interface has the following method: public interface HowToMakeDrink { public void prepare ();...

  • Write a complete Java program to do the following Your code should have: A super class...

    Write a complete Java program to do the following Your code should have: A super class named Home Properties address SF (this is the number of Square feet) value (this is the dollar value of the home) PPSF (price per square foot) you will calculate this in a method Methods Set and get methods for all properties CalcPPSF - (PPSF is the value/SF) A sub class named Condo, which has one more HOA property: Property: HOAfee (home owners association fee)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT