Download Tutorial#1

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

Elementary arithmetic wikipedia , lookup

Arithmetic wikipedia , lookup

Mathematics of radio engineering wikipedia , lookup

Addition wikipedia , lookup

Transcript
King Saud University
College of Computer and Information Sciences
Information Technology Department
First semester 1430 /31
CSC 113: programming with Java
sheet# 1
chapter 8 -- I
lab:
1. Write a program composed of two classes: class Circle and a client class.
Circle contains appropriate components to store the radius of a circle and set
and get methods; in addition to calculating area and circumference.
Your client class should create an array of 10 circles of radii 1.0 , 2.0, …,
10.0. print the area and circumference of each circle.
2. Create a class Reals for performing integer arithmetic on real numbers.
Each real number has a whole part and a decimal part; your class should
contain data components representing both parts as integers and should
contain methods to do the following:
a. Default constructor
b. set the two components
c. get the components
d. print a real number
e. add two real numbers.
Add a client class to test your work.
3. Create a class called Complex for performing arithmetic with complex
numbers. Complex numbers have the form:
Real part + imaginary part * i
Your class should contain two int instance vars; r for the real part and m for
the imaginary part. The methods should be :
a. a default constructor
b. a constructor with parameters
c. a method that sets the values of the instance vars
d. 2 methods that return the two instance vars
e. A method that adds 2 complex numbers
Write a driver program that creates 2 complex numbers and then tests the above
methods.
Tutorial:
1. Create a client program for the class clock to create an array of Clock
objects for the times 5:32:00 8:22:8 23:7:34 4:49:49 12:2:12 . Print the
array elements.
2. Find the errors in the following program:
public class Err{
private int x;
char y;
static String s;
public void Err(int a, char b){
x = a; y = b; }
public int getx() { return x; } }
public class Client{
public static void main(String[] args) {
Err p , d;
p.y = ‘r’;
d = new Err();
p = new Err( 3 , ‘s’ );
p.x = 9;
System.out.println(getx() ) ; } }
3. Trace the following program :
public static void main(String[] args)
{
Roman roman = new Roman();
String s = “MXVI”;
roman.setRoman(s);
System.out.println("The equivalent of the Roman numeral "
+ s + " is ");
roman.printDecimal();
System.out.println(); }}
public class Roman{
private String romanNum;
private int decimalNum;
public Roman()
{
romanNum = "I";
decimalNum = 1; }
public Roman(String rString)
{
romanNum = rString;
romanToDecimal(); }
public void printDecimal()
{
System.out.println(decimalNum);
}
public void printRoman()
{
System.out.println(romanNum); }
public void setRoman(String rString)
{
romanNum = rString;
romanToDecimal(); }
public void romanToDecimal()
{ int sum = 0;
int len = romanNum.length();
int i;
int previous = 1000;
for (i = 0; i < len; i++)
{
switch (romanNum.charAt(i))
{
case 'M': sum = sum + 1000;
if (previous < 1000)
sum = sum - 2 * previous;
previous = 1000;
break;
case 'D': sum = sum + 500;
if (previous < 500)
sum = sum - 2 * previous;
previous = 500;
break;
case 'C': sum = sum + 100;
if (previous < 100)
sum = sum - 2 * previous;
previous = 100;
break;
case 'L': sum = sum + 50;
if (previous < 50)
sum = sum - 2 * previous;
previous = 50;
break;
case 'X': sum = sum + 10;
if (previous < 10)
sum = sum - 2 * previous;
previous = 10;
break;
case 'V': sum = sum + 5;
if (previous < 5)
sum = sum - 2 * previous;
previous = 5;
break;
case 'I': sum = sum + 1;
previous = 1;
}
}
decimalNum = sum;
}
}
2. Write another client program for the class Roman. Your program should
create two two Roman objects, one of your choice and the default. Your
program should calculate and print their sum.