Professional Documents
Culture Documents
2023
By Rutendo Mtisi
Homework Instructions
This is an individual homework however if you find it difficult you are allowed to collaborate with one
other person (i.e groups of two)
If you have a partner you are only allowed to discuss and don’t copy each other
This homework is to reinforce your understanding of the java basic concepts thus you would benefit
more from doing it alone
Try doing it yourself alone before you google it or look for a partner
You can also use the IDE to simulate some of problem but again it’s much better to try doing it yourself
Q4 and Q5 are research problems we haven’t covered in lecture but part of being a programmer means
researching how to do things you don’t know how to do. (Should take no more than 30mins for both
questions)
The HW is due Friday when the lecture starts. All hw submissions should be put into the HW0
submission folder in the drive
Variables & Types
Fill in the data type and final value of the variable a. Write “CE” as the data type if the statements will
cause a compiler error or “RE” if they will cause a run-time error. If there is an error, give the reason for
it in the third column. Tip: These questions are meant to be tricky, so take your time!
Data type of a
int x = 4;
____ a = x / 2.0;
double
2.0
String s = “Hello”;
______ a = s - ‘o’;
CE
String s = “CI”;
______ a = s + ‘S’;
String
CIS
double d = 3 / 2;
______ a = d + 1;
Double
2.0
char c = ‘a’;
____ a = c + 1;
Integer
98
______ a = Integer.parseInt(“4.0”);
double d = 1.5;
Double
6.5
Integer
2
int a = 5;
System.out.println(5 + a);
Integer
10
int x = 7;
____ a = x + 2 + “Hello”;
String
9Hello
int x = 7;
____ a = “Hello” + x + 2;
String
Hello72
_____ a = 5 / 2 + 1.0;
Double
3.0
_____ a = 1.0 + 5 / 2;
Double
3.0
_____ a = 1 + 5 / 2.0;
Double
3.0
_____ a = (5.0 + 1) / 2 + 5 / 2
Double
3.5
What does the following program print out? Answer “CE” or “RE” if compiler or runtime error.
int i = 4;
int j = 21;
int k = i * 7 + 2 - j;
k + 15;
char i = 65.0;
int j = 7;
int k = i / j;
System.out.println("k = " + k );
CE =a number has been placed inside a container meant for a letter/single character
What is the value and type of the variable k by the end of the main function? Answer “CE” or “RE” if
compiler or runtime error.
double i = 4;
int j = i;
double k = i + j;
CE :the double value for it cannot be put into a container meant for whole numbers unless there is some
casting to change the double into an integer
What is the value and type of the variable k by the end of the main function? Answer “CE” or “RE” if
compiler or runtime error.
int i = 4;
double j = i;
double k = i + j;
System.out.println(k);
Double:8.0
Strings
What are the following types and values of x? Answer “CE” or “RE” if compiler or runtime error.
___ x = "asdf".charAt(1);
Chart
character=s
___ x = "asdf".charAt(4);
CE-all four values have been erased leaving the container empty
___ x = "asdf".length();
Integer=4
. D.___ x = "asdf".length;
. CE-
Bolean=false
F. ___ x = "asdf".substring(2);
String =as
String =as
H.___ x = "asdf".indexOf("asD");
Integer -0
I.___ x = “asdf”.compareTo(“sadf”);
Integer -18
String -asdfsadf
Exercise 5 - String Manipulation Practice (collab group of 3 - write out in english what the code should
do and the code it)
SRS TA’s were planning on spring breaking in a borough in Butler County, Pennsylvania called Mars.
However, Bhrajit put the location wrong in the GPS, and now the SRS TA’s are stranded on the planet
Mars!
They meet aliens who speak their English, except they replace the first letter of every word to “Z" and
replace the last letter with an “a”. The SRS TA’s quickly realize they can break this language barrier
through programming!
Given a String, help the SRS TA’s out by replacing the first letter with “Z” and the last letter with “a”.
// replace the first letter with "Z" and last letter with "a"
}
public class Main {
input.substring(0,1);
convertedword = input.substring(1,input.length()-1);
System.out.println("z"+convertedword+"a");