Skip to content

Friendlist #157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path="lectures/3-first-program"/>
<classpathentry kind="src" path="lectures/4-new-methods"/>
<classpathentry kind="src" path="lectures/5-for-loop"/>
<classpathentry kind="src" path="lectures/7-if-statement"/>
<classpathentry kind="src" path="problem-set"/>
<classpathentry kind="lib" path="C:/Users/user/Desktop/assignment1/karel.jar"/>
<classpathentry kind="lib" path="/Assignment2/acm.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>programming-methodology-java</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
33 changes: 33 additions & 0 deletions problem-set/CountEvenNumbers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// importing console program
import acm.program.ConsoleProgram;

/*
* This program reads integers from user until he enters -1 and counts how many
* even numbers are among the entered numbers.
*/

public class CountEvenNumbers extends ConsoleProgram {

// This global variable is the indicator that we should stop reading integers
// form user and print the output
private final static int SENTINEL = -1;

public void run() {
// The variable which stores total number of even numbers
int evens = 0;
// Reading integers from user and increment 'even' variable if the given number
// is even
while (true) {
int input = readInt("Enter the number or -1 for stop");
// Check if the number is SENTINEL and stop reading if it is
if (input == SENTINEL) {
break;
}
if (input % 2 == 0) {
evens++;
}
}
// Printing the result
println("The total number of even numbers are: " + evens);
}
}
50 changes: 50 additions & 0 deletions problem-set/Frendliest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

// Importing map and hashmap
import java.util.HashMap;
import java.util.Map;
//Importing console program
import acm.program.ConsoleProgram;

public class Frendliest extends ConsoleProgram {
// This program prints the person who has the highest number of friends
public void run() {
setFont("-20");
Map<String, Integer> friendlist = new HashMap<>();
readPairs(friendlist);
printMostPopularPerson(friendlist);
}

// This method searches the most popular person in map and prints his/her name in console
private void printMostPopularPerson(Map<String, Integer> friendlist) {
String person = "";
int max = 0;
for(String key : friendlist.keySet()) {
if(friendlist.get(key) > max) {
max = friendlist.get(key);
person = key;
}
}
println(person + " has the highest number of friends!");
}

// This method reads string pairs from console and stores the number of friends for each person
// in map. Each pair contains two people's names.
private void readPairs(Map<String, Integer> friendlist) {
while(true) {
String friend = readLine("Enter the first person's name or [enter] to end: ");
if(friend.equals("")) {
break;
}
String person = readLine("Enter the second person's name or [enter] to end: ");
if(person.equals("")) {
break;
}
if(friendlist.containsKey(person)) {
friendlist.put(person, friendlist.get(person) + 1);
} else {
friendlist.put(person, 1);
}
println(friend + " is a friend of " + person);
}
}
}
Binary file added problem-set/Friendlist.md
Binary file not shown.
65 changes: 65 additions & 0 deletions problem-set/MostFrequentSymbol.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Importing console program
import acm.program.ConsoleProgram;

public class MostFrequentSymbol extends ConsoleProgram {

// This program reads string from console and prints the most frequent symbol in it
public void run() {
while(true) {
String input = readLine("Enter the string: ");
// Initialising array with 26 so that each index has frequency value of the corresponding
// character.
int[] frequencies = new int[26];
countFrequencies(frequencies, input);
char result = findMostFrequentSymbol(frequencies, input);
println("The most frequent symbol is: " + result);
}
}

// This method searches and returns the most frequent character
private char findMostFrequentSymbol(int[] frequencies, String input) {
int maxFreq = 0;
char result = 'a';
for(int i = 0; i < frequencies.length; i++) {
if(maxFreq < frequencies[i]) {
result = (char)('a' + i);
maxFreq = frequencies[i];
}
}
return result;
}

// This method counts the frequencies of the characters and stores them in array
private void countFrequencies(int[] frequencies, String input) {
for(int i = 0; i < input.length(); i++) {
char curChar = input.charAt(i);
int index = -1;
if(isUpperCase(curChar)) {
index = curChar - 'A';
} else if(isLowerCase(curChar)){
index = curChar - 'a';
} else {
continue;
}
frequencies[index]++;
}
}

// This method checks if the character is lower case or not
private boolean isLowerCase(char curChar) {
if(curChar >= 'a' && curChar <= 'z') {
return true;
} else {
return false;
}
}

// This method checks if the character is upper case or not
private boolean isUpperCase(char curChar) {
if(curChar >= 'A' && curChar <= 'Z') {
return true;
} else {
return false;
}
}
}
Empty file.
73 changes: 73 additions & 0 deletions problem-set/Robot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@

// Importing graphics program.
import acm.program.GraphicsProgram;
// Import for colours.
import java.awt.Color;
// Import for rectangles.
import acm.graphics.GRect;

