Skip to content
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
2 changes: 0 additions & 2 deletions README.md

This file was deleted.

29 changes: 29 additions & 0 deletions Week 5/Task 2-Word Count/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
8 changes: 8 additions & 0 deletions Week 5/Task 2-Word Count/.idea/.gitignore

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

6 changes: 6 additions & 0 deletions Week 5/Task 2-Word Count/.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 Week 5/Task 2-Word Count/.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 Week 5/Task 2-Word Count/.idea/vcs.xml

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

4 changes: 4 additions & 0 deletions Week 5/Task 2-Word Count/Lincoln.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Java is /a -programming ,language.
Java is _easy.
the file contains 8

11 changes: 11 additions & 0 deletions Week 5/Task 2-Word Count/Task 2-Word Count.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
28 changes: 28 additions & 0 deletions Week 5/Task 2-Word Count/src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.io.*;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
try {
File file = new File("Lincoln.txt");
Scanner scanner = new Scanner(file);
int count = 0;
while (scanner.hasNext()) {
scanner.next();
count++;
}
scanner.close();

BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));
String str = "the file contains " + count + '\n';
writer.write(str);
writer.newLine();
writer.close();

} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
29 changes: 29 additions & 0 deletions Week 5/Task 3/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
8 changes: 8 additions & 0 deletions Week 5/Task 3/.idea/.gitignore

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

6 changes: 6 additions & 0 deletions Week 5/Task 3/.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 Week 5/Task 3/.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 Week 5/Task 3/.idea/vcs.xml

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

2 changes: 2 additions & 0 deletions Week 5/Task 3/Scores.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
34 34 54 14 32 24 31 34 53 74 22 29
4 34 14 14 32 24 31 34 53 74 22 29
11 changes: 11 additions & 0 deletions Week 5/Task 3/Task 3.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
44 changes: 44 additions & 0 deletions Week 5/Task 3/src/FileManipulation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import java.io.*;
import java.net.URL;
import java.util.*;

public class FileManipulation {
File readFromWebToFile(String url) throws IOException {
URL fileUrl = new URL(url);
InputStream is = fileUrl.openStream();
String filePath = "Scores.txt";
FileOutputStream fos = new FileOutputStream(filePath);

byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
is.close();
fos.close();
return new File(filePath);
}

int getSum(File filePath) throws IOException{
FileInputStream file =new FileInputStream(filePath);
Scanner scanner = new Scanner(file);
int sum = 0;
while (scanner.hasNextInt()) {
int score = scanner.nextInt();
sum += score;
}
return sum;
}

int getAverage(File filePath) throws IOException{
FileInputStream file =new FileInputStream(filePath);
Scanner scanner = new Scanner(file);
int sum = 0 , count = 0;
while (scanner.hasNextInt()) {
int score = scanner.nextInt();
sum += score;
count++;
}
return sum / count;
}
}
16 changes: 16 additions & 0 deletions Week 5/Task 3/src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import java.io.File;
import java.io.IOException;

public class Main {

public static void main(String[] args) {
FileManipulation file = new FileManipulation();
try{
File f = file.readFromWebToFile("http://liveexample.pearsoncmg.com/data/Scores.txt");
System.out.println("The summation of words: " + file.getSum(f));
System.out.println("The average of words: " + file.getAverage(f));
}catch (IOException e){
System.out.println(e.getMessage());
}
}
}
29 changes: 29 additions & 0 deletions Week 5/Task1-InputMismatchException/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
8 changes: 8 additions & 0 deletions Week 5/Task1-InputMismatchException/.idea/.gitignore

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

6 changes: 6 additions & 0 deletions Week 5/Task1-InputMismatchException/.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 Week 5/Task1-InputMismatchException/.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 Week 5/Task1-InputMismatchException/.idea/vcs.xml

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

11 changes: 11 additions & 0 deletions Week 5/Task1-InputMismatchException/InputMismatchException.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
25 changes: 25 additions & 0 deletions Week 5/Task1-InputMismatchException/src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import java.util.InputMismatchException;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num1 , num2 ;
boolean isValidInput = false;

while (!isValidInput) {
try {
System.out.print("Enter first integer: ");
num1 = scanner.nextInt();
System.out.print("Enter second integer: ");
num2 = scanner.nextInt();

System.out.println("You entered " + num1 + " and " + num2 + "\nThe summation is: " + (num1 + num2));
isValidInput = true;
} catch (InputMismatchException e) {
System.out.println("Invalid input, Enter integers only.");
scanner.nextLine();
}
}
}
}