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

1. Write a complete Java method called sumNums that takes no parameters and retu

ID: 3696992 • Letter: 1

Question

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.

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

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.

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.

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.

Explanation / Answer

CalcOperations.java


import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class CalcOperations {

   /**
   * @param args
   */
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       System.out.println("----------sumNums()-----------");
       int total = sumNums();
       System.out.println("The total is : "+total);
       Scanner scan = new Scanner(System.in);
      
       System.out.println("----------checkPassword()-----------");
       System.out.println("Enter Pasword : ");
       String pass = scan.next();
      
       boolean status = checkPassword(pass);
       if(status)
       System.out.println("Valid Password");
       else
       System.out.println("Invalid Password");
       System.out.println("----------authenticate()-----------");
       status = authenticate("Chucky", "Cheese1");
       System.out.println("Authenticate : "+status);
       System.out.println("----------getScoreSheet()-----------");
       getScoreSheet(2, 2);
       System.out.println("Done. 2-dimensional array initialized each value in the array to 100.00 ");
       System.out.println("----------writeList()-----------");
       ArrayList<String> list = new ArrayList<String>();
       list.add("aaaa");
       list.add("bbbb");
       list.add("cccc");
       list.add("dddd");
       list.add("eeee");
       writeList(list);
   }
   public static int sumNums(){
  
   int total = 0;
       int n = 0;
       Scanner scan = new Scanner(System.in);
       System.out.println("Please enter a series of positive integer values (-1 to Quit):");
       do{
           n = scan.nextInt();
           if(n == -1) break;
           total = total + n;
          
       }while(true);
       return total;
   }
   public static boolean checkPassword (String password){
  
   int n = password.length();
       boolean found = false;
       if(n >= 8){
           int i = 0;
           while(i <= n){
               if(Character.isDigit(password.charAt(i))){
                   found = true;
                   break;
               }
               i++;
           }
           return found;
       }
       else
           return false;
   }
   public static boolean authenticate (String username, String password){
  
   if(username.equals("Chucky") && password.equals("Cheese1")){
           return true;
       }
       else{
           return false;
       }
   }
   public static double[][] getScoreSheet (int rows, int columns){
  
   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;
      
   }
   public static void writeList (ArrayList<String> list){
  
   try{
       String fileName = "list.txt";
       File file = new File(fileName);
       FileWriter fw = new FileWriter(file);
       String s = "";
       for(String str: list){
           s = s + str +" ";
       }
       fw.write(s);
       fw.flush();
       fw.close();
       System.out.println("Data has been saved in file");
   }
   catch(IOException e){
       System.out.println("Error: "+e.toString());
   }  
   }
}

Output:

----------sumNums()-----------
Please enter a series of positive integer values (-1 to Quit):
3
4
5
6
7
6
5
41

12
34
-1
The total is : 123
----------checkPassword()-----------
Enter Pasword :
Suresh12!@
Valid Password
----------authenticate()-----------
Authenticate : true
----------getScoreSheet()-----------
Done. 2-dimensional array initialized each value in the array to 100.00
----------writeList()-----------
Data has been saved in file

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote