Question

Please help. I need a very simple code. For a CS 1 class. Write a program...

Please help. I need a very simple code. For a CS 1 class.

Write a program in Java and run it in BlueJ according to the following specifications:

  • The program reads a text file with student records (first name, last name and grade).
  • Then it prompts the user to enter a command, executes the command and loops. The commands are the following:
    • "printall" - prints all student records (first name, last name, grade).
    • "firstname name" - prints all students with first name that starts with the provided parameter name.
    • "lastname name" - prints all students with first name that starts with the provided parameter name.
    • "interval m n" - prints all students with grades in the interval [m, n].
    • "end" - exits the loop the terminates the program.
  • For example, if the input text file is students.txt and the user enters "printall" the program prints the following:
John Smith 90 
Barack Obama 95 
Al Clark 80 
Sue Taylor 55 
Ann Miller 75 
George Bush 58 
John Miller 65 

If the user enters "firstname A" the program prints the following:  

Al Clark 80 
Ann Miller 75 

If the user enters "lastname Miller" the program prints the following:

Ann Miller 75 
John Miller 65  

If the user enters "interval 75 90" the program prints the following:

John Smith 90 
Al Clark 80 
Ann Miller 75 

Requirements and restrictions:

  1. Use the Student.java and Students.java classes from the course website to represent and process student records and modify them accordingly:
    • Define and use an array of objects of class Student to store all student records read from the file.
    • In class Students define the following methods that accept the array of students and other proper parameters and do the following:
      • Prints all student records in the array.
      • Prints all students with first name that starts with the provided parameter. Hint: use the indexOf(String str) method form class String.
      • Prints all students with last that starts with the provided parameter. Hint: use the indexOf(String str) method form class String.
      • prints all students with grades in the interval [m, n].
    • Do NOT use the array of Students directly in the main method, all processing must be done by the methods described above using the array passed to them as a parameter.
    • Enforce the encapsulation principle.
    • No need to verify the user input.
  2. When you write your program
    • use proper names for the variables suggesting their purpose.
    • format your code accordingly using indentation and spacing.
    • use multiple line comment in the beginning of the code and write your name, e-mail address, class, and section.
    • for each line of code add a short comment to explain its meaning.

Extra credit (up to 2 points):

The extra credit will be given for extending the program to accept the "sort" command that will print the array of students sorted by grade. Use the bubble sort algorithm as implemented in array.java.

students:

(John Smith 90

Barack Obama 95

Al Clark 80

Sue Taylor 55

Ann Miller 75

George Bush 58

John Miller 65)

