Skip to content
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

Functions yasin vahedian #45

Open
wants to merge 1 commit 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
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/Functions-Introduction.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions Answers/40230112131/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
8 changes: 8 additions & 0 deletions Answers/40230112131/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Answers/40230112131/.idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions Answers/40230112131/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions Answers/40230112131/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>untitled1</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

</project>
235 changes: 235 additions & 0 deletions Answers/40230112131/src/main/java/org/example/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
package org.example;

import java.util.Scanner;

public class Main {
public static void main(String[] args)
{
//2
Scanner scanner = new Scanner(System.in);
String phone;

do {
System.out.print("Phone: ");
phone = scanner.nextLine();
phone = phoneNumber(phone);
} while (phone == null);

System.out.println("Output: " + phone);






//3
String id;

do {
System.out.print("ID: ");
id = scanner.nextLine();
id = userId(id);
} while (id == null);

System.out.println("Output: " + id);






//4
String[] interests = new String[10];
System.out.println("Enter your interests (maximum 10). Enter 'done' to finish:");
int count = 0;
String interest;
do {
System.out.print("Input: ");
interest = scanner.nextLine().trim();

if (!interest.equalsIgnoreCase("done")) {
if (count < 10) {
interests[count] = interest;
count++;
} else {
System.out.println("Maximum number of interests reached.");
break;
}
}
} while (!interest.equalsIgnoreCase("done"));

System.out.print("Output: {");
for (int i = 0; i < count; i++) {
System.out.print("\"" + interests[i] + "\"");
if (i < count - 1) {
System.out.print(", ");
}
}
System.out.println("}");







//5
System.out.print("Full name: ");
String fullName = scanner.nextLine();

System.out.print("Phone number: ");
String phoneNumber = scanner.nextLine();

System.out.print("User ID: ");
String userID = scanner.nextLine();

String[] interests2 = getInterests(scanner);

String output = userFullInformation(fullName, phoneNumber, userID, interests2);
System.out.println(output);







//6
String information = "Hello, my name is Aryanoor. I am learning Java.";
int shift = 3;

String encryptedInformation = informationEncoder(information, shift);
System.out.println("Output: " + encryptedInformation);






//7
System.out.print("Enter your information: ");
String information2 = scanner.nextLine();

// Get the shift value
System.out.print("Enter the shift value: ");
int shift2 = scanner.nextInt();
scanner.nextLine(); // Consume the newline character

// Encrypt the information
String encodedInformation = informationEncoder2(information2, shift2);
System.out.println("Encoded information: " + encodedInformation);

// Decode the encoded information
String decodedInformation = informationDecoder(encodedInformation, shift2);
System.out.println("Decoded information: " + decodedInformation);
}




public static String phoneNumber(String phone) {
if (phone.startsWith("9") && phone.length() == 10) {
return "0" + phone;
} else {
System.out.println("Wrong entry. Try again.");
return null;
}
}
public static String userId(String id) {
if (id.length() >= 4 && id.length() <= 13) {
if (id.matches("\\d+")) {
return id;
} else {
System.out.println("Invalid format. ID should contain only digits.");
return null;
}
} else {
System.out.println("Invalid length. ID should be between 4 to 13 digits.");
return null;
}
}
public static String userFullInformation(String fullName, String phoneNumber, String userID, String[] interests) {
StringBuilder info = new StringBuilder("Hello! My name is " + fullName + ". My ID is " + userID + ". Here are some of my interests:\n");

for (int i = 0; i < interests.length; i++) {
info.append(i + 1).append(". ").append(interests[i]).append("\n");
}

info.append("\nYou can reach me via my phone number ").append(phoneNumber).append(".");
return info.toString();
}

public static String[] getInterests(Scanner scanner) {
String[] interests = new String[10];

System.out.println("Enter your interests (maximum 10). Enter 'done' to finish:");

int count = 0;
String interest;
do {
System.out.print("Interest: ");
interest = scanner.nextLine().trim();

if (!interest.equalsIgnoreCase("done")) {
if (count < 10) {
interests[count] = interest;
count++;
} else {
System.out.println("Maximum number of interests reached.");
break;
}
}
} while (!interest.equalsIgnoreCase("done"));

// Truncate the interests array if the count is less than 10
String[] truncatedInterests = new String[count];
System.arraycopy(interests, 0, truncatedInterests, 0, count);
return truncatedInterests;
}

public static String informationEncoder(String information, int shift) {
StringBuilder encrypted = new StringBuilder();

for (int i = 0; i < information.length(); i++) {
char currentChar = information.charAt(i);

// Encrypt alphabetic characters (ignore non-alphabetic characters)
if (Character.isLetter(currentChar)) {
// Determine if the character is uppercase or lowercase
char base = Character.isUpperCase(currentChar) ? 'A' : 'a';
// Apply the Caesar Cipher shift
char encryptedChar = (char) (((currentChar - base + shift) % 26) + base);
encrypted.append(encryptedChar);
} else {
// Keep non-alphabetic characters unchanged
encrypted.append(currentChar);
}
}

return encrypted.toString();
}
public static String informationEncoder2(String information, int shift) {
return applyCipher(information, shift);
}

public static String informationDecoder(String encodedInformation, int shift) {
// To decode, we apply a negative shift
return applyCipher(encodedInformation, -shift);
}

private static String applyCipher(String text, int shift) {
StringBuilder result = new StringBuilder();

for (char character : text.toCharArray()) {
if (Character.isLetter(character)) {
char base = Character.isUpperCase(character) ? 'A' : 'a';
char encryptedChar = (char) (((character - base + shift + 26) % 26) + base);
result.append(encryptedChar);
} else {
result.append(character);
}
}

return result.toString();
}
}