Docsity

import java.util.Scanner; public class CourseManager1 ..., Lecture notes of Java Programming

Toggle sidebar
bg1
import java.util.Scanner;
public class CourseManager1 {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter number of students: ");
/* read the number of students ( array size) */
while (/* array size less than 1*/){
System.out.print("Number of students is invalid. Enter" + "number of students: ");
/* read array size again */
}
/* define and declare the array ( double) */
System.out.print("Please enter students' scores: ");
// read the array score
for (int i = 0; i < scores.length; i++){
/* read a score and store it in variable score*/
if (score >= 0 && score <= 100){
scores[i] = score;
}
else {
System.out.println("The score " + score + " you "+
"entered is wrong. Program will" +
" store score 0.");
}//end of if/else
}//end of for
System.out.print("The scores are: ");
/* write a code to output the contents of the array (print the
array score */
System.out.println();
}// end of main method
}// end of CourseManager1 class
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download import java.util.Scanner; public class CourseManager1 ... and more Lecture notes Java Programming in PDF only on Docsity!

import java.util.Scanner; public class CourseManager1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter number of students: "); /* read the number of students ( array size) / while (/ array size less than 1/){ System.out.print("Number of students is invalid. Enter" + "number of students: "); / read array size again / } / define and declare the array ( double) / System.out.print("Please enter students' scores: "); // read the array score for (int i = 0; i < scores.length; i++){ / read a score and store it in variable score/ if (score >= 0 && score <= 100){ scores[i] = score; } else { System.out.println("The score " + score + " you "+ "entered is wrong. Program will" + " store score 0."); }//end of if/else }//end of for System.out.print("The scores are: "); / write a code to output the contents of the array (print the array score */ System.out.println(); }// end of main method }// end of CourseManager1 class

import java.util.Scanner; public class CourseManager2 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter number of students: "); // read the number of students ( array size) while (/* array size less than 1/){ System.out.print("Number of students is invalid. Enter" + "number of students: "); / read array size again / } / define and declare the scores array ( double) / System.out.print("Please enter students' scores: "); /Read scores and store them in scores array if they are valid/ / array grades / = scoreToGrade(scores); System.out.print("The scores/grades are: "); for (int i = 0; i < scores.length; i++){ System.out.print(scores[i] + "/" + grades[i] + " "); / prints each score along with the letter grade using format scores[i] + "/" + grades[i] + " " / } System.out.println(); }// end main //Precondition: all scores in the array are between 0 and 100 public static /return type/ scoreToGrade(/parameter array scores/){ / create array grades of length equal to length of scores/ for (int i = 0; i < scores.length; i++){ / if score >=90 store A in grade / / else if score > 80 store B in grade */

System.out.print("Please enter students' scores: "); for (int i = 0; i < scores.length; i++) { double score = input.nextDouble(); /* validate score. If valid store it in array score. if not valid then print a message ""The score " + score + you entered is wrong. Program will store score 0." if you entered a wrong score. / } }// end read scores //Precondition: all scores in the array are between 0 and 100 public static /return type/ scoreToGrade(/parameter array scores/){ / create array grades of length equal to length of scores/ for (int i = 0; i < scores.length; i++){ / if score >=90 store A in grade / / if score > 80 store B in grade / / if score > 70 store C in grade / else if (scores[i] >= 60) grades[i] = 'D'; else / store F / grades[i] = 'F'; } return grades; }// end method scoreToGrade public void printGrades(){ /write body of method printGrades to print array grades */ System.out.println(); }

