1. Write a complete Java method called sumNums that takes no parameters and retu
ID: 3697875 • 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
1.
import java.util.*;
class sumNumTest
{
public static int sumNums()
{
int i=0,s=0,n;
Scanner scan = new Scanner(System.in);
do
{
System.out.println("Enter Number");
n=scan.nextInt();
if(n>0)
s=s+n;
else
return s;
}while(true);
}
public static void main(String args[])
{
int s=sumNums();
System.out.println("Sum is ="+s);
}
}
2.
import java.util.*;
class passwordTest
{
public static boolean checkPassword(String p)
{
int n=p.length();
if(p.contains("[0-9]+") && n>8)
return true;
else
return false;
}
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter Password");
String p=scan.next();
if(checkPassword(p))
System.out.println("Password is correct");
else
System.out.println("Password is incorrect");
}
}
3.
import java.util.*;
class passwordTest
{
public static boolean autheticate(String u, String p)
{
if(u.equals("Chucky") && p.equals("Cheese1")
return true;
else
return false;
}
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter Username");
String u = scan.next();
System.out.println("Enter Password");
String p=scan.next();
if(autheticate(u, p))
System.out.println("USer Authenticated successfull");
else
System.out.println("User Authentication failed");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.