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

Add Composition Example #22

Merged
Merged
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
39 changes: 39 additions & 0 deletions Java/Algorithm/FindTheOccurenceOfCharacter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import java.util.HashMap;
import java.util.Map;

//Given a string, count the frequency/occurence of each Character
public class FindTheOccurenceOfCharacter {
static HashMap<Character, Integer> getOccurence(String inputString) {
HashMap<Character, Integer> characterFrequencyMap
= new HashMap<Character, Integer>();


char[] inputArray = inputString.toLowerCase().toCharArray();

for (char character : inputArray) {
if (characterFrequencyMap.containsKey(character)) {
characterFrequencyMap.put(character, characterFrequencyMap.get(character) + 1);
}
else {
characterFrequencyMap.put(character, 1);
}
}

return characterFrequencyMap;
}

static void printOccurence(HashMap<Character, Integer> map){
for (Map.Entry<Character, Integer> value: map.entrySet()) {
System.out.println(value.getKey() + "->" + value.getValue());
}
}

public static void main(String[] args) {
String inputString = "Test";

HashMap<Character, Integer> map = getOccurence(inputString);

printOccurence(map);

}
}
47 changes: 47 additions & 0 deletions Java/OOP/Composition/CompositionStudentExample/Student.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package CompositionStudentExample;

import java.util.List;

public class Student {
private String name;
private int age;
private List<Subject> subjects; //Student has a list of Subjects

public Student(String name, int age, List<Subject> subjects) {
this.setName(name);
this.setAge(age);
this.setSubjects(subjects);
}

public List<Subject> getSubjects() {
return subjects;
}

public void setSubjects(List<Subject> subjects) {
this.subjects = subjects;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public void printStudentDetails() {
System.out.println("Student " + this.name + " has taken the sujects ");
for (int i = 0; i < subjects.size(); i++) {
System.out.println(subjects.get(i).getName());
}
}

}
27 changes: 27 additions & 0 deletions Java/OOP/Composition/CompositionStudentExample/Subject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package CompositionStudentExample;

public class Subject {
private String code;
private String name;

public Subject(String code, String name) {
this.setCode(code);
this.setName(name);
}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
34 changes: 34 additions & 0 deletions Java/OOP/Composition/CompositionStudentExample/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package CompositionStudentExample;

import java.util.ArrayList;
import java.util.List;

public class Test {
public static void main(String[] args) {
Subject english = new Subject("ENG101", "English");
Subject mathematics = new Subject("MATH101", "Mathematics");
Subject science = new Subject("SCI01", "Science");

Subject economics = new Subject("ECO101", "Economics");
Subject businessStudies = new Subject("BUS01", "Business Studies");
Subject accounts = new Subject("ACC101", "Accounts");

List<Subject> subjectsForScienceStudent = new ArrayList<Subject>();
subjectsForScienceStudent.add(english);
subjectsForScienceStudent.add(mathematics);
subjectsForScienceStudent.add(science);

List<Subject> subjectsForCommerceStudent = new ArrayList<Subject>();
subjectsForCommerceStudent.add(english);
subjectsForCommerceStudent.add(economics);
subjectsForCommerceStudent.add(businessStudies);
subjectsForCommerceStudent.add(accounts);

Student commerceStudent = new Student("Ravi",18, subjectsForCommerceStudent);
Student scienceStudent = new Student("Chavi",19, subjectsForScienceStudent);

commerceStudent.printStudentDetails();
scienceStudent.printStudentDetails();
}

}