JAVA PROBLEM:
I. The Problem Statement for String functions
In this lab part of the lab, you are asked to write a class (you are free to name it anything you like) that asks a user for input string. Then, output a substring to the screen.
[Note from student who posted this question to clarify the professor's assignment: There are 2 different problems in the assignment, the first one asked to write a program use Scanner class and substring() method from the String class and name however you like, but follow the example in the Test Case 1.
The second problem is to change the code follow the instructions below and answer the question whether we seen any outputs on the output screen?]
Requirements:
Use Scanner class and use substring() method from the String class.
Test Case 1
Input string:
“Welcome to ICS 110 Lab 3 and it is interesting.”
Output:
ICS 110 Lab 3
SCORE YOUR POINTS BY DOING THE FOLLOWING
Cut and paste your complete Java source code in the space below (3 pts).
Cut and paste your run output in the space below (show your output to your lab instructor if asked) (4 pts).
II. The Problem Statement for Methods
In this lab part of the lab, you are going to learn Java methods by changing given code and following instructions. The following is detailed steps:
1. Open Eclipse and create a new project and name it 'Lab3' and create a class called 'Lab4' inside the 'Lab3' project.
2. Write the checkAge method definition after the main method as follows:
public class Lab3 {
public static void main(String[]args) {
// TODO Auto-generated method stub
}
public static void checkAge(int age){
if (age < 18)
System.out.println("Minor");
else if (age < 65)
System.out.println("Adult");
else
System.out.println("Senior");
}
}
3. In the main method, write statement to ask the user for his age (produce appropriate prompts), read the age, and then call the checkAge method with the user's age as a parameter. Test your program. The requirement for this step is to use Scanner class.
4. Follow the following steps to change the definition of the checkAge method such that instead of writing a message on the screen, the method returns a String as output.
output = "Minor";
5. After the above change, do the debug and make sure no compilation error and logical error.
SCORE YOUR POINTS BY DOING THE FOLLOWING
Cut and paste your complete Java source code in the space below (4 pts).
Cut and paste your run output in the space below (show your output to your lab instructor if asked) (4 pts).
Question to ponder (no extra points): do you seen any outputs on the output screen?
Hand to your instructor the following:
1) this document (with all the bold faced sections finished), with output results.
1)
Ans:-
import java.util.Scanner;
/**
* The Class StringPractice.
*/
public class StringPractice {
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
Scanner scan = new
Scanner(System.in);
System.out.println("Input String:
");
String input =
scan.nextLine();
// substring method
String subString =
input.substring(11, 24);
System.out.println(subString);
scan.close();
}
}
Input String:
Welcome to ICS 110 Lab 3 and it is interesting.
ICS 110 Lab 3
2)
Ans :-
import java.util.Scanner;
/**
* The Class Lab4.
*/
public class Lab4 {
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
Scanner scan = new
Scanner(System.in);
System.out.print("Please input age:
");
int age = scan.nextInt();
System.out.println(checkAge(age));
scan.close();
}
/**
* Check age.
*
* @param age the age
*/
public static String checkAge(int age) {
String output = "";
if (age < 18)
output = "Minor";
else if (age < 65)
output = "Adult";
else
output = "Senior";
return output;
}
}
/***********output**************/
Please input age: 75
Senior
Please let me know if you have any doubt or modify the answer, Thanks :)
Get Answers For Free
Most questions answered within 1 hours.