Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA QUIZ package popQ; public class Methods { /* 1. Write a complete Java metho

ID: 3771634 • Letter: J

Question

JAVA QUIZ

package popQ;

public class Methods {


/*
1. Write a complete Java method called sumNums that takes no parameters and returns an integer.
The method uses a do-while loop to ask the user to enter a series of positive integer values and keeps a running total.
Use a sentinel (-1) to allow the user to indicate when they are finished.
The method returns the total.
*/
  
// Write your method here
  
  
  

/*
2. Write a complete Java method called checkPassword that takes a String as
a parameter (password) and returns a boolean. First, check that the password is
at least 8 characters long. Then use a while loop to check that the
password contains at least one digit. If the password has the correct length
and contains a digit, return true, otherwise return false
*/
  
// Write your method here
  
  
  
  
/*
3. Write a complete Java method called authenticate that takes two Strings (username, password)
as parameters and returns a boolean. If the username parameter has the value "Chucky" and
the password parameter has the value "Cheese1", return true. Otherwise return false.
*/

// Write your method here   

  
  
  
/*
4. Write a complete Java method called getScoreSheet that takes two ints
as parameters (rows and columns) and constructs a 2-dimensional array
of doubles (calles scores) with a size based on the rows and columns parameters.
Use a nested loop to initialize each value in the array to 100.00.
The method returns the scores array.
*/
  
// Write your method here


/*
5. Write a complete Java method called writeList that takes an ArrayList of Strings as a parameter
and writes each String as a line in a text file.
Make sure to handle the Exception that might be thrown and print the error message to the
command line.
*/
  
// Write your method here
  
  
}

Explanation / Answer

Implemented parts 1,2,3 and 4

package assignment;

import java.util.Scanner;

public class Methods {


   public static void main(String args[]) {
       Methods methods = new Methods();
       System.out.println("Testing sum function");
       int sum = methods.sumNums();
       System.out.println(" Sum of numbers : "+sum);
      
       System.out.println("Testing checkPassword");
       System.out.println(" Password: P@ssw@rd : "+methods.checkPassword("P@ssw@rd"));
       System.out.println(" Password: P@s1 : "+methods.checkPassword("P@s1"));
       System.out.println(" Password: P@s1abcedf : "+methods.checkPassword("P@s1abcedf"));
      
      
       System.out.println("Testing authenticate");
       System.out.println(" Username: John, Password: Chunky "+methods.authenticate("John", "Chunky"));
       System.out.println(" Username: Chucky, Password: Cheese1 "+methods.authenticate("Chucky", "Cheese1"));
      
       System.out.println("Testing getScoreSheet");
       double scores[][] = methods.getScoreSheet(5, 4);
   }
        public Methods() {
          
        }


        /*
        1. Write a complete Java method called sumNums that takes no parameters and returns an integer.
           The method uses a do-while loop to ask the user to enter a series of positive integer values and keeps a running total.
           Use a sentinel (-1) to allow the user to indicate when they are finished.
           The method returns the total.
        */
      
        // Write your method here
      
      
       public int sumNums() {
           int total = 0;
           int input = 0;
           Scanner scan = new Scanner(System.in);
           do {
               System.out.println("Enter a positive integer ( Negative value(-1 or other) to quit): ");
               input = scan.nextInt();
               if(input >= 0)
                   total += input;
           } while(input >= 0);
          
           return total;
       }
      

        /*
        2. Write a complete Java method called checkPassword that takes a String as
           a parameter (password) and returns a boolean. First, check that the password is
           at least 8 characters long. Then use a while loop to check that the
           password contains at least one digit. If the password has the correct length
           and contains a digit, return true, otherwise return false
        */
      
        // Write your method here
      
        public boolean checkPassword(String password) {
           if(password != null && password.length() >= 8) {
               int i = 0;
               while(i < password.length()) {
                   if(Character.isDigit(password.charAt(i)))
                       return true;
                   i++;
               }
           }
           return false;
        }
      
      
        /*
        3. Write a complete Java method called authenticate that takes two Strings (username, password)
           as parameters and returns a boolean. If the username parameter has the value "Chucky" and
           the password parameter has the value "Cheese1", return true. Otherwise return false.
        */

        // Write your method here   
        public boolean authenticate(String username, String password) {
          
           if("Chucky".equals(username) && "Cheese1".equals(password) )
               return true;
           return false;
        }
      
      
      
        /*
        4. Write a complete Java method called getScoreSheet that takes two ints
           as parameters (rows and columns) and constructs a 2-dimensional array
           of doubles (calles scores) with a size based on the rows and columns parameters.
           Use a nested loop to initialize each value in the array to 100.00.
           The method returns the scores array.
        */
      
        // Write your method here

        public double[][] getScoreSheet(int rows, int columns) {
           if(rows <= 0 || columns <= 0)
               return null;
           double[][] scores = new double[rows][columns];
           for(int i= 0; i < rows; i++)
               for(int j = 0; j < columns; j++)
                   scores[i][j] = 100.0;
          
           return scores;
        }
  
}

--output--

Testing sum function
Enter a positive integer ( Negative value(-1 or other) to quit):
10
Enter a positive integer ( Negative value(-1 or other) to quit):
20
Enter a positive integer ( Negative value(-1 or other) to quit):
-1
Sum of numbers : 30
Testing checkPassword
Password: P@ssw@rd : false
Password: P@s1 : false
Password: P@s1abcedf : true
Testing authenticate
Username: John, Password: Chunky false
Username: Chucky, Password: Cheese1 true
Testing getScoreSheet