You are on page 1of 11

დავალება #2 მარტივი Java პროგრამები

ამოცანა 1
დაწერეთ GraphicsProgram ქვეკლასი რომელიც დახატავს პირამიდას. პირამიდა შედგება
ჰორიზონტალურად განლაგებული აგურებისგან, ისე რომ ყოველ მომდევნო ხაზში (ქვემოდან
ზემოთ) აგურების რაოდენობა იკლებს ერთით.

პირამიდა უნდა დაიხატოს ფანჯრის ცენტრში, როგორც სურათზეა ნაჩვენები და უნდა იყოს
აგებული მოცემული პარამეტრების მიხედვით:
BRICK_WIDTH თითოეული აგურის სიგრძე (30 pixels)
BRICK_HEIGHT თითოეული აგურის სიმაღლე (12 pixels)
BRICKS_IN_BASE აგურების რაოდენობა პირველ (ქვედა) ხაზში (14)

ფრჩხილებში მოცემული პარამეტრები აღწერს სურათზე მოცემულ პირამიდას, მაგრამ


თქვენ თქვენს პროგრამაში უნდა შეგეძლოთ ამ მონაცემების შეცვლა.

public class BlankClass extends GraphicsProgram {


private static final int BRICK_WIDTH = 30;
private static final int BRICK_HEIGHT = 12;
private static final int BRICKS_IN_BASE = 14;

public void run() {


/*
* number of the bricks decreases from (in this case) 14 to 1. At the same time,
* number of Lines increases from (in this case) 1 to 14. For each stage, number
* of bricks and number of the line, summed together, is bricks-in-base + 1 (in
* this case 15).
*/
for (int i = BRICKS_IN_BASE; i > 0; i--) {
fillLine(i, BRICKS_IN_BASE+1 - i);
}
}

/*
* This method fills each floor of pyramid. "nBricks" is the number of bricks
* for each "Line", e.g. 14 is the number of bricks on the first "Line" of
* Pyramid. xCoord and yCoord give a location for each brick. The first brick's
* xCoord is found by subtracting number of bricks-in-base from width and
* dividing by two. Every following brick's X coordinate is +brick's width added
* to previous brick's xCoord. The first brick's Y coordinate is exactly one
* brick-height away from "getHeight". Every following brick's Y coordinate is
* defined by adding one more brick-height.
*/
private void fillLine(int nBricks, int nLine) {
for (int k = 0; k < nBricks; k++) {
int xCoord = (getWidth() - nBricks * BRICK_WIDTH) / 2 + k * BRICK_WIDTH;
int yCoord = getHeight() - nLine * BRICK_HEIGHT;
GRect brick = new GRect(BRICK_WIDTH, BRICK_HEIGHT);
add(brick, xCoord, yCoord);
}
}

ამოცანა 2
თქვენ დაგიქირავს იმისთვის, რომ დაწეროთ პროგრამა რომელიც დახატავს სამიზნეს.
მაგალითი ნაჩვენებია სურათზე:
ფიგურა შედგება სამი GOval ობიექტისგან, რომელთაგანაც ორი წითელია და ერთი თეთრი.
გარეთა წრის რადიუსია 2.54სმ (72 პიქსელი), თეთრი წრის რადიუსია 1.65 სმ და შიდა წრის
რადიუსია 0.76 სმ. ფიგურა უნდა იყოს ფანჯრის შუაში.
private static final double bigCircleRadiusCm = 2.54;
private static final int bigCircleRadiusPix = 72;
private static final double mediumCircleRadius = 1.65;
private static final double smallCircleRadius = 0.76;

public void run() {


drawBigCircle();
drawMediumCircle();
drawSmallCircle();
}
/*This method creates the big red circle.*/
private void drawBigCircle() {
GOval bigCircle = new GOval(bigCircleRadiusPix * 2, bigCircleRadiusPix * 2);
bigCircle.setFilled(true);
bigCircle.setColor(Color.RED);
bigCircle.setFillColor(Color.RED);
add(bigCircle, getWidth() / 2 - bigCircleRadiusPix, getHeight() / 2 - bigCircleRadiusPix);
}
/*This method creates the middle, white circle. "mediumCircleRadiusPix" is the number of that
circle's radius in pixels. In this case it is counted
* like this: (2.54/72)*1.65 In general, firstly it counts how much centimeter is one pixel and then it
multiplies it by medium circle's radius (cm) .*/
private void drawMediumCircle() {
double mediumCircleRadiusPix = (bigCircleRadiusPix / bigCircleRadiusCm) * mediumCircleRadius;
GOval mediumCircle = new GOval(mediumCircleRadiusPix * 2, mediumCircleRadiusPix * 2);
mediumCircle.setFilled(true);
mediumCircle.setColor(Color.WHITE);
mediumCircle.setFillColor(Color.WHITE);
add(mediumCircle, getWidth() / 2 - mediumCircleRadiusPix, getHeight() / 2 -
mediumCircleRadiusPix);
}
/*This method creates the small, red circle. "smallCircleRadiusPix" is the number of that circle's
radius in pixels. In this case it is counted
* like this: (2.54/72)*0.76 In general, firstly it counts how much centimeter is one pixel and then it
multiplies it by small circle's radius (cm)*/
private void drawSmallCircle() {
double smallCircleRadiusPix = (bigCircleRadiusPix / bigCircleRadiusCm) * smallCircleRadius;
GOval smallCircle = new GOval(smallCircleRadiusPix * 2, smallCircleRadiusPix * 2);
smallCircle.setFilled(true);
smallCircle.setColor(Color.RED);
smallCircle.setFillColor(Color.RED);
add(smallCircle, getWidth() / 2 - smallCircleRadiusPix, getHeight() / 2 - smallCircleRadiusPix);
}
}

