[go: up one dir, main page]

0% found this document useful (0 votes)
44 views9 pages

A. List All The Primitive Data Types and Explain The Difference Between Them

The document contains solutions to questions asked in a Week 3 Lab assignment involving Java programming. It includes: 1) Summarizing programs that print outputs based on variable assignments and operations. 2) Identifying and correcting errors in code snippets. 3) Writing Java programs that take command line arguments to perform operations like doubling, summing, multiplying, dividing, and averaging numbers. 4) A program to calculate wind chill given temperature and wind speed inputs. 5) Programs to check if a number is even or if one number is a multiple of another.

Uploaded by

VIKRAM KUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views9 pages

A. List All The Primitive Data Types and Explain The Difference Between Them

The document contains solutions to questions asked in a Week 3 Lab assignment involving Java programming. It includes: 1) Summarizing programs that print outputs based on variable assignments and operations. 2) Identifying and correcting errors in code snippets. 3) Writing Java programs that take command line arguments to perform operations like doubling, summing, multiplying, dividing, and averaging numbers. 4) A program to calculate wind chill given temperature and wind speed inputs. 5) Programs to check if a number is even or if one number is a multiple of another.

Uploaded by

VIKRAM KUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

Week 3 Lab 2012-10-15

Question 1
a. List all the primitive Data types and explain the difference between them.

Solution: see lecture slides for

b. What is the output of the following program?

public class First{


public static void main (String args[]){
int x, y, z;
x=1; y=2; z=3;
z=x; y=z;
System.out.println(y);
}
}

The program will print 1. The value of the variable z becomes 1 after(z=x;) the
the value of the variable y become 1 after (y=z;)

c. Change the program First.java such the variable y will hold the sum of x, y, z. Save
this program as sum1.java. The program should print the value of y.

Solution

public class Sum1{


public static void main (String args[]){
int x, y, z;
x=1; y=2; z=3;
int sum = x+y+z;
System.out.println(sum);
}
}
d. Change the following program by giving initial values to the variable a, b, c and d.
Compile and run and find the output of the program.

e.

