CS 150, Fall 2017
Programming Assignment - P4
Introduction to Strings and String Methods
Due - October 3rd, 2017 at 6:00pm
Late - October 4th, 2017 at 8:00am
Objectives of this Assignment
- To write a program that uses the String class of Java,
- to declare a String object and initialize it by user input, and
- to learn how to implement a few basic String methods.
Instructions
Create a Java program called P4 with a class named P4, which after created
should contain a file named P4.java in the src folder. An example of
the code is shown below. All of the Java code you write should be contained
in the main method. For this assignment, you must follow the directions below
exactly:
- Create a Java project in Eclipse called P4.
- Create a new class in the P4 project and let Eclipse create a main method in P4.java.
- Copy the comment block below and personalize it if you have not set up a default comment header.
All variables and code should reside in the main method!
- Instantiate an object (variable) named testString of type String to "".
- Create a Scanner object to read console input.
- Prompt the user for the string literal to be assigned to testString by printing "Program String: ".
- Next, write a line of code that will assign an entire line of text to testString once the program is run.
- When you run P4, reassign testString to "Java is fun!".
- The spacing in the string literal above must match exactly.
- Using String methods, print the following lines to the console:
- First line: the String testString.
- Second line: the length of the testString.
- Third line: the index of the first 'a' in testString.
- Fourth line: the character at index 2.
- Fifth line: testString as uppercase.
- Sixth line: the substring starting at index 3 and including the rest of the testString.
- Seventh line: the substring starting at index 2 and ending at index 5 (inclusive).
- Do not hard code any of the String values to be printed to the console.
Program Structure
// P4 Assignment
// Author: Russ Wakefield
// Date: 9/24/2017
// Class: CS150
// Email: waker@colostate.edu
import java.util.Scanner;
public class P4 {
public static void main(String[] args) {
// Declare String object.
// Instantiate Scanner (declare and initialize).
// Prompt user and initialize testString.
// Print output to console.
// Close scanner
} // end main
} // end class P4
Sample Output
Your program should print eight lines. This includes the first, where the user enters
the value for testString. User input is shown in blue.
Program String: Java is fun!
1: Java is fun!
2: 12
3: 1
4: v
5: JAVA IS FUN!
6: a is fun!
7: va i
Specifications
- Work on your own, as always.
- The name of the source code file must be exactly
P4.java
- Name the file exactly - upper and lower case matters!
- Comments at the top as shown above.
- Assignments should be implemented using Eclipse.
- Assignments should be implemented using Java, version 1.8.
- Make sure your code runs on machines in the CSB 120 lab.
- Turn in through the Checkin tab.
- Read the syllabus for the late policy.
- We will be checking programs for plagiarism, so please don't copy from anyone else.
Grading Criteria
- 100 points for perfect submission.
- 0 points for no submission, will not compile, submitted class file, etc.
- Preliminary Tests
Preliminary testing tests lines 1-7 .
- testCompile: checks that program compiles. (0 points)
- lineTests: verify correct output based on String method used.
- test1 - test6: 12 points each
- Final Tests
Final testing tests lines 8-9.
- test7: 14 points
- test8: 14 points
- Final grading includes the preliminary tests.
Submit P4.java to Checkin.