public class Robot extends GraphicsProgram {

// Some useful constants
private static final int FACE_LENGTH = 300;
private static final int FACE_WIDTH = 400;
private static final int EYE_SIZE = 50;
private static final int NECK_WIDTH = 100;
private static final int NECK_HEIGHT = 300;
private static final int MOUTH_WIDTH = 100;
private static final int MOUTH_HEIGHT = 20;
private static final int MOUTH_OFFSET = 70;

public void run() {
drawRobot();
}

// This problem can be divided into 4 sub-problems.
private void drawRobot() {
drawNeck();
drawFace();
drawEyes();
drawMouth();
}

// This method draws the face of the robot. Robot has gray rectangle face.
private void drawFace() {
GRect face = new GRect(getWidth() / 2 - FACE_WIDTH / 2, getHeight() / 2 - FACE_LENGTH / 2, FACE_WIDTH,
FACE_LENGTH);
face.setFilled(true);
face.setFillColor(Color.LIGHT_GRAY);
add(face);
}

// This method draws the eyes of the robot. Robot has round black eyes.
private void drawEyes() {
// Lets create one universal method in order to not repeat the code.
drawOneEye(getWidth() / 2 - FACE_WIDTH / 3);
drawOneEye(getWidth() / 2 + FACE_WIDTH / 3 - EYE_SIZE);
}

// This method takes the eye width as a parameter and draws black eye with that width.
private void drawOneEye(int width) {
GRect eye = new GRect(width, getHeight() / 2 - FACE_LENGTH / 6, EYE_SIZE, EYE_SIZE);
eye.setFilled(true);
eye.setColor(Color.BLACK);
add(eye);
}

// This method draws the mouth of the robot. Robot has black rectangle mouth.
private void drawMouth() {
GRect mouth = new GRect(getWidth() / 2 - MOUTH_WIDTH / 2, getHeight() / 2 + MOUTH_OFFSET,
MOUTH_WIDTH, MOUTH_HEIGHT);
mouth.setFilled(true);
mouth.setColor(Color.BLACK);
add(mouth);
}

// This method draws the neck of the robot. Robot has gray rectangle neck.
private void drawNeck() {
GRect neck = new GRect(getWidth() / 2 - NECK_WIDTH / 2, getHeight() - NECK_HEIGHT, NECK_WIDTH, NECK_HEIGHT);
neck.setFilled(true);
neck.setFillColor(Color.LIGHT_GRAY);
add(neck);
}
}
76 changes: 76 additions & 0 deletions problem-set/Robot.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#Robot

## ამოცანის პირობა:
დახატეთ რობოტის სახე კანვასზე.

## ამოხსნა:
პირველი რიგში უნდა მოვიფიქროთ ამ ამოცანის კარგი დეკომპოზიცია. დეკომპოზიცია ძალიან გვეხმარება, განსაკუთრებით როცა საქმე გრაფიკულ პროგრამასთან გვაქვს, ვინაიდან დეკომპოზიციის გარეშე ხშირად მოგვიწევს ერთი და იმავე კოდის ბევრჯერ დაწერა.
ამ ამოცანისთვის გამოვიყენოთ შემდეგი დეკომპოზიცია: დავხატოთ კისერი, სახე, თვალები და პირი.

მთავარი მეთოდი მიიღებს ამგვარ სახეს:

private void drawRobot() {
drawNeck();
drawFace();
drawEyes();
drawMouth();
}

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

### დავიწყოთ drawNeck() მეთოდით:

private void drawNeck() {
GRect neck = new GRect(getWidth() / 2 - NECK_WIDTH / 2, getHeight() - NECK_HEIGHT, NECK_WIDTH, NECK_HEIGHT);
neck.setFilled(true);
neck.setFillColor(Color.LIGHT_GRAY);
add(neck);
}

