Revision for midterm2

1 / 9

# Revision for midterm2 - PowerPoint PPT Presentation

Revision for midterm2. csc111.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'Revision for midterm2' - teenie

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### Revision for midterm2

csc111

Write a class called PrimeNum which has a private attribute called n. The PrimeNumclass has two constructors :the default sets the private attribute to 2 and another take integer parameter, and set() and get() methods for the private attribute. In addition, the PrimeNum class has a method isPrime() that determines whether the number n is a prime number or not.

• Write a another class called PrimeNumTest with a main() method that uses the class PrimeNum to determine and display all the prime numbers less than a specific integer number entered by a user. UML classes are represented by the diagram shown below.

Hint: A prime number is a natural number that has exactly two natural number divisors which are 1 and itself.

Private int n;

Public void setNum(int n1){n=n1;}

Public intgetNum(){return n;}

Public booleanisPrime(){

for (inti=2; i<n; i++)

if (n%i==0) return false;

return true; }

}

Public static void main(String [] args){

int num;

s.o.p(“enter a positive number: ”);

num= console.nextInt();

For(inti=2;i<=num;i++){

p.setNum(i);

if (p.isPrime())

s.o.print(i + “ “); //or s.o.print(p.getNum()+ “ ”);

}}

Write a class Point with two private attributes x and y, which represent the x and y coordinates in the x-y plane. The class should have:

• A default constructor that initializes the point x, y to the origin (0,0).
• A constructor that initializes x and y to given values.
• get() methods for x and y.
• A method called isEqual() that returns true if the current point object is equal to another point object sent as a parameter.

Write another class called TestPoint with a main() method that will create two point objects. Set the first point to the origin and the second point to coordinates of your choice. The program should then display the coordinates of both points. Finally, the program should display a message indicating whether the two points are equal or not.

Class Point{

Private int x;

Private int y;

Public Point(){x=0;y=0}

Public point(int x1,int y1){x=x1;y=y1;}

Public intgetx(){return x;}

Public intgety(){return y;}

Public booleanisEqual(Point p1){

If (x== p1.x && y==p1.y)

Return true;

Else return false;

}

}

Class TestPoint{

Public static void main(String [] args){

Point p1,p2;

P1= new point();

P2= new point(2,4);

s.o.p(“the coordinates of p1: x=“+p1.getx()+ ” y=”+ p1.gety());

s.o.p(“the coordinates of p2: x=“+p2.getx()+ ” y=”+ p2.gety());

If (p1.isEqual(p2))

s.o.p(“these two points are equal”);

Else

s.o.p(“these two points are not equal”);

}}

gcd

For example,

• GCD(15, 5) = GCD(5, 0) = 5
• GCD(99,88) = GCD(88,11) = GCD(11,0) = 11GCD(3456,1233) = GCD(1233,990) = GCD(990,243) = GCD(243,18) = GCD(18,9) = GCD(9,0) = 9

Sol1:

Public static int GCD(int a, int b){ // assume that a ≥ b

Int temp;

while (b != 0) { // Change the value of a and b: a ← b, b ← a mod b, and repeat until b is 0

temp = b ;

b = a % b;

a = temp;}

Return a; }

Sol2:

Inti,gcd=1

For( i=1; i<=n1 && <=n2; i++)

If (n1%i ==0 && n2%i==0)

{s.o.print( i + “ ”);

If (i>gcd)

gcd=i;}