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:

USE JAVA PLEASE And answer all questions Question 5.1 1. Write a program that reads in...
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 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 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 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() { . 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 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 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 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 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 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 ...