ამოცანა 3
დაწერეთ GraphicsProgram ქვეკლასი, რომელიც დახატავს acm.program კლასთა
იერარქიის ნაწილს დიაგრამის სახით. მაგალითი ნაჩვენებია სურათზე:
დიაგრამის დასახატად გამოიყენეთ GRect, GLabel, და GLine კლასები. ამ ამოცანაში მთავარ
სირთულეს წარმოადგენს კოორდინატების სწორად მითითება. თქვენ უნდა
გაითვალისწინოთ შემდეგი მოთხოვნები:
 მართკუთხედების სიგრძე და სიგანე უნდა აღწეროთ როგორც კონსტანტები ისე, რომ
ადვილად შეიძლებოდეს შეცვლა.
 კლასების სახელები უნდა იხატებოდეს მართკუთხედების ცენტრში. ტექსტის სიგრძის
და სიგანის გაგება შეგიძლიათ label.getWidth() და label.getAscent()
მეთოდების საშუალებით.
 შემაერთებელი ხაზები უნდა იწყებოდეს და მთავრდებოდეს მართკუთხედების
შესაბამისი გვერდების ცენტრებიდან.
 მთლიანი დიაგრამა უნდა იყოს ფანჯრის ცენტში.

o private static final int rectHeight = 50;


 private static final int rectWidth = 125;

 public void run() {
 drawBoxes();
 drawLines();
 }
 /*This method draws boxes with it's names.*/
 private void drawBoxes() {
 drawProgramBox();
 drawConsoleBox();
 drawDialogBox();
 drawGraphicsBox();
 }
 /*This method draws main, "program" rect and add's its name.*/
 private void drawProgramBox() {
 GRect programBox = new GRect (rectWidth, rectHeight);
 add (programBox, getWidth()/2 - rectWidth/2, getHeight()/2 -
3*rectHeight/2);
 GLabel program = new GLabel ("Program");
 program.setLocation(getWidth()/2 - program.getWidth()/2, getHeight()/2 -
rectHeight + program.getAscent()/2);
 add (program);
 }
 /*This method draws ConsoleProgram box with it's name.*/
 private void drawConsoleBox() {
 GRect consoleBox = new GRect (rectWidth, rectHeight);
 add (consoleBox, getWidth()/2 - rectWidth/2, getHeight()/2 +
rectHeight/2 );
 GLabel console = new GLabel ("ConsoleProgram");
 console.setLocation(getWidth()/2 - console.getWidth()/2, getHeight()/2 +
rectHeight + console.getAscent()/2);
 add (console);
 }
 /*This method draws DialogProgram's rect and adds it's title.*/
 private void drawDialogBox() {
 GRect dialogBox = new GRect (rectWidth, rectHeight);
 add (dialogBox, getWidth()/2 + rectWidth/2 + rectHeight/2, getHeight()/2 +
rectHeight/2);
 GLabel dialog = new GLabel ("DialogProgram");
 dialog.setLocation (getWidth()/2 + rectWidth + rectHeight/2 -
dialog.getWidth()/2, getHeight()/2 + rectHeight + dialog.getAscent()/2);
 add (dialog);
 }
 /*This method draws GraphicsBox and add's relevant label*/
 private void drawGraphicsBox() {
 GRect graphicsBox = new GRect (rectWidth, rectHeight);
 add (graphicsBox, getWidth()/2 - 3*rectWidth/2 - rectHeight/2 ,
getHeight()/2 + rectHeight/2);
 GLabel graphics = new GLabel ("GraphicsProgram");
 graphics.setLocation(getWidth()/2 - rectWidth - rectHeight/2 -
graphics.getWidth()/2 , getHeight()/2 + rectHeight + graphics.getAscent()/2);
 add (graphics);
 }
 /*This method draws lines. x and y coordinates represent the spot where all three
lines gather.*/
 private void drawLines() {
 int x = getWidth()/2;
 int z = rectHeight/2;
 int y = getHeight()/2 - z;
 GLine a = new GLine ( x, y, x - rectWidth - z, getHeight()/2 + z );
 add (a);
 GLine b = new GLine ( x, y, x, getHeight()/2 + z );
 add (b);
 GLine c = new GLine ( x, y, getWidth()/2 + rectWidth + z, getHeight()/2 +
z );
 add (c);
 }

