Download Program 2

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Program 1:
Implement a class Student. For the purpose of this exercise, a student has a name
and a total quiz score. Supply an appropriate constructor and methods getName(),
addQuiz( ), getTotalQuizScore( ), and getAverageScore( ). To compute the
getAverageQuizScore( ) you also need to store the number of quizzes that a student
took.
Program 2:
Write a program that request the user to input string then determine and print the middle
character of the string if the length of the string is odd. If the length of the string is even
then determine and print the two middle characters. For example; if the string is
“Seahawks” the program should print “ha”. If the string is “holiday” it should print “i”.
Program 3:
Write a program that reads a sequence of integers from a file into an array and then
computes the alternating sum and difference of all elements in the array. For example,
if the program is executed with the input data
1 4 9 16 9 7 4 9 11
then it computes
1 – 4 + 9 – 16 + 9 – 7 + 4 – 9 + 11 = -2.
You can assume there will be no more than 100 integers in the file.
CSS 161 Fundamentals of Computing
Page 1
Program 1:
public class Student
{
private String studentName;
private double totalScore;
private int quizCount;
public Student(String name)
{
studentName = name;
totalScore = 0;
quizCount = 0;
}
public String getName()
{
return studentName;
}
public void addQuiz(int quizScore)
{
totalScore = totalScore + quizScore;
quizCount = quizCount + 1;
}
public double getTotalScore()
{
return totalScore;
}
public double getAverageScore()
{
return totalScore / quizCount;
}
}
CSS 161 Fundamentals of Computing
Page 2
Program 2:
import java.util.Scanner;
public class MiddleExtractor
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Please enter a string: ");
String str = in.nextLine();
int position;
int length;
if (str.length() % 2 == 1)
{
position = str.length() / 2;
length = 1;
}
else
{
position = str.length() / 2 - 1;
length = 2;
}
String result = str.substring(position, position + length);
System.out.println("Middle: " + result);
}
}
CSS 161 Fundamentals of Computing
Page 3
Program 3:
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class AlternatingSum {
public static void main (String[] args) {
Scanner inputFile = null;
try {
inputFile = new Scanner(new FileInputStream("input.txt"));
}
catch (FileNotFoundException e) {
System.out.println("File not found or not opened.");
System.exit(0);
}
int [ ] numbers = new int[100];
int i = 0;
while (inputFile.hasNext()) {
numbers[i] = inputFile.nextInt();
i++;
}
int sum = 0;
for (int j = 0; j < i; j++) {
if (j % 2 == 0)
sum = sum + numbers[j];
else
sum = sum - numbers[j];
}
System.out.println("Alternating Sum: " + sum);
}
}
CSS 161 Fundamentals of Computing
Page 4
Related documents