## Q2.1

Write a program that:
(i): Determines, and prints to the screen, every three digit number that is a palindrome using a for loop. Note: A three digit number is a palindrome if it reads the same forward as in reverse. For example, 212 and 787 are palindromes while 124 and 995 are not palindromes。
(ii): Your program should count the number of occurrences of these palindromes and print the total number found to the screen.

``````package J2018;

import java.util.Scanner;

public class Q21 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int sum = 0;
while (input.hasNext()) {
String numString = input.next();
if(numString.equals("-1")) {
break;
}
if (numString.charAt(0) == numString.charAt(2)) {
System.out.println("it is palindrome numbers");
sum++;
} else {
System.out.println("it is not palindrome numbers");
}
}
System.out.println(sum);
}
}
``````

## Q2.2

Write a program that:
(i): Asks the user to enter the size of a one dimensional array of Strings they would like to create using user input – you can assume they enter a valid number. You should declare an array of that size.
(ii): Using user input read in the contents of the array which should be in the form of Strings.
(iii): Counts the number of occurrences of the letter ‘a’ in the array.
(iv): Replace every occurrence of the letter ‘t’ with a ‘*’ and store it back in the original Strings of the array.
(v): Print the contents of the array.

``````package J2018;

import java.util.*;

public class Q22 {
public static void main(String[] args) {
// 1
Scanner in = new Scanner(System.in);
int size = in.nextInt();
String[] input = new String[size];
// 2
for (int i = 0; i < size; i++) {
input[i] = in.next();
}
in.close();
// 3
int anum = 0;
for (int i = 0; i < size; i++) {
for (int j = 0; j < input[i].length(); j++) {
if (input[i].charAt(j) == 'a') {
anum++;
}
}
}
System.out.println(anum);
// 4
for (int i = 0; i < size; i++) {
String temp = input[i].replace('t', '*');
// 原字符串就是不能变，只是创建了相同的但替换过内容的新字符串
input[i] = temp;
// 创建新的指向对象
}
// 5
for (int i = 0; i < size; i++) {
System.out.print(input[i] + " ");
}
}
}
``````

## Q2.3

Write a program that will use a two dimensional array to store details of rental accommodation in Maynooth. The array should store data in the following format:
id, no_occupants, total_weekly_rent, vacant
The vacant field can have one of 2 values – 0 or 1 to represent that the house is unoccupied (0) or that the house is occupied (1).
For example, one row of the array might store the following data:
, , ,
Your program should (i): ask the user to enter the number of properties they would like stored in the array and create an appropriate array.
CS161 Page 5 of 5 January 2018
(ii): ask the user to input data to the array for each property – you can assume that the user enters the correct data types.
(iii): return the total number of occupants in all occupied properties along with the total rent per week paid by these occupants. The program should then print out the average rental.
(iv): Print out the contents of the array in the following format:
Property 0: 101, 5, 400, 1,
Property 1: 102, 4, 1000, 0,
Property 2: 103, 2, 600, 1, …
(v): The program should then increase the weekly rent for each property by €100 and recalculate the average weekly rental price of all occupied properties.
(vi): The details of the 5th property stored need to be amended to the following values:
, , ,

``````package J2018;

import java.util.*;

public class Q23 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int size = in.nextInt();
int[][] array = new int[size];
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
array[i][j] = in.nextInt();
}
}
// 3
int sumR = 0, sumQ = 0;
for (int i = 0; i < array.length; i++) {
if (array[i] == 1) {
sumR += array[i];
sumQ += array[i];
}
}
System.out.println(sumQ / sumR);
// 4
for (int i = 0; i < array.length; i++) {
System.out.print("Property " + i + ":");
for (int j = 0; j < array[i].length; j++) {
System.out.print(" " + array[i][j]);
}
System.out.println();
}
// 5
for (int i = 0; i < array.length; i++) {
array[i] += 100;
}
int sumN = 0, sumH = 0;
for (int i = 0; i < array.length; i++) {
if (array[i] == 1) {
sumH++;
sumN += array[i];
}
}
System.out.println(sumN / sumH);
// 6
array = 105;
array = 4;
array = 100;
array = 1;
}
}
``````