ამოცანა 4
დაწერეთ ConsoleProgram, რომელიც კონსოლიდან წაიკითხავს ორ int ტიპის რიცხვს,
მართკუთხა სამკუთხედის კათეტების მნიშვნელობებს. გამოთვლის ჰიპოტენუზას
მნიშვნელობას და გამოიტანს ეკრანზე double ტიპის რიცხვს.

შეგახსენებთ პითაგორას თეორემას:

სადაც a და b მართკუთხა სამკუთხედის კათეტებია, c კი ჰიპოტენუზა.


პითაგორას თეორემა შეიძლება გადაიწეროს შემდეგნაირად:

რიცხვიდან ფესვის ამოსაღებად გამოიყენეთ Math.sqrt მეთოდი:


double y = Math.sqrt(x);
y–ს მიენიჭება ფესვი x–დან.
public void run() {
/*This code implements a well-known Pythagorean theorem.
* "Whiles" are used to make user type positive integers.*/
println ("Enter values to compute Pythagorean theorem.");
int a = readInt("a: ");
while (a <=0) {
println ("invalid number. Try Again");
a = readInt("a: ");
}
int b = readInt("b: ");
while (b <=0) {
println ("invalid number. Try Again");
b = readInt("b: ");
}
double c = Math.sqrt(a*a +b*b);
println ("c = " + c);
}

ამოცანა 5
დაწერეთ ConsoleProgram, რომელიც ყოველი მომდევნო ხაზიდან კითხულობს int ტიპის
რიცხვებს, მანამ სანამ არ წაიკითხავს გარკვეულ სპეციალურ სიმბოლოს, ჩვენს შემთხვევაში
0–ს (გააკეთეთ ისე რომ შესაძლებელი იყო ამ სიმბოლოს მარტივად შეცვლა). რიცხვების
წაკითხვის შემდეგ პროგრამამ ეკრანზე უნდა გამოიტანოს ამ რიცხვებს შორის მაქსიმალური
და მინიმალური, როგორც სურათზეა ნაჩვენები:

თქვენ უნდა გაითვალისწინოთ შემდეგი სპეციალური შემთხვევები:


 თუ მომხმარებელი შეიყვანს მხოლოდ ერთ რიცხვს, პროგრამამ ეს რიცხვი უნდა
