AP Com Sci Chapter 2 Lab Review

// ***************************************************************
// StringManips.java
//
// Test several methods for manipulating String objects
// ***************************************************************

import java.util.Scanner;

public class StringManips
{
public static void main (String[] args)
{
//Declaring scanner object
Scanner scan = new Scanner (System.in);

//All the var declared and shows the phrase being manipulated
String phrase = “This is a String test.”;
int phraseLength; // number of chars in the phrase String
int middleIndex; // index of the middle char in the String
String middle3; //the middle three characters of the string
String firstHalf; // first half of the phrase String
String secondHalf; // second half of the phrase String
String switchedPhrase; //a new phrase w/ orig halves switched
String city, state; //var to hold city and state input

// compute the length and middle index of the phrase
phraseLength = phrase.length();
middleIndex = phraseLength / 2;

// get the substring for each half of the phrase
firstHalf = phrase.substring(0, middleIndex);
secondHalf = phrase.substring(middleIndex);

// get the substring for the middle three characters of the phrase
middle3 = phrase.substring(middleIndex – 1, middleIndex + 2);

//concat first half onto the end of second half
switchedPhrase = secondHalf.concat(firstHalf);
// replace all spaces in switchedphrase to astericks
switchedPhrase = switchedPhrase.replace (‘ ‘, ‘*’);

//assigning appropriate input to var city and state
System.out.println (“Please enter your city.”);
city = scan.nextLine();
System.out.println (“Please enter your state.”);
state = scan.nextLine();

//Modifying city and state input
state = state.toUpperCase();
city = city.toLowerCase();

// print information about the phrase
System.out.println (“Original phrase: “ + phrase);
System.out.println (“Length of the phrase: “ + phraseLength + ” characters”);
System.out.println (“Index of the middle: “ + middleIndex);
System.out.println (“Character at the middle index: “ +                                                                                            phrase.charAt(middleIndex));
System.out.println (“Switched phrase: “ + switchedPhrase);
System.out.println (“Middle three character: “ + middle3);
System.out.println (state.concat(city.concat(state)));
}
}

Notice that when I instantiated the String phrase, I didn’t have to use the new operator. This is a shortcut for creating new String objects only. You can skip the new operator and go straight to assigning a string to the object. When creating instances of other types of objects, you need to always use the new operator.

Published by

LtDemonLord

Life's too short to let your brain go to waste.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s