Sunteți pe pagina 1din 3

German University in Cairo Faculty of Media Engineering and Technology

Introduction to Computer Science, Lab Exercise 1

Spring Term 2011

This lab assignment is intended to practice with you data types and variables in Java and also sequential algorithms in Java. In every programming language, you will learn about data types like Integer, Float, etc. They are necessary for variable declaration. Data Types in Java are primitive types that are built into the Java language itself. Data types in Java can be classied as Byte, Short, Long, Integer, Float, Double, Character, Boolean. Let us discuss some of them in short. Integers are used for storing integer values. Float is mainly used to store decimal values. Character is used for storing characters. Boolean stores True and False values. String represents a combination of characters like "Hello World". You cannot declare the variables just by giving the names and values. It should be declared according to the rules and conventions. The next session explains how to declare and use the variables in Java.

Variables

are nothing but locations in memory that can hold values. First a data type as described above should have to be selected, and a variable should be declared before assigning values. Generally we use the following Syntax while declaring the Variables.
<Data Type><Variable name> = <Value of the variable>
Exercise 1-1

Write, compile and run the following programs in Java. a)


Program 1:

public class AddExample { public static void main(String args[]) { System.out.println("Hello World!"); } }

b)

Program 2:

public class AddExample { public static void main(String args[]) { int a = 10; int b = 5; int c; c = a + b; System.out.println("Result is : " + c);

c)

Program 3:

public class IntExample { public static void main(String args[]) { int a,b,c; a = 10; b = 5; c = a + b; } System.out.println("Result is : " + c);

d)

Program 4:

public class Area { public static void main(string args[]) { double pi, r, a; r = 10.8; //radius of circle pi = 3.1416; //pi approxmately a = pi * r * r; //compute area } System.out.println("Area of circle is : " + a);

Exercise 1-2

Write a Java program that given the gross salary and the taxes to be paid calculates the net salary. You can assume values for the gross salary and the tax.
Exercise 1-3

Write a Java program that given the total number of eggs will calculate the corresponding number of dozens and the remaining eggs. You can also assume the total number of eggs.
Exercise 1-4

Write a java program to determine the ying time between two cities given the mileage between them and the average speed of the airplane.
Exercise 1-5

Write a java program that calculates and prints the annual cost of running an appliance given the cost per kilowatt-hour in cents and the number of kilowatt-hours the appliance uses in a year.
Exercise 1-6

Write a java program that given the width and length of a garden in meters calculates the area of the garden together with the number of trees that could be planted on this garden area, knowing that each tree needs a space of 50cm2 .
Exercise 1-7

The Pythagorean Theorem states that the sum of the squares of the two sides of a right angle triangle is equal to the square of its hypotenuse. For example, 3, 4 and 5 are the sides of a right angle triangle as they form a Pythagorean Triple (52 = 42 +32 ). Given 2 numbers, m and n where m n, a Pythagorean Triple can be generated by the following formulae:
a = m2 n 2 b = 2mn c = a2 + b2

Write a java program that reads in values for m and n and prints the values of the Pythagorean Triple generated by the formulae above.
Exercise 1-8

Write a java program that reads the amount of time in seconds and then displays the equivalent hours, minutes and remaining seconds.

S-ar putea să vă placă și