გამოიტანოს როგორც მინიმალურიც და მაქსიმალურიც.
 თუ მომხმარებელი პირდაპირ სპეციალურ სიმბოლოს შეიყვანს, ანუ არცერთ რიცხვს
არ შეიყვანს, ამ შემთხვევაში გამოიტანეთ შესაბამისი მესიჯი.

private static final int specialNum = 0;

public void run() {


/*The number user enters first, is used as a base number, which then is
* compared to every following number and changed if each following number is
* more/less than the max/min number. If the first number is specialNumber, then program warns the
user.*/
println("This program finds the largest and smallest numbers.");
int max = readInt("? ");
int min = max;
if (max == specialNum) {
println("no number to operate on.");
} else {
while (true) {
int a = readInt("? ");
if (a == specialNum) {
break;
} else {
if (a < min) {
min = a;
}
if (a > max) {
max = a;
}

}
}

println("largest: " + max);


println("smallest: " + min);
}
}

ამოცანა 6
განვიხილოთ შემდეგი პროცესი:
აიღეთ ნატურალური რიცხვი n.
თუ n ლუწია გაყავით 2 ზე.
თუ n კენტია გაამრავლეთ 3 ზე და დაუმატეთ 1.
გააგრძელეთ ეს პროცესი მანამ სანამ n არ გახდება 1.

ქვემოთ მოცემულია ეს პროცესი n = 15 ისთვის:


15 კენტია, ამიტომ ვაკეთებთ 3n+1: 46
46 ლუწია, ასე რომ ვყოფთ ორზე: 23
23 კენტია, ამიტომ ვაკეთებთ 3n+1: 70
70 ლუწია, ასე რომ ვყოფთ ორზე: 35
35 კენტია, ამიტომ ვაკეთებთ 3n+1: 106
106 ლუწია, ასე რომ ვყოფთ ორზე: 53
53 კენტია, ამიტომ ვაკეთებთ 3n+1: 160
160 ლუწია, ასე რომ ვყოფთ ორზე: 80
80 ლუწია, ასე რომ ვყოფთ ორზე: 40
40 ლუწია, ასე რომ ვყოფთ ორზე: 20
20 ლუწია, ასე რომ ვყოფთ ორზე: 10
10 ლუწია, ასე რომ ვყოფთ ორზე: 5
5 კენტია, ამიტომ ვაკეთებთ 3n+1: 16
16 ლუწია, ასე რომ ვყოფთ ორზე: 8
8 ლუწია, ასე რომ ვყოფთ ორზე: 4
4 ლუწია, ასე რომ ვყოფთ ორზე: 2
2 ლუწია, ასე რომ ვყოფთ ორზე: 1

დაწერეთ ConsoleProgram, რომელიც კითხულობს ნატურალურ რიცხვს და გამოქვს


პროცესის თითოეული ბიჯი. მაგალითი ნაჩვენებია სურათზეა:

საგულისხმოა რომ ვერავინ მოახერხა იმის დამტკიცება, რომ ეს პროცესი ნებისმიერი საწყისი
რიცხვისთვის დასრულდება(ანუ ერთამდე მივალთ).

public void run() {


/*This code takes a number from user and divides it in two if it's even. If it's odd, it multiplies it by 3
and adds 1. This process goes on until
* the number reaches 1. Every calculation is counted as steps. From the beginning, step equals to
zero, as no calculation is done yet.
* If the number equals 1 from the beginning, then the number of calculation (steps) remain 0. After
every calculation, step increases by 1.
* For every following step, number (that is to define whether it's odd or even) equals to previous
calculation's result. */
int step = 0;
int number = readInt("Enter a number: ");
while (number <= 0) {
println ("Invalid number. Try again.");
number = readInt("Enter a number: ");
}
while (true) {
if (number == 1) {
println("The process took " + step + " steps to reach 1");
break;
}
if (number % 2 != 0) {
int odd = number * 3 + 1;
println(number + " is odd, so I make 3n+1: " + odd);
step += 1;
number = odd;
} else {
int half = number / 2;
println(number + " even, so I take half: " + half);
number = half;
step += 1;
}
}
}

You might also like