Professional Documents
Culture Documents
CSEN202 Practice 1 Solution 31917 PDF
CSEN202 Practice 1 Solution 31917 PDF
a) −(−1)
Solution:
Syntactically correct, value 1.
b) −−1
Solution:
Syntactically incorrect. “−−” is an operator in Java that only works on variables. Correct uses of this opera-
tor (and the related “++” operator) are: “++i”, “−−i”, “i++”, and “i−−”.
c) 2∗4(−6)
Solution:
Syntactically incorrect.
There is an operator missing between 4 and (-6).
d) (12)∗(4)−6
Solution:
Syntactically correct, value 42.
e) 29%5%3∗4−2
Solution:
Syntactically correct, value 2.
f) 29%5%3−4∗2
Solution:
Syntactically correct, value -7.
g) 7%3 = 1
Solution:
Syntactically Incorrect.
The assignment operator = requires a variable on the left side. Danger of confusion with the equality
operator ==.
h) 1 / 0
Solution:
Syntactically correct, run-time error.
1
i) 1 / 0.0
Solution:
Syntactically correct, value infinity.
j) ((10 − 2) ∗ 4%)8)
Solution:
Syntactically incorrect , ’ ’ ) ’ ’ expected .
k) char c = ’a ’ ;
int x = c;
System . o u t . p r i n t l n (x );
x ++;
System . o u t . p r i n t l n (x );
char v = ( char ) x ;
System . o u t . p r i n t l n (c );
System . o u t . p r i n t l n (v );
c ++;
System . o u t . p r i n t l n (c );
Solution:
Syntactically correct.
l) f l o a t x = 2 . 5 ;
float y = 3.4;
float z = x + y;
System . o u t . p r i n t ( " The v a l u e i s " + z ) ;
Solution:
Syntactically incorrect. By default in Java, a floating point number is represented as double with 64-bits.
Thus, we can not put a double in float without writing the ’f’ suffix.
m) i n t x = 3 4 5 6 7 4 5 6 ;
long y = 1234567;
long z = x + y ;
System . o u t . p r i n t l n ( " The v a l u e i s " + z ) ;
y = 46547864784282;
z = x + y;
System . o u t . p r i n t ( " The v a l u e i s " + z ) ;
Solution:
Syntactically incorrect. By default in Java, a literal for integers is an int with 32-bits. If we want to store a
number like 46547864784282 that exceeds the range of the int, we need to write the ’l’ suffix..
n) b y t e x = 1 2 ;
byte y = 8;
byte z = x + y ;
System . o u t . p r i n t ( " The v a l u e i s " + z ) ;
Solution:
Syntactically incorrect. On the hardware level the ’+’ operation is done on int values. Thus, we need to
typecast it into byte as well.
2
o) S t r i n g x = "CSEN " ;
i n t y = 202;
String z = x + y + "!";
System . o u t . p r i n t l n ( z );
String u = y + x + "!";
System . o u t . p r i n t l n ( u );
String v = y;
System . o u t . p r i n t l n ( v );
String m = "204";
i n t o = m;
System . o u t . p r i n t l n ( o );
Solution:
Syntactically incorrect. int can not be converted into String , we need to concatenate it to a String . Also,
String can not be converted into int directly, we need to parse it.
For each of the .java classes in the “ Lab 1 Exercises.zip ” zipped folder, compile the files on JCreator. You are
asked to define in each file the lines which have the following faults:
a) Syntax Errors
b) Runtime Errors
c) Logic Errors
Furthermore, specifically define the errors. For example, the following line of code:
has two Syntax Errors and 1 bad identifier naming. The correct solution is:
d o u b l e d1 = 1 . 0 3 ;
d o u b l e d2 = 0 . 4 2 ;
d o u b l e d3 = d1 − d2 ;
System . o u t . p r i n t l n ( d3 ) ;
d o u b l e d4 = 0 . 1 ;
float f = ( f l o a t ) d4 ;
d4 = 1 − f;
System . o u t . p r i n t l n ( d4 ) ;
3
Solution:
d3 will equal 0.6100000000000001, and d4 will equal 0.8999999761581421.
Solution:
import j a v a . u t i l . S c a n n e r ;
p u b l i c c l a s s Time
{
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
S c a n n e r s c = new S c a n n e r ( System . i n ) ;
i n t seconds , minutes , hours ;
System . o u t . p r i n t l n ( " E n t e r number o f s e c o n d s " ) ;
seconds = sc . n e x t I n t ( ) ;
hours = seconds / 3600;
seconds = seconds % 3600;
For example if we have: 3 quarters, 2 dimes, 1 nickle and 6 pennies, then the total is 1.06 dollars.
Solution:
Initializing variables by value:
4
p u b l i c c l a s s CountChange
{
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
i n t q u a r t e r s , dimes , n i c k l e s , p e n n i e s ;
quarters = 3;
dimes = 2;
nickles = 1;
pennies = 6;
d o u b l e t o t a l = q u a r t e r s ∗ 25 + d i m e s ∗ 10 + n i c k l e s ∗ 5 + p e n n i e s ;
System . o u t . p r i n t l n ( " T o t a l i s : $ " + t o t a l / 1 0 0 ) ;
}
}
Solution:
p u b l i c c l a s s CookSomePasta {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
f i n a l double PI = 3 . 1 4 1 5 ;
double d i a m e t e r = 7 . 0 ;
double h e i g h t = 6.0;
double r a d i u s = diameter / 2;
/ / c a l c u l a t e t h e volume
d o u b l e volume = PI ∗ r a d i u s ∗ r a d i u s ∗ h e i g h t ;
5
/ / Assume t h a t d i a m e t e r & h e i g h t a r e g i v e n i n c e n t i m e t e r s
double l i t e r s = volume / 1 0 0 0 ;
/ / The s t o v e n e e d s two m i n u t e s p e r l i t e r
double time = l i t e r s ∗ 2;
import j a v a . u t i l . ∗ ;
p u b l i c c l a s s CookSomePasta {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
f i n a l double PI = 3 . 1 4 1 5 ;
Scanner sc = new S c a n n e r ( System . i n ) ;
double r a d i u s = diameter / 2;
/ / c a l c u l a t e t h e volume
d o u b l e volume = PI ∗ r a d i u s ∗ r a d i u s ∗ h e i g h t ;
/ / Assume t h a t d i a m e t e r & h e i g h t a r e g i v e n i n c e n t i m e t e r s
double l i t e r s = volume / 1 0 0 0 ;
/ / The s t o v e n e e d s two m i n u t e s p e r l i t e r
double time = l i t e r s ∗ 2;
Solution:
public class Resistance
{
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) throws I O E x c e p t i o n
{
f l o a t r1 = 8 , r2 = 8 , r3 = 4;
f l o a t resS = r1 + r2 + r3 ;
f l o a t resPInv = 1/ r1 + 1/ r2 + 1/ r3 ;
f l o a t resP = 1 / resPInv ;
6
System . o u t . p r i n t l n ( " S e r i e s r e s i s t a n c e : " + r e s S ) ;
System . o u t . p r i n t l n ( " P a r a l l e l r e s i s t a n c e : " + r e s P ) ;
}
}
9
Tf = × Tc + 32, and
5
Tk = Tc + 273.
Where T f is temperature in degrees Fahrenheit, Tc is temperature in degrees Celsius, and Tk is temperature in de-
grees Kelvin.
Write a Java program that takes as an input a degree in Celsius scale and converts it into both Fahrenheit and Kelvin
scales.
Solution:
import j a v a . u t i l . ∗ ;
public class TemperatureCalculator {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
S c a n n e r s c = new S c a n n e r ( System . i n ) ;
double c e l s i u s , f a h r e n h e i t , k e l v i n ;
Solution:
import j a v a . u t i l . ∗ ;
public class Octal
{
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
i n t oct0 , oct1 ;
i n t q0 , q1 , q2 , r0 , r1 , r 2 ;
S c a n n e r s c = new S c a n n e r ( System . i n ) ;
System . o u t . p r i n t ( " P l e a s e e n t e r t h e o c t a l number : " ) ;
int o c t a l = sc . n e x t I n t ( ) ;
7
/ / extracting the d i g i t s
o c t 0 = o c t a l %10;
oct1 = o c t a l /10;
/ / C a l c u l a t e th e decimal Value
double dec = o c t 0 ∗ 1 + o c t 1 ∗ 8 ;
i n t r e s u l t = ( i n t ) dec ;
System . o u t . p r i n t l n ( o c t a l + " i n o c t a l = " + r e s u l t + " i n d e c i m a l . " ) ;
/ / c o n v e r t i n g the f i r s t d i g i t base 8 i n t o 3 b i t s
q0 = o c t 1 / 2 ;
r 0 = o c t 1 %2;
q1 = q0 / 2 ;
r 1 = q0 %2;
q2 = q1 / 2 ;
r 2 = q1 %2;
System . o u t . p r i n t ( o c t a l + " i n o c t a l = " + r 2 + " " + r 1 + " " + r 0 + " " ) ;