You are on page 1of 10

CODEBREAKERS

2023

By Rutendo Mtisi

Due Date: May 5 @3pm

Topic(s): Variables & Types and Strings

Homework 0 - Introductions & Conceptual Review

Types: ints, doubles, booleans, Strings, chars

What is casting, parsing?

How to convert double to int, int to double, int to String, etc?

+, -, /, *, % for ints & doubles

String concatenation (+)

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

Exercise 1 - Variable 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

Value of a / Error Explanation

int x = 4;

____ a = x / 2.0;

double

2.0

String s = “Hello”;

______ a = s - ‘o’;

CE

You cannot subtract an integer from a letter (string).Strings are immutable

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;

____ a = (int) 5.2 + d;

Double

6.5

____ a = ‘5’ - ‘3’;

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

Exercise 2 - More Operators

What does the following program print out? Answer “CE” or “RE” if compiler or runtime error.

public static void main(String[] args) {

int i = 4;

int j = 21;

int k = i * 7 + 2 - j;

k + 15;

System.out.println("k = " + k);

CE ='k+15' is a variable which has not been declared


What does the following program print out? Answer “CE” or “RE” if compiler or runtime error.

public static void main(String[] args) {

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

Exercise 3 - Lossy Conversions

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.

public static void main(String[] args) {

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.

public static void main(String[] args){

int i = 4;

double j = i;

double k = i + j;

System.out.println(k);

Double:8.0

Strings

Exercise 4 - String Methods

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();

.length() counts the number of charactercharacteristics

Integer=4

. D.___ x = "asdf".length;

. CE-

E. ___ x = "asdf1" == "asdf";

Bolean=false

F. ___ x = "asdf".substring(2);

String =as

G____ x = "asdf".substring(0, 2);

String =as

H.___ x = "asdf".indexOf("asD");

Integer -0
I.___ x = “asdf”.compareTo(“sadf”);

Integer -18

J ___ x = “asdf” + “sadf”;

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”.

Public static void main (String[] args) {

String input = "_some_arbitrary_word_";

// replace the first letter with "Z" and last letter with "a"

}
public class Main {

public static void main(String[] args) {

String input = "some arbitrary word";

String convertedword =input.substring(input.length()-1);

input.substring(0,1);

convertedword = input.substring(1,input.length()-1);

System.out.println("z"+convertedword+"a");

You might also like