Download Week 7 - NUS Computing

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Questions for Discussion (Week 7)
Q1. Finding substrings
Write a program that reads a string from the user and stores it into a character array
(array of char type). Using at most one loop, devise an algorithm that counts the
number of substrings that begin with character ‘A’ and ends with character ‘X’.
For example:
Given the input string “CAXAAYXZA”, there are four substrings that begin with ‘A’
and ends with ‘X’, namely: “AX”, “AXAAYX”, “AAYX”, and “AYX”.
As this is an exercise on array, you are not allowed to use java.lang.String class.
(Why do we want you to use a character array? Firstly, strings are inefficient because
they are ‘immutable’, meaning that their contents cannot be changed (see page 171 of
textbook). When we do ‘change’ the content of a string, for example, s = s +
"abc", what really happens is a new string is created and assigned to s. Secondly,
this is to prepare you for the next lab assignment!)
Sample runs
Please enter a string: CAXAAYXZA
Number of substring of "A...X" is 4
Please enter a string: AAXOXXA
Number of substring of "A...X" is 6
Skeleton code
import java.util.Scanner;
public class Week7Q1
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
Exploration: Find out about the
toCharArray() method.
Where can you find it? Under
which class?
System.out.print("Please enter a string: ");
char[] input = stdIn.nextLine().toCharArray();
/***********************
Fill in your code here
************************/
System.out.println("Number of substring of \”A...X\” is "
+ numSubStrings);
}
}
Page 1 of 3
Q2. Bubble Sort
Find out about Bubble sort on the Internet. Complete the following program that sorts
a character array using Bubble sort.
Sample run
Please enter a string: anoeoienjcz
The sorted array is : aceeijnnooz
Skeleton code
import java.util.Scanner;
public class Week7Q2
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
System.out.print("Please enter a string: ");
char[] input = stdIn.nextLine().toCharArray();
/**********************
Fill in your code here
***********************/
System.out.print("The sorted array is : ");
for (int ctr = 0; ctr < input.length; ctr++)
{
System.out.print(input[ctr]);
}
System.out.println();
}
}
Exploration:
Can you replace the ‘for’ loop above with just this simple statement?
System.out.print(input);
Can you do the same for an array of another type, say, an integer array?
Page 2 of 3
Q3. Statistics
Given a list of n monthly prices for an item, compute the mean value and standard
deviation of the prices, which are double values. Mean is the average value and
standard deviation measures the dispersion of the values.
Let x1, x2, …, xn denote the prices in the list,  the mean, and  the standard deviation.
Mean  = (x1 + x2 + ... + xn) / n;
Standard deviation = √( ( (x1 – )2 + (x2 – )2 + .... + (xn – )2 ) / n )
Refer to the sample run below for the output format.
Sample run
Enter size of list: 10
Enter 10 prices: 1.2 3.4 12 3.2 4.3 7 8.3 1.9 2.4 3.3
Mean = 4.70
Standard deviation = 3.21
Q4. Reversing a sentence
Write a program to read a sentence, and to reverse the order of words in the sentence.
You should use an array of strings to hold the words in the sentence. You may assume
that there are at most 100 words in the sentence.
Sample runs
Enter a sentence:
Reverse this sentence
sentence this Reverse
Enter a sentence:
I Love CS1101 Discussion Session
Session Discussion CS1101 Love I
Page 3 of 3