public class TestCourseManager3 { public static void main(String[] args) { /create an object cm of Class CourseManager3 / / call method readScores on object cm/ /* call method scoreToGrade on object cm/ / call method printGrades on object cm/ } } import java.util.Scanner; class CourseManager4 { / declare an data member array named scores of type double[] /* declare an data member array named grades of type char[] / public void readScores() { Scanner input = new Scanner(System.in); System.out.print("Enter number of students: "); int numOfStudents = input.nextInt(); while (/ array size less than 1/) { System.out.print("Number of students is invalid. Enter" + "numbe / READ numOfStudents AGAIN / } scores = new double[numOfStudents]; System.out.print("Please enter students' scores: "); for (int i = 0; i < scores.length; i++) { double score = input.nextDouble(); / validate score. If valid store it in array score. if not valid then print a message ""The score " + score + you entered is wrong. Program will store score 0."

// Precondition: scores is not null public /return type/ average(){ /* compute and return average score. Call method sum() above to get total / } } // end class CourseManager public class TestCourseManager4 { public static void main(String[] args) { /create an object cm of Class CourseManager3 / / call method readScores on object cm/ / call method scoreToGrade on object cm/ / call method printGrades on object cm/ / print average by calling method average on object cm/ } } import java.util.Scanner; class CourseManager5 { / Declare the class data members as shown in the UML/ CourseManager5() { / Write the constructor that initializes the attributes and creates the arrays each of size 100. here / } public / method modifier/ addStudent(/ parameters list /) { / 1- check if nStudents is less than the maximum size to add a new student else print the message: System.out.println("ERROR: COURSE IS FULL");/ } public / method modifier / displayStudent(/ parameters list /) { / print the id, name , and scores of the index i passed to the method / } public / method modifier */ getNStudents() {

/* retun nStudents / } } public class TestCourseManager5 { public static void main(String[] args) { Scanner kb = new Scanner(System.in); / creat a CourseManager5 object named c1/ / use for loop to do the following 3 times: 1 - ask the user to enter student information ID, name, and score / //System.out.println("Please enter the ID, name, and score of a student: "); / 2- use the scanner to get the id, name , and score / / 3- add student using the method "addStudent" / / display all students in class. / } } import java.util.Scanner; class CourseManager6 { / Declare the class data members as shown in the UML/ public CourseManager6() { / Write the constructor that initializes the attributes and creates the arrays each of size 100 nStudents should be initilized to 0. here / } public / method type / addStudent(/ parameters list /) { / 1- check if nStudents is less than the maximum size to add a new student else print the message: System.out.println("ERROR: COURSE IS FULL");/ / 2- check if the student is not already in the list methos findStudentName if the student is not in the list, add the new if the student is already in the list print System.out.println("ERROR: STUDENT ALRAEDY THERE"); / } public / method type / findStudentName(/ parameters list /) { / 1- use for loop to check the student list / / 2- check if the name in the array "names[]" is equal to the name passed to the method if you find the name return the index number otherwise return - 1 / } public / method type / displayStudent(/ parameters list /) { / print the id, name , and scores of the index i passed

if the student is not in the list, add the new student. if the student is already in the list print System.out.println("ERROR: STUDENT ALRAEDY THERE"); or copy from CourseManager6 and paste here* / } public / method type / findStudentName(/ parameters list /) { / 1- use for loop to check the student list / / 2- check if the name in the array "names[]" is equal to the name passed to the method if you find the name return the index number Otherwise return - 1 or copy from CourseManager6 and paste here / } public / method type / findAverageScore(/ parameters list /) { / 1- check if nStudents is grater than zero which means that the list is not empty if the list is empty return 0 / / 2- use for loop to count the sum of all scores / / 3- return the average ==> sum/nStudents / } public / method type / findMaxScoreIndex(/ parameters list /) { / create integer max = 0; /* 2- check if nStudents is grater than zero which means that the list is not empty if nStudents is equla to or less than 0 make max = - 1 / / 3- use for loop to compare the score of every student to the max score 3.1- the max score should be initialized to be equal to the first element max = 0; the comparison should be like this if (score [i]>score[max]) => max = i; / / 4- return max / } public / method type / displayStudent(/ parameters list /) { / print the id, name , and scores of the index i passed to the method / } public / method type / getNStudents(/ parameters list /) { / return nStudents / } } public class TestCourseManager7{ public static void main(String[] args) { Scanner kb = new Scanner(System.in); / creat CourseManager7 object named c1 */

/* use for loop to do the following 3 times: /* 1- ask the user to enter student information ID, name, and score / //System.out.println("Please enter the ID, name, and score of a student: "); / 2- use the scanner to get the id, name , and score / / 3 - add student using the method "addStudent" / / after the loop print the class average score / / print the student information whos has the max score using the method displayStudent */ } }