Question

USE JAVA PLEASE And answer all questions Question 5.1 1. Write a program that reads in...

USE JAVA PLEASE
And answer all questions

Question 5.1

1. Write a program that reads in a number n and then reads n strings. it then reads a string s from the user and gives you the word that was typed in after s.
Example input:
4
Joe
Steve
John
Mike
Steve
Example output:
John
Example input:
7
Up
Down
Left
Right
North
South
East
Right
Example output:
North
2. Remember to call your class Program

Question 5.2

1. Write a class called TeamTracker that is meant to keep track of the name and current league points accrued by a football team. It must have a constructor that accepts a team name and sets the current league points to zero. It must also have a method called win() which will increment the points by three, draw() which will increment the points by one and lose() which does nothing. It must also have a method called print() which prints out the team name followed by a colon, followed by their points.
2. The method header for the win method is shown below to help you.
public void win(){
3. You can make a main method in your TeamTracker class to test that it works, but the main method will not be marked.
3.1 Example main method
public static void main(String args[]){
TeamTracker t = new TeamTracker("Bafana");
t.win();
t.lose();
t.win();
t.draw();
t.print();
}
3.2 output
4
Bafana:7

Question 5.3

1. Write a class that will make the following code work:
public class Program{
public static void main(String args[]){
Example Output
} }
MarkCollector mc = new MarkCollector();
mc.addMark(14.2);
mc.addMark(34.7);
mc.addMark(10.1);
mc.addMark(34.9);
double av = mc.getAverageMark();

2. YouneedtocodetheMarkCollectorclass.Notethatitneedstostoremarks,andyoudon’tknow how many marks you are going to need to store. Consider using an ArrayList<Double> to store the marks.
3. You can use the Program class shown above to test your code. You can even include it in the zip file you submit to the marking system, but it will not be marked. Only your MarkCollector class will be marked.
4.1 Example Output
23.475


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

Question 5.1

import java.util.Scanner;
class Program
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in); //creating Scanner class object for inputs
int n; //to store number of strings
System.out.print("Enter the value of n: "); //input prompt
n=in.nextInt(); //input
String arr[]=new String[n]; //creating array to store the Strings
System.out.println("Enter the strings: "); //input prompt
for(int i=0;i<n;i++)
arr[i]=in.next(); //input
String s; //to store the String s
System.out.print("Enter s: "); //input prompt
s=in.next(); //input
for(int i=0;i<n-1;i++) //we loop from 0 to n-2, since n-1 is the last element and there is not string after it
{
if(arr[i].equals(s))
{
System.out.println("String after "+s+" is "+arr[i+1]);
return;
}
}
System.out.println("No string found after "+s); //if program reaches this point, s was not found
}
}

Output:

Question 5.2

class TeamTracker
{
//class variables
String name;
int points;
TeamTracker(String name) //parametrized constructor
{
this.name=name;
this.points=0;
}
public void win() //function to add 3 points to total points for a win
{
this.points+=3;
}
public void draw() //function to add 1 point to total points for a draw
{
this.points+=1;
}
public void lose() //function to add 0 points to total points for a loss
{
this.points+=0;
}
public void print() //function to print team name and current points
{
System.out.println(name+": "+points);
}
public static void main(String args[])
{
//testing the class
TeamTracker t = new TeamTracker("Bafana");
t.win();
t.lose();
t.win();
t.draw();
t.print();
}
}

Output:

Question 5.3

class MarkCollector
{
//class variables
double total;
int count;
MarkCollector() //default constructor
{
total=0.0;
count=0;
}
void addMark(double marks) //function to add a mark to the list
{
total+=marks; //adding a mark to the total
count++; //incrementing count
}
double getAverageMark() //function to return average of marks
{
if(count!=0)
return total/count;
return 0.0;
}
public static void main(String args[])
{
//testing the class
MarkCollector mc = new MarkCollector();
mc.addMark(14.2);
mc.addMark(34.7);
mc.addMark(10.1);
mc.addMark(34.9);
double av=mc.getAverageMark();
System.out.println(av);
}
}

Output:

Add a comment
Know the answer?
Add Answer to:
USE JAVA PLEASE And answer all questions Question 5.1 1. Write a program that reads in...
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
  • java question: Write a program that reads in data from a text file named in.txt. Compute...

    java question: Write a program that reads in data from a text file named in.txt. Compute the sum of all the valid integers in the input file. Likewise, compute the sum of all the valid doubles in the input file. Write the former to an output file called int_total.txt, and write the latter to an output file called double_total.txt. B) Write a program that converts the Java source code from the next-line brace style to the end-of-line brace style. For...

  • Write a program that reads a person's first and last names, separated by a space. Then...

    Write a program that reads a person's first and last names, separated by a space. Then the program outputs last name, comma, first name. End with newline. Example output if the input is: Maya Jones Jones, Maya import java.util.Scanner; public class SpaceReplace {    public static void main (String [] args) {       Scanner scnr = new Scanner(System.in);       String firstName;       String lastName; //answer goes here//    } }

  • Java. (20 pts)          Write a program that reads all the numbers from the file mynums.dat...

    Java. (20 pts)          Write a program that reads all the numbers from the file mynums.dat and prints out the sum of the positive values from the file. Assume that the file contains only numeric values. You must output an error if the file can't be opened. You must write the complete program and the output when the program runs successfully must conform exactly to the sample output. Bonus ( 5 pts). Output an error if the file contains non-numeric...

  • Write a program that reads a person's first and last names, separated by a space. Then...

    Write a program that reads a person's first and last names, separated by a space. Then the program outputs last name, comma, first name. End with newline. Example output if the input is Maya Jones Tones, Maya import java . util·Scanner; public class SpaceReplace 4 public static void main (String [1 ares) f( Scanner scnr-new Scanner(System.in); String firstName; String lastName; Your solution goes here */ 10 12

  • 2016 points output of following Java Program? public class Base { public final void show() {...

    2016 points output of following Java Program? public class Base { public final void show() { . System.out.println("Base::show() called"); public class Derived extends Base { public void show() { System.out.println("Derived::show() called"); } e lor rested in order public class Main { ects from and public static void main(String[] args) { Base b = new Derived(); b.show();

  • java 1. Write a method header on line three with the following specs: Returns: a boolean...

    java 1. Write a method header on line three with the following specs: Returns: a boolean Name: beTrue Parameters: none public class Main {       {        return true;    } } 2. Write a method header on line two with the following specs: Returns: a String Name: makeCapital Parameters: a String named "name" You should not be writing code on any line other than #2 public class Main {       {    String ans = name.toUpperCase();...

  • JAVA Programming Produce a method that reads in a set of values (double) from the user...

    JAVA Programming Produce a method that reads in a set of values (double) from the user and returns them. Use this header: public static double[] getNumsFromUser(String msg1, String msg2) The implementation of the method should start with a message to input the total number of array elements, followed by a message to enter all the array elements. The method returns the array of elements. The two strings msg1 and msg2 that are passed to the method as parameters will be...

  • Modify the objectstudent JAVA program to add a private variable zipcode, you must also add setters...

    Modify the objectstudent JAVA program to add a private variable zipcode, you must also add setters and getter methods for the new variable and modify the toString method. The objectstudent program is in this weeks module, you can give it a default value of 19090. class Student { private int id; private String name; public Student() { id = 8; name = "John"; } public int getid() { return id; } public String getname() { return name; } public void...

  • This is a Java program Write a program that reads an unspecified number of integers, determines...

    This is a Java program Write a program that reads an unspecified number of integers, determines home many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a floating-point number to 2 decimal places. System.out.println(countPositive); System.out.println(countNegative); System.out.println(total); System.out.printf("%.2f",total * 1.0 / count); Assume inputs are always integer [-912985158, 912985158] and ends with 0. Input 1 2 -1 3...

  • Write a program in Java that prompts a user for Name and id number. and then...

    Write a program in Java that prompts a user for Name and id number. and then the program outputs students GPA MAIN import java.util.StringTokenizer; import javax.swing.JOptionPane; public class Main {    public static void main(String[] args) {                       String thedata = JOptionPane.showInputDialog(null, "Please type in Student Name. ", "Student OOP Program", JOptionPane.INFORMATION_MESSAGE);        String name = thedata;        Student pupil = new Student(name);                   //add code here       ...

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