Download Lab solution week 4

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Lab week 4: Classes/objects , UML representations , Object creation, Access to attributes, Static
Attributes
Exercise 1:
Exercise 1: Write a program that reads the unknown variables found in the following expression to
compute the value of Z.
Z=2*x^(h/y).
Hint: a^b in java =Math(a,b)
Solution:
import java.util.*;
public class ex1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
System.out.println("enter the value of x, h, y: ");
int x=s.nextInt(); int h= s.nextInt(); int y= s.nextInt();
Double Z=2*Math.pow(x,(h/y));
System.out.println("Z= " + Z);
}
}
Exercise 2:
Write a Java program to read in the weight (in pounds) of an object and then compute and print
the weight in kilograms and grams. You must create a class weight contains pounds, Kilos. And
grams and use it in class main as shown in the following UML representation.
(Hint: 1 pound is equal to 0.453592 kilogram and 453.59237 grams.)
Example:
Enter the weight in pounds: 8
Weight
+ pounds: double
+ Kilos: double
+ grams: double
The weight in Kilograms is: 3.628736
The weight in Grams is: 3628.73896
Solution:
import java.util.*;
public class ex2 {
double pounds, kilos, grams;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ex2 e= new ex2();
Scanner s=new Scanner(System.in);
System.out.println("Enter the weight in pounds: ");
e.pounds=s.nextDouble();
e.kilos=e.pounds/0.45359237;
e.grams=e.pounds/453.59237;
System.out.println("The weight in Kilograms is:
System.out.println("The weight in grams is:
}
"+e.kilos);
"+e.grams);
}
Exercise 3:
Write a Java program to read two data items and print their sum, difference, product, and
quotient.
Example:
Enter two numbers: 10 5
The sum = 15
The difference = 5
The product = 50
The quotient = 2
import java.util.*;
public class ex3 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
System.out.println("Enter 2 numbers");
int x=s.nextInt();int y=s.nextInt();
System.out.println("The sum =
"+(x+y));
System.out.println("The difference = "+(x-y));
System.out.println("The product =
System.out.println("The quotient =
"+(x*y));
"+(x/y));
}
}
Exercise 4:
Write a java program that reads an integer and print the least significant digit and the next least
significant digit.
Example:
Enter an integer number > 7235
The least significant digit is 5
The next least significant digit is 3
import java.util.*;
public class ex4 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
System.out.println("Enter an integer number ");
int x=s.nextInt();
System.out.println("The least significant digit is "+(x%10));
System.out.println("The next least significant digit is "+((x%100)/10));
}
}
Exercise 5:
Write a java program that reads an integer decimal number smaller than 32 and prints the
equivalent representation in binary system.
Hint: use four variables to store the value of the binary digits.
Example:
Enter an integer number smaller than 32 > 12
The equivalent in binary = 01100
import java.util.*;
public class ex5 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
System.out.println(" Enter an integer number smaller than 32 ");
int x=s.nextInt(); int a1,a2,a3,a4,a5;
a1=x%2;
x/=2;
a2=x%2;
x/=2;
a3=x%2;
x/=2;
a4=x%2;
x/=2;
a5=x%2;
System.out.println("The equivalent in binary = "+a5+a4+a3+a2+a1);
}
}
Related documents