public class precedence{

public static void main (String args[]){

double a= 10.2, b=7.0, c=2.0, d = 3.0;

double myValue = a + b % d – c * d / b;

System.out.println(myValue);

Solutions

public class precedence{


public static void main (String args[]){
double a = 10.2, b=7.0, c=2.0, d = 3.0;
double myVal = a + b % d - c *d/b;
System.out.println(myVal);
System.out.println(a + (b % d) - c *(d/b));
System.out.println((a + b) % d - c *d/b);

}
}
The program outputs:
10.34..
10.34..
1.34..
Question 2
a. Find all the errors in the following program:

Public class IntString{


Public station void main (String args[]){
int x = 2;
string y = “3”
int z= x+y
int t;
t = “4”;
system.out.println()
}
}

Once you think you've found the errors, compile/run a corrected version of this
program

Solution:

public class IntString{


public static void main (String args[]){
int x = 2;
int y = 3;
int z= x+y;
int t;
t = 4;
System.out.println()
}
}

b. What is the output of the following program?


public class hellohello
{
public static void main(String[] args)
{
String x = "hello ";
x=x+x;
System.out.println(x);
}
}

Solution:

The output is hellohello

Question 3
a. Write a program. Double.java, which takes one integer command-line argument
and prints out double the number.
Solution:
// Integer.parseInt(args[0]) will convert the first argument from a string to integer
//Integer.parseInt(args[1]) * 2 will double it.
// SumInt will print the double

public class Double


{
public static void main(String args[])
{

System.out.println(Integer.parseInt(args[0]) * 2);

}
}
b. Write a program, Sum.java which takes two integers command-line arguments. The
computer adds them up and prints out the answer

Solution.

// Integer.parseInt(args[0]) will convert the first argument from a string to integer


//Integer.parseInt(args[1]) will convert the second argument from a string to
integer
// sum2 will print the sum of the two integers.

public class sum2


{
public static void main(String args[])
{

System.out.println(Integer.parseInt(args[0]) +
Integer.parseInt(args[1]));

}
}
c. Write a program, Multiply.java which takes two integers command-line arguments.
The computer multiplies them together and prints out the answer.
Solution:

// Integer.parseInt(args[0]) will convert the first argument from a string to integer


//Integer.parseInt(args[1]) will convert the second argument from a string to
integer
// Multiply will print print the multiplication of both arguments.

public class Multiply


{
public static void main(String args[])
{

System.out.println(Integer.parseInt(args[0]) *
Integer.parseInt(args[1]));

}
}
d. Write a program, Divide.java; which takes two integers command-line arguments.
The computer divides the first argument by the second one and prints out the
answer.

Solution:
// Integer.parseInt(args[0]) = a will convert the first argument from a string to
integer
//Integer.parseInt(args[1])=b will convert the second argument from a string to
integer
// SumInt will print the a/b

public class Divide


{
public static void main(String args[])
{

System.out.println(Integer.parseInt(args[0]) /
(Integer.parseInt(args[1])*1.0));

}
}

e. Write a program, Average.java, which takes three integers command-line


arguments and prints out their arguments.
Solution

// Integer.parseInt(args[0]) will convert the first argument from a string to integer


//Integer.parseInt(args[1]) will convert the second argument from a string to
integer
// SumInt will print the sum of the two integers.

public class Average


{
public static void main(String args[])
{
Double average = (Integer.parseInt(args[0]) + Integer.parseInt(args[1])+
Integer.parseInt(args[2]))/3.0;
System.out.println(average);

}
}

Question 4:

Write a program WindChill.java that takes two double command-line arguments t


(temperature in Fahrenheit) and v in miles per hour)and prints out the wind
chill (W). Assume the wind chill formula is
w = 35.74 + 0.6215*t + (0.4275*t - 35.75) * v ^ 0.16

Use Math.pow(a,b) to compute ab.


Solution

public class WindChill {


public static void main(String[] args) {
double t = Double.parseDouble(args[0]);
double v = Double.parseDouble(args[1]);
double w = 35.74 + 0.6215*t + (0.4275*t - 35.75) *
Math.pow(v, 0.16);
System.out.println("Temperature = " + t);
System.out.println("Wind speed = " + v);
System.out.println("Wind chill = " + w);
}

Question 5
a. Write a program, Even.java, which prints out ‘yes’ if the number is even and
‘no’ otherwise.
Solution:

// Integer.parseInt(args[0]) will convert the first argument from a string to integer


//Integer.parseInt(args[1]) % 2 will check if the integer is a multiple of 2.
// Even will print the yes if the argument is even and no otherwise.
public class Even
{
public static void main(String args[])
{
if (Integer.parseInt(args[0]) % 2 == 0)

{
System.out.println("Yes");
}
else
{
System.out.println("NO");
}
}
}
b. Write a program. Multiple.java, which takes two integer arguments and prints
out ‘yes’ if one is a multiple of the other

Solution
// Integer.parseInt(args[0]) will convert the first argument from a string to
integer
//Integer.parseInt(args[1]) % 2 will check if the integer is a multiple of 2.
// Even will print the yes if the argument is even and no otherwise.

public class Multiple


{
public static void main(String args[])
{
int first = Integer.parseInt(args[0]);
int second = Integer.parseInt(args[0]);

if (first % second == 0)

{
System.out.println("Yes");
}
else
{
if (second % first == 0)
{
System.out.println("Yes");
}

else
{
System.out.println("NO");
}
}
}
}

This can be done in a shorter way as follow:

public class Multiple


{
public static void main(String args[])
{
int first = Integer.parseInt(args[0]);
int second = Integer.parseInt(args[0]);

if ((first % second == 0) || (second % first == 0))


{
System.out.println("Yes");
}
else
{
System.out.println("NO");
}
}
}
}

You might also like