Array: (

// A program demonstrating the use of arrays
// arrays parameters, min, max and bubble sort

import java.util.Scanner;

class array {

public static void main (String[] args)
{

Scanner scan = new Scanner(System.in);

int [] a = new int [100];
int i, n = 0, min, max;
float total = 0;

System.out.println ("Enter integers separated by blanks (non-integer to end):");

while (scan.hasNextInt()) {
a[n] = scan.nextInt();
n = n + 1;
}

System.out.println ("You entered " + n + " numbers");
  
swap(a,0,n-1);
  
System.out.println ("After swapping elements 0 and " + (n-1));
for (i=0; i<n; i++)
System.out.print (a[i] + " ");
System.out.println ();

min = a[0];
max = a[0];
for (i = 1; i < n; i++) {
if (max < a[i]) max = a[i];
if (min > a[i]) min = a[i];
total = total + a[i];
}

System.out.print ("Min = " + min + ", Max = " + max + ", Average = " + total/n);

int t, swaps = 0;
do {
swaps = 0;
for (i=0; i<n-1; i++)
if (a[i]>a[i+1]) {
t=a[i];
a[i]=a[i+1];
a[i+1]=t;
swaps++;
}
} while (swaps>0);

System.out.print ("\nSorted: ");
for (i=0; i<n; i++)
System.out.print (a[i] + " ");
System.out.println ();

}

public static void swap(int arr[], int i, int j)
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
)

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


As per the problem statement I have solve the problem. Please let me know if you have any doubts or you want me to modify the answer. And if you find this answer useful then don't forget to rate my answer as thumps up. Thank you! :)

import java.util.Scanner;
import java.io.*;

public class StudentDatabaseDriver
{
    public static void main (String[] args) throws IOException {
        String studentName, grades;
        int studentgrade, totalStudents=0, totalexcellentStudents=0, totalOkStudents=0, totalFailStudents=0, countStudents=0, countExcellentStudents=0, countOkStudents=0, countFailStudents=0;

        double averageGrades, averageGradesOfExcellentStudents, averageGradesOfOkStudents, averageGradesOfFailStudents;
        Scanner studetnsDataInput = new Scanner(new File("/Users/swapnil/IdeaProjects/StudentDatabase/src/students.txt"));
        while (studetnsDataInput.hasNext())
        {
            //set student name format first nam and last name
            studentName = studetnsDataInput.next() + " " + studetnsDataInput.next();

            //grades of current student
            studentgrade = studetnsDataInput.nextInt();

            //students grades
            grades = studentGradeName(studentgrade);

            //new student object for current student
            Student student = new Student(studentName, studentgrade, grades);
            //counts and totals Excellent students
            if (grades == "Excellent")
            {
                totalexcellentStudents = totalexcellentStudents + studentgrade;
                countExcellentStudents++;
            }
            //counts and totals OK students
            if (grades == "OK")
            {
                totalOkStudents = totalOkStudents + studentgrade;
                countOkStudents++;
            }
            //counts and totals Fail students
            if (grades == "Fail")
            {
                totalFailStudents = totalFailStudents + studentgrade;
                countFailStudents++;
            }

            //prints out students information
            System.out.println(student);

            totalStudents = totalStudents + studentgrade;
            countStudents++;
        }
        //all type of average grades calculations
        averageGrades = (double)totalStudents/countStudents;
        averageGradesOfExcellentStudents = (double)totalexcellentStudents/countExcellentStudents;
        averageGradesOfOkStudents = (double)totalOkStudents/countOkStudents;
        averageGradesOfFailStudents = (double)totalFailStudents/countFailStudents;
        //Prints for user
        System.out.println("There are total " + countStudents + " students with average grade " + averageGrades);
        System.out.println("There are total" + countExcellentStudents + " excellent students with average grade " + averageGradesOfExcellentStudents);
        System.out.println("There are total " + countOkStudents + " OK students with average grade " + averageGradesOfOkStudents);
        System.out.println("There are total" + countFailStudents + "failure students with average grade " + averageGradesOfFailStudents);
    }
    //method to check the student grades type
    public static String studentGradeName(int studentGrade)
    {

        if (studentGrade > 89){return "Excellent";}
        if (studentGrade > 60 && studentGrade < 89){return "OK";}
        if (studentGrade < 60) {return "Failure";}
        return "Null";
    }

}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
class Student
{
    private String studentName, grades;
    private int studentGrades;
    //method for student object
    public Student (String studName, int studGrades, String grades)
    {
        studentName = studName;
        studentGrades = studGrades;
        this.grades = grades;
    }
    //a method to print student information
    public String toString()
    {
        return studentName + "\t" + studentGrades + "\t" + grades;
    }


}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


Program Output:StudentDatabase - StudentDatabaseDriver.java StudentDatabase src Student DatabaseDriver m main Main StudentDatabaseDriver.jav

Add a comment
Know the answer?
Add Answer to:
Please help. I need a very simple code. For a CS 1 class. Write a program...
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
  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • I need to change the following code so that it results in the sample output below...

    I need to change the following code so that it results in the sample output below but also imports and utilizes the code from the GradeCalculator, MaxMin, and Student classes in the com.csc123 package. If you need to change anything in the any of the classes that's fine but there needs to be all 4 classes. I've included the sample input and what I've done so far: package lab03; import java.util.ArrayList; import java.util.Scanner; import com.csc241.*; public class Lab03 { public...

  • ***Please complete the code in C*** Write a program testArray.c to initialize an array by getting...

    ***Please complete the code in C*** Write a program testArray.c to initialize an array by getting user's input. Then it prints out the minimum, maximum and average of this array. The framework of testArrav.c has been given like below Sample output: Enter 6 numbers: 11 12 4 90 1-1 Min:-1 Max:90 Average:19.50 #include<stdio.h> // Write the declaration of function processArray int main) I int arrI6]i int min-0,max-0 double avg=0; * Write the statements to get user's input and initialize the...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • I have a multithreaded java sorting program that works as follows: 1. A list of double...

    I have a multithreaded java sorting program that works as follows: 1. A list of double values is divided into two smaller lists of equal size 2. Two separate threads (which we will term sorting threads) sort each sublist using a sorting algorithm of your choice 3. The two sublists are then merged by a third thread merging thread that merges the two sublists into a single sorted list. SIMPLE EXECUTION >java SortParallel 1000 Sorting is done in 8.172561ms when...

  • Write and submit the source code for the following program. The program will use an integer...

    Write and submit the source code for the following program. The program will use an integer array of size 10 to store the prices of smartphones. It will then determine and print the prices of the most expensive and cheapest phones. Use the following variables: int[] prices = new int[10]; // Array of smartphone prices Assignment Ask the user for the price of each smartphone (using a for loop) Sort the list of smartphones (once) from low to high price...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • the code needs to be modifed. require a output for the code Java Program to Implement...

    the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...

  • write a program which include a class containing an array of words (strings).The program will search...

    write a program which include a class containing an array of words (strings).The program will search the array for a specific word. if it finds the word:it will return a true value.if the array does not contain the word. it will return a false value. enhancements: make the array off words dynamic, so that the use is prompter to enter the list of words. make the word searcher for dynamic, so that a different word can be searched for each...

  • I need help asking the user to half the value of the displayed random number as...

    I need help asking the user to half the value of the displayed random number as well as storing each number generated by the program into another array list and displayed after the game is over at the end java.util.*; public class TestCode { public static void main(String[] args) { String choice = "Yes"; Random random = new Random(); Scanner scanner = new Scanner(System.in);    ArrayList<Integer> data = new ArrayList<Integer>(); int count = 0; while (!choice.equals("No")) { int randomInt =...

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