ამ მეთოდით კანვასზე დაიხატება მართკუთხედი. კისერი უნდა დავხატოთ კანვასზე ჰორიზონტალურად გათანაბრებულად, ანუ მარჯვენა და მარცხენა კანვასის კიდეებამდე დაშორება უნდა იყოს თანაბარი ნებისმიერი კანვასის ზომისათვის, ამიტომაც აუცილებელია, რომ მართკუთხედის მდებაორება დამოკიდებული გავხადოთ getWidth() ცვლადზე, რომელიც აღნიშნავს კანვასის სიგანეს. ასევე კოდის ადვილად შეცვლის შესაძლებლობის შესანარჩუნებლად კარგი იქნება თუ გამოვიყენებთ კონსტანტებს კისრის ზომებისთვის. ამ შემთხვევაში ეს კონსტანტებია NECK_WIDTH და NECK_HEIGHT, რომლებიც აღნიშნავენ კისრის სიგანესა და სიმაღლეს. GRect neck არის მართკუთხედის ობიექტი, რომელსაც გააჩნია ორნაირი კონსტრუქტორი (ანუ ორნაირად შეგვიძლია აღვწეროთ). პირველი ვარიანტი არის ის რაც წერია, ანუ გადავცეთ სიგრძე/სიგანე/x/y და ასევე შეგვეძლო აღწერისას მხოლოდ სიგრძე/სიგანე გადაგვეცა, თუმცა ამ შემთხვევაში add-ში მოგვიწევდა კოორდინატების გადაცემა add(neck, x, y), ხოლო თუ არ გადავცემდით, დეფოლტად 0,0 კოორდინატებზე დახატავდა მართკუთხედს. neck.setFilled(true) - ეს ხაზი უზრუნველყოფს, რომ მართკუთხედის ობიექტს ჰქონდეს შიგთავსი. neck.setFillColor(Color.LIGHT_GRAY - ხოლო ეს ხაზი კი მართკუთხედის შიგთავსს აფერადებს იმ ფრად, რომელსაც გადავცემთ (ჩვენს შემთხვევაში LIGHT_GRAY). add არის ობიექტის კანვასზე გამოსაჩენი ბრძანება, მის გარეშე ობიექტი კანვასზე არ დაიხატება.

### drawFace() მეთოდი:

private void drawFace() {
GRect face = new GRect(getWidth() / 2 - FACE_WIDTH / 2, getHeight() / 2 - FACE_LENGTH / 2, FACE_WIDTH,
FACE_LENGTH);
face.setFilled(true);
face.setFillColor(Color.LIGHT_GRAY);
add(face);
}

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

### drawEyes() მეთოდი:
private void drawEyes() {
drawOneEye(getWidth() / 2 - FACE_WIDTH / 3);
drawOneEye(getWidth() / 2 + FACE_WIDTH / 3 - EYE_SIZE);
}

ეს მეთოდი ხატავს ორ თვალს რობიტის სახეზე, რომლებიც უნდა იყოს რობოტის სახის ზომებსა და კანვასის სიგანეზე დამოკიდებული. აქ მნიშვნელოვანი დეტალია უნივერსალური მეთოდის გაკეთება. ვინაიდან ორივე თვალის დახატვა თითქმის ერთი და იგივე კოდის, ჩვენ შეგვიძლია ცალკე მეთოდი გავაკეთოთ და ორჯერ გამოვიძახოთ ნაცვლად იმისა, რომ ერთიდაიგივე კოდი ორჯერ დავწეროთ. ჯერ უნდა დავფიქრდეთ იმაზე თუ რა განასხვავებს თვალებს. თვალები ზუსტად ერთიდაიგივეა გარდა მათი მდებარეობისა. განსხვავებული აქვთ მხოლო x კოორდინატი. ამიტომაც შევქმანთ მეთოდი რომელსაც გადავცემთ x კოორდინატს, როგორც ცვლადს, და ეს მეთოდის დახატავს თვალს ამ x კოორდინატზე. ეს მეთოდი გამოიყურება ასე:

### drawOneEye(int x) მეთოდი:

private void drawOneEye(int x) {
GRect eye = new GRect(width, getHeight() / 2 - FACE_LENGTH / 6, EYE_SIZE, EYE_SIZE);
eye.setFilled(true);
eye.setColor(Color.BLACK);
add(eye);
}


### და ბოლოს drawMouth() მეთოდი:

private void drawMouth() {
GRect mouth = new GRect(getWidth() / 2 - MOUTH_WIDTH / 2, getHeight() / 2 + MOUTH_OFFSET,
MOUTH_WIDTH, MOUTH_HEIGHT);
mouth.setFilled(true);
mouth.setColor(Color.BLACK);
add(mouth);
}

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

## შესაძლო ხარვეზები იმპლემენტაციისას:
1. კანვასის ზომების ცვლადების (getWidth() და getHeight()) გარეშე ობიექტების მდებარეობის განსაზღვრა, რაც გამოიწვევს იმას, რომ კოდის გამოვა ნაკლებად კითხვადი, მოგვიწევს ბევრი წვალება კოორდინატების ზუსტად შესარჩევად და სხვა ზომის კანვასზე რობოტის სახე არასწორად არ იქნება ისეთი პროპორცოული, როგორიც იყო თავდაპირველად.
2. დეკომპოზიციის გარეშე დაწერა. ამ შემთხვევაში მოგვიწევს მთლიანი კოდის ერთ მეთოდში დაწერა, რაც მას გახდის ძნელად წასაკითხსა და გასაგებს. ამასთან რთული იქნება კოდში შეცდომის პოვნა.