Homework Clinic

Science Clinic => Computer Science => Programming and Graphic Design => Topic started by: WhattoUnderstand on Aug 30, 2020

Title: Using the various String methods, manipulate a String called current to be current's last character ...
Post by: WhattoUnderstand on Aug 30, 2020

Question 1

Provide three examples of code using assignment statements where one assignment statement would result in a syntax error, one would result in a logical error, and one would result in a run-time error.

Question 2

Using the various String methods, manipulate a String called current to be current's last character followed by the remainder of its characters in order, placing the result in a String called rearranged.
Title: Using the various String methods, manipulate a String called current to be current's last character ...
Post by: cpetit11 on Aug 30, 2020

Answer 1

int x = 5.0 / 2.0; //  syntax error caused by right side providing a double and the left side wants an int
double x = (intValue1 + intValue2)  / 2;  // logical error caused by not casting the division as a double
int x = 5 / (y - y); // run-time error caused by division by 0 when y - y is evaluated

Answer 2

String rearranged = current.charAt(current.length( ) - 1) + current.substring(0, current.length( ) - 1);

The last character, which is found at charAt(current.length( ) - 1) is the first item in rearranged, followed by the substring of current starting at 0 (the first character) and going to current.length( ) - 2 (the second to last character).
Title: Re: Using the various String methods, manipulate a String called current to be current's last charac
Post by: Tanmay Joshi on Oct 6, 2020
thank you