STIA1123 Exercise 1 - Revision String
1. Fill in the blanks in the program below as follows:
(a) declare
the variable town as a reference to a String object and initialize it to "
UUM Sintok".
(b) write
an assignment statement that invokes the length method of the String class to find the length of the college String object and assigns the result to the stringLength variable
(c) complete
the assignment statement so that change1 contains the same characters as college but all in upper case
(d) complete
the assignment statement so that change2 is the same as change1 except all capital O's are replaced with the asterisk (*) character.
(e) complete
the assignment statement so that change3 is the concatenation of college and town (use the concat method of the String class rather than the + operator)
//
**************************************************
//
StringExercise.java
//
//
Play with String objects
//
**************************************************
public
class StringExercise
{
public static void main (String[] args)
{
String college = new String ("College of
Arts and Sciences");
String town = new String (“ UUM Sintok”);
//String town = “ UUM Sintok”; // part (a)
int stringLength;
String change1, change2, change3;
stringLength = college.length();// part (b)
System.out.println (college + " contains
" + stringLength + " characters.");
change1 = college.toUpperCase(); // part (c)
System.out.println ("The string is all in
upper case: " + change1);
change2 = change1.replaceAll(“/”, “.”);//change1.replaceAll(“O”,”*”);
// part (d)
System.out.println
("All capital O's are replaced with the asterisk character: " +
change2);
change3 = college.concat(town); // part (e)
System.out.println ("The final string is
" + change3);
}
}
2.
Write a program called “FunnyString” that
asks the user to enter a favourite color, food, animal and the first name of a
friend. Use a Scanner object
and read one thing per line. The program
should than print the following: For example if the user entered blue for
color, burger for food, cat for the animal and Jack for the friend the output
will be:
I had a dream that Jack ate a blue cat and said it tasted like burger !
import java.util.Scanner;
public class FunnyString {
public static void main (String[] args) {
Scanner scan = new Scanner(System.in);
String color, food, animal, friend;
System.out.println ("Please enter a color");
color = scan.nextLine();
System.out.println ("Please enter a food");
food = scan.nextLine();
System.out.println ("Please enter a animal");
animal = scan.nextLine();
System.out.println ("Please enter a friend");
friend = scan.nextLine();
//System.out.println ("I had a dream that " + friend + " ate a " + color + " " + animal + " and said it tasted like " + food + "!");
String output1 = "I had a dream that ".concat(friend);
String output2 = output1.concat(" ate a ");
String output3 = output2.concat(color);
String output4 = output3.concat(" ");
String output5 = output4.concat(animal);
String output6 = output5.concat(" and said it tasted like ");
String output7 = output6.concat(food);
String output8 = output7.concat("!");
System.out.println(output8);
}
}
3. Write a program that read a string for a date in the format day/month/year and displays it as in the format day.month.year. For example if the input is 10/04/2022 the output should be 10.04.2022 .
import java.util.Scanner;
public class DateChanger {
public static void main (String[] args) {
Scanner scan = new Scanner(System.in);
String date,change1;
System.out.println ("Please enter a date");
date = scan.nextLine();
change1 = date.replaceAll("/",".");
System.out.println ("The changed date is " + change1);
}
}
4.
Write a program that reads a
four-digit number (such 2022) as a String and then display it one digit per line like so:
2
0
2
2
(Use charAt() method
from String, Think
about the right type for the variables you use).
Next, print the sum of the
digits.
Hint: Use Integer.parseInt() to
convert digits to integers.
import java.util.Scanner;
public class NumChanger {
public static void main (String[] args) {
Scanner scan = new Scanner(System.in);
String num;
int sum =0;
System.out.println ("Please enter 4 numbers");
num = scan.nextLine();
int num1 = Integer.parseInt(num);
for (int i=0; i<num.length(); i++){
System.out.println (num.charAt(i));
sum += num1 % 10;
num1 = num1 / 10;
}
System.out.println ("The sum of the digits is " + sum);
}
}
Comments
Post a Comment