Question

JAVA PROBLEM: I. The Problem Statement for String functions In this lab part of the lab,...

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.

  1. Change the method header by setting the output data type to String instead of void.
  2. Declare a String variable, called output, inside the checkAge method.
  3. Change the body of the if-else branches to assign the string to the variable output instead of writing the message on the screen. For example, the first body of the first branch will be as follows:

output = "Minor";

  1. Add a return statement at the end of the checkAge method to return the output string.

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.

Homework Answers

Answer #1

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 :)

Know the answer?
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
Similar Questions
IN JAVA Methods*: Calorie estimator Write a method ActivityCalories that takes a string indicating an activity...
IN JAVA Methods*: Calorie estimator Write a method ActivityCalories that takes a string indicating an activity (sit, walk, jog, bike, swim) and duration in minutes (integer), and returns the estimated calories expended (double). Calories per minute for a 150 lb person (source): sit: 1.4 walk: 5.4 run: 13.0 bike: 6.8 swim: 8.7 If the input is sit 2, the output is 2.8 Hints: Use an if-else statement to determine the calories per minute for the given activity. Return the calories...
7.6 LAB: Exception handling to detect input String vs. Integer The given program reads a list...
7.6 LAB: Exception handling to detect input String vs. Integer The given program reads a list of single-word first names and ages (ending with -1), and outputs that list with the age incremented. The program fails and throws an exception if the second input on a line is a String rather than an Integer. At FIXME in the code, add a try/catch statement to catch java.util.InputMismatchException, and output 0 for the age. Ex: If the input is: Lee 18 Lua...
write a program (in Java/Eclipse to loop through a string character by character. If the character...
write a program (in Java/Eclipse to loop through a string character by character. If the character is a letter, print a question mark, otherwise print the character. Use the code below for the message string. This will be the first string that you will decode. Use the String class method .charAt(index) and the Character class method .isLetter(char). (You can cut and paste this line into your program.) String msg = "FIG PKWC OIE GJJCDVKLC MCVDFJEHIY BIDRHYO.\n";
I need this before the end of the day please :) In Java 10.13 Lab 10...
I need this before the end of the day please :) In Java 10.13 Lab 10 Lab 10 This program reads times of runners in a race from a file and puts them into an array. It then displays how many people ran the race, it lists all of the times, and if finds the average time and the fastest time. In BlueJ create a project called Lab10 Create a class called Main Delete what is in the class you...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the 2 in 2 for $1.99. private double groupPrice; //Part of price, like the $1.99 in 2 for $1.99. private int numberBought; //Total number being purchased. public Purchase () { name = "no name"; groupCount = 0; groupPrice = 0; numberBought = 0; } public Purchase (String name, int groupCount, double groupPrice, int numberBought) { this.name = name; this.groupCount = groupCount; this.groupPrice = groupPrice; this.numberBought...
I am having some trouble writing some java code involving strings. I have included the code...
I am having some trouble writing some java code involving strings. I have included the code provided by my professor below. that has the instructions in it as well public class StringExercise { public static void main(String[] args) { String given = "The quick brown fox jumped over the moon!"; String given2 = "mary"; String given3 = "D"; //Write a Java code snippet to check (print a boolean) whether the given String ends with the contents of "oon!", see endsWith...
Hello, I am trying to create a Java program that reads a .txt file and outputs...
Hello, I am trying to create a Java program that reads a .txt file and outputs how many times the word "and" is used. I attempted modifying a code I had previously used for counting the total number of tokens, but now it is saying there is an input mismatch. Please help! My code is below: import java.util.*; import java.io.*; public class Hamlet2 { public static void main(String[] args) throws FileNotFoundException { File file = new File("hamlet.txt"); Scanner fileRead =...
Java program question! If I pass null to a method, how can I use the passed...
Java program question! If I pass null to a method, how can I use the passed value to compare. Here is example code, it might be wrong, please make it could run on eclipse. Public class hw{ public static void main (String args[]) { method1(); } private static void method1() { System.out.println(method(null)); } public static String method(int[] a) { return null; } What I want to get in the output is just "()". It is not just to type the...
Here is the code I am supposed to Analyze: // If we want to use the...
Here is the code I am supposed to Analyze: // If we want to use the Scanner class (type) to get user input, we need // to import the following Java package that includes the Scanner class // definitions. We do not have to add an import statement to use the // print() or println() methods of the System object, because they // are built in. import java.util.Scanner; public class PRG420Week1_AnalyzeAssignment { /* The main() method you see below is...
public class Programming { public static void main(String args[]) { System.out.println("I am learning how to program.");...
public class Programming { public static void main(String args[]) { System.out.println("I am learning how to program."); } } Compile this and execute this program then modify it to display I am learning how to program in Java. Change class name to Javaprogramming.java. compile and execute. Modify the Javaprogramming class so it prints two lines of output. Change name to Awesome. Add second output statement that displays That's awesome ! Save as awesome.java then compile and execute