Data Structure Help - Understanding the thought process - java

public class Example {
private static class Courses {
public final String name;
public final Courses[] children;
public Courses(String name, Courses ... children) {
this.name = name;
this.children = children;
}
}
public static void main(String[] args) {
Courses courses =
new Courses("School",
new Courses("Mathematics",
new Courses("Algebra"),
new Courses("Trig"),
new Courses("Calculus"),
new Courses("Calculus 2"),
new Courses("Geometry")),
new Courses("Sciences",
new Courses("Biology"),
new Courses("Chemistry"),
new Courses("Physics"),
new Courses("Business",
new Courses("Finances",
new Courses("Accounting"),
new Courses("Accounting 1"),
new Courses("Accounting 2"),
new Courses("Administration",
new Courses("Economics"),
new Courses("Business Studies"),
new Courses("Administration 1"),
new Courses("Accounting"))),
new Courses("Physical Education"))));
System.out.println(find(courses, "Economics", courses.name));
public static String find(Courses courses, String name, String currentPath) {
if((courses.name).equals(name)){
System.out.println(currentPath);
return currentPath + " / " + name;
}
else{
//System.out.println(currentPath);
for(Courses child:courses.children){
currentPath += " / " + child.name;
find(child, name, currentPath);
}
}
return currentPath + " / " + name;
}
}
So this is the code that I have acquired. I'm trying to determine what the right thinking pattern should be when coding this find courses method. This is an array but I'm thinking of it in like a tree like manner and trying to find the answer. IS that something you guys would do too? I'm trying to find a path like this School / Business / Administration / Economics. But either I'm getting the whole path or it's iterating through the whole thing. Also, what's the approach you guys will take to accomplish this. I wrote a recursive method to achieve this, but its not working out.
Thanks, for your help
CC

Thinking about it as a tree is obviously the way to go, as it is indeed a tree.
It might help you to think what you expect the method to do if the current value of courses was the parent of the node you want. That is in your case, if it's the Administration node. In your current implementation, you will iterate over all the children, never realizing you found the correct child!
As another hint, you would generally in recursion want to do something with the result of the recursive call. In your code, you call find(child, name, currentPath) and then you do nothing with the result!
Hope these hints help you.

Your question contains some opinion-based (sub)questions. These we cannot answer, but we can help with your recursive algorithm.
The if branch of your find method seems alright. It enters that branch when an exact match occurs. The problem is in your else branch, where you need recursion to keep looking in the children courses.
Note that your method declaration returns a String.
public static String find(Courses, String, String)
And note that, when you recursively call this function, you are ignoring its return value, rendering the recursive call useless.
for (Courses child: courses.children) {
currentPath += " / " + child.name;
find(child, name, currentPath); // <-- this return value is being ignored!
}
Start by assigning the return value of the recursive search to some variable, and define a return value for when the search doesn't find the provided name (you will not find Biology under Administration, for instance). I'll assume that an empty String means the search didn't find the course.
String result = find(child, name, currentPath);
if (!result.isEmpty()) {
// it has been found
}
Finally, note that you are changing the value of the currentPath variable on each iteration, by appending to it. That will result in erroneous paths, if the name is found after the first iteration. Assign that temporary path, for that iteration, to another variable.

I would do something like this
Course.java
public class Course {
private Course parentCourse;
private String name;
public Course(String name){
this.setName(name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Course getParentCourse() {
return parentCourse;
}
public void setParentCourse(Course parentCourse) {
this.parentCourse = parentCourse;
}
}
Courses.java
public class Courses {
private List<Course> courses;
public Courses(){
}
public List<Course> getCourses() {
return courses;
}
public void setCourses(List<Course> courses) {
this.courses = courses;
}
}
Main
Courses courses = new Courses();
List listCourses = new ArrayList<Course>();
Course generalMaths = new Course("General Maths");
Course linAlgebra = new Course("Linear Algebra");
linAlgebra.setParentCourse(generalMaths);
listCourses.add(generalMaths);
listCourses.add(linAlgebra);
courses.setCourses(listCourses);
Find path
for(Course course : courses.getCourses()){
StringBuffer coursePath = new StringBuffer();
coursePath.append(course.getName());
while(course.getParentCourse() != null){
course = course.getParentCourse();
coursePath.append(" | "+course.getName());
}
System.out.println(coursePath);
}

Related

contains method with list of objects in Java

I have a list of objects "SaleItem". they are all objects of the same class. each object has a String field "name" and an int field "value". I want to see if one of the objects contains a name. It seems that I can't use the "contains" method to do this. I see two solutions. one is to iterate through all the objects to check if one has said name:
for (SaleItem item: myList) {
if (item.getName() == "banana") {
// do stuff
}
}
The other solution would be to create a new list of Strings from "myList" and use the contains method on that:
ArrayList<String> nameList = new ArrayList<>();
for (SaleItem item: myList) {
nameList.add(item.getName());
}
if (nameList.contains("banana")) {
// do stuff
}
I imagine the first method would be most efficient if I'm only doing it once, and the second would be more efficient if I'm doing it many times. Being a bit of a newbie without a formal education, I don't know what's proper in this situation.
Since SaleItem.getName() returns a string, you should be able to use "contains" method.
It seems like you have initialized the ArrayList or the SaleItem object incorrectly.
public class TestApp {
public static void main(String[] args) {
List<SaleItem> list = new ArrayList<SaleItem>();
SaleItem s1 = new SaleItem();
s1.setName("banana");
s1.setValue(1);
SaleItem s2 = new SaleItem();
s2.setName("apple");
s2.setValue(2);
list.add(s1);
list.add(s2);
for (SaleItem item: list) {
if (item.getName().contains("banana")) {
System.out.println("Pass");
}
}
}
}
class SaleItem {
private String name;
private int value;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
Try with this code
public class SaleItem {
private String itemName;
public String getItemName() {
return itemName;
}
public SaleItem setItemName(String itemName) {
this.itemName = itemName;
return this;
}
#Override
public String toString() {
return "[SaleItem : { itemName = " + this.getItemName() + " }]";
}
public static void main(String[] args) {
ArrayList<SaleItem> nameList = new ArrayList<>();
nameList.add(new SaleItem().setItemName("banana"));
nameList.add(new SaleItem().setItemName("grape"));
nameList.add(new SaleItem().setItemName("watermelon"));
nameList.add(new SaleItem().setItemName("orange"));
nameList.add(new SaleItem().setItemName("guava"));
for (SaleItem item : nameList) {
if (item.toString().contains("banana")) {
// Do this
}
}
}
}
A List's .contains method isn't magical, it will generally just loop through the elements checking for equality, O(n) linear performance.
Your first solution is probably fine.
If you really did expect repeated access and wanted better than linear performance on subsequent lookups, you'd probably want to construct a Map<String,SaleItem>, or a Set<String> depending on what you wanted to do with it. But those solutions would normally only work on exact matches. Once you need case-insensitive matches, they have to be TreeMap or TreeSet with a case-insensitive comparator. And if you want partial matching (like using String.contains() or a regular expression), you'd want to go back to a linear search.
But don't do any of that unless you have to. Keep it simple.

JAVA how to check array parameter

I have 2 classes. one is named "shipment", the other is called "Inventory"
inside the shipment, there are some variables as below.
public class Shipment
{
private int trackingCode;
private int priority;
private double shippingPrice;
private double weight;
private String originCity;
private String destCity;
private String trackingPage;
and I create the "Inventory" as below
public class Inventory
{
private ArrayList<Shipment> packages;
public Inventory(Shipment[] listOfPackage)
{
if(listOfPackage == null){
throw new IllegalArgumentException("List of Packages cannot be null.");
}
packages = new ArrayList<Shipment>(Arrays.asList(listOfPackage));
}
Now my question is how do I create a method to add a new package to the ArrayList, and also duplicate tracking code is not allowed need to throw an exception.
public ArrayList<Package> addPackage()
I'm very confusing how to do the duplicate tracking code check because it's one of the Shipment[] array element
You can use the this keyword to refer to your private member variables and still keep a similar naming convention for parameters passed into your constructor or functions. It makes your code much more understandable to others and to yourself =). Having two separate naming conventions for everything you pass into a class can get very confusing.
I have added a main function to demonstrate how you would effectively operate on these classes.
public class Application {
public static void main(String[] args) {
// define some unique shipments
Shipment a = new Shipment(1,1, 10.0, 20.3, "Denver", "Seattle", "xyz");
Shipment b = new Shipment(2,9, 45.88, 130.1, "Denver", "Los Angeles", "xyz");
Shipment c = new Shipment(3,3, 14.67, 6.8, "Houston", "Dallas", "xyz");
Shipment d = new Shipment(1,4, 12.99, 2.3, "New York", "London", "xyz");
// populate your inventory with an array of initial shipments "a", "b", and "c"
Shipment[] initialShipments = new Shipment[] { a, b, c };
Inventory inventory = new Inventory(initialShipments);
// print the inventory before adding the new shipment
System.out.println(inventory.toString());
// add shipment "d" to your inventory with the new method
try {
inventory.addShipment(d);
}
catch (Exception e) {
System.out.println(e.getMessage());
}
// print the inventory after adding the new shipment
System.out.println(inventory.toString());
}
}
For your Inventory:
public class Inventory {
private ArrayList<Shipment> shipments;
public Inventory(Shipment[] shipments) {
if(shipments == null) {
throw new IllegalArgumentException("List of shipments cannot be null.");
}
this.shipments = new ArrayList<>(Arrays.asList(shipments));
}
public void addShipment(Shipment shipment) throws Exception {
Optional<Shipment> duplicateShipment = shipments
.stream()
.filter(otherShipment -> otherShipment.getTrackingCode() == shipment.getTrackingCode())
.findAny();
if(duplicateShipment.isPresent()) {
String errorMessage = MessageFormat.format(
"A shipment with tracking code {0} already exists in this inventory.",
shipment.getTrackingCode()
);
throw new Exception(errorMessage);
}
else {
this.shipments.add(shipment);
}
}
#Override
public String toString() {
return "Inventory{" +
"shipments=" + shipments +
'}';
}
}
In order to check for duplicate tracking codes, you must make a stream of your existing shipments so that you can look for any tracking code in those shipments that match the one you are trying to add.
There are many ways to accomplish this in theory, but I did it by making a stream of your shipments so I could make a filter that looked for any shipment tracking code equal to the one you are adding.
The findAny at the end of this stream just returns an Optional which means that it could potentially return something or not.
With the Optional type, you can test if it found a duplicate by using the isPresent() function. If the duplicate is present, you can throw an exception, as needed.
Here I just made the function throw up the Exception, but you could handle it here in the function and just log that you tried to add the same shipment. In real code, you wouldn't want your code to break because you tried to add a duplicate shipment. You would just want to stop it from happening and move on!
For your Shipment:
public class Shipment {
private int trackingCode;
private int priority;
private double shippingPrice;
private double weight;
private String originCity;
private String destCity;
private String trackingPage;
public Shipment(int trackingCode, int priority, double shippingPrice, double weight, String originCity, String destCity, String trackingPage) {
this.trackingCode = trackingCode;
this.priority = priority;
this.shippingPrice = shippingPrice;
this.weight = weight;
this.originCity = originCity;
this.destCity = destCity;
this.trackingPage = trackingPage;
}
public int getTrackingCode() {
return trackingCode;
}
}
You need to add a 'getter' to the Shipment class so that you can access the tracking code outside of this class; otherwise, it will remain private, and you won't be able to make comparisons outside of this class where you need to check for duplicate tracking codes.

Best way to store collection of objects in Java?

I am creating a dump Java app for student information system for learning and implementing OOPS Concepts like inheritance, abstraction, polymorphism and encapsulation.
What I am doing is, I have created Faculty Class, Student Class and a College Class. Now i want to add new faculty in College. So my approach is to create a method in College class i.e. addFaculty(Faculty f) and fireFaculty(Faculty f), now i want to add Faculties in College class.
Whats the best way to do it? How do i store list of Faculty Object in College Object. Because i can add more than one faculty and more than one student in college.
Whats the best approach to solve this problem in OOPS?
Here is College.java code which i have implemented, it works fine but is this the best way i can solve it?
public class College
{
String name;
String location;
String courses[];
HashMap<String,Faculty> faculties;
int noOfFaculties = 0;
int noOfStudents = 0;
public College(String name,String location,String courses[])
{
this.name = name;
this.location = location;
this.courses = courses;
faculties = new HashMap<>();
}
public void addFaculty(Faculty faculty)
{
faculties.put(faculty.getName(),faculty);
}
public void printFaculties()
{
Set<String> set = faculties.keySet();
if(set.size()>0)
{
for(String s:set)
{
System.out.println(faculties.get(s).getName());
}
}
else
{
System.out.println("No Faculties Currently Working");
}
}
public void fireFaculty(Faculty faculty)
{
faculties.remove(faculty.getName());
}
public String getName()
{
return name;
}
public String getLocation()
{
return location;
}
public String[] getCourses()
{
return courses;
}
}
If you cannot have duplicates use HashSet<Faculty> if you dont mind use a List<Faculty>.
Example:
class College {
private List<Faculty> listFactories = new ArrayList<>(); // dupes allowed
private Set<Faculty> setFactories = new HashSet<>(); // no dupes allowed
}
Check collections API.
There's a ton of ways you can do it. Probably the easiest way to handle storing a collection of objects is by using one of the Collections provided by Java. For beginners, probably the easiest one to understand is an ArrayList, which is basically an array that grows in size dynamically depending on the amount of objects in the collection.
So, as an axample, your code might be something like this:
public class College
{
private ArrayList<Faculty> faculty;
public College()
{
faculty = new ArrayList<Faculty>();
}
public void addFaculty(Faculty f)
{
faculty.add(f);
}
public void fireFaculty(Faculty f)
{
faculty.remove(f);
}
}
imho It depends what kind of services College college offers. If I were coding, I would start with:-
List<Faculy> faculties = new ArrayList<>();
....
public void addFaculty(Faculty f) {
faculties.add(f);
}
//... etc
And change to an altearnative later if needed.

My code doesn't work right? IF-statement multiple classes

I am relatively new to Java and as a newbie I have trouble understanding how the code works or executes. Most often I've figured out the answer in a minute or two, sometimes in an hour or two. However, I've been stuck for two days now and I'm afraid I can't work out the problem on my own.
The programming exercise that I'm currently working on is nearly finished, save for one bit that isn't working right: method setMaxSize doesn't seem to work the way it should. I've tried to edit the method addPlayer to make a IF-statement concerning the team's max size and current size. However, the method does not add players to the list, regardless of the fact that the team list is empty at the moment. What did I do wrong? Where's my mistake? How can I get the IF-statement in addPlayer to accept new players in an empty list while checking for the maximum possible number of players in team?
I'd appreciate any feedback I can get and I apologize if it's a noobish question, but I'm really running out of patience here. Also, it's not homework: it's a programming exercise I found online from a university website I found, but I have trouble finishing it.
I'm including the two class files and the main field.
import java.util.ArrayList;
public class Team {
private String name;
private ArrayList<Player> list = new ArrayList<Player>();
private int maxSize;
public Team (String name){
this.name = name;
}
public String getName(){
return this.name;
}
public void addPlayer(Player player){
if (list.size() <= this.maxSize){
this.list.add(new Player(player.getName(), player.goals()));
}
}
public void printPlayers(){
for (Player player : list){
System.out.println(player.toString());
}
}
public void setMaxSize(int maxSize){
this.maxSize = maxSize;
}
public int size(){
return this.list.size();
}
public int goals(){
int goalSum = 0;
for (Player player : list){
goalSum+=player.goals();
}
return goalSum;
}
}
public class Main {
public static void main(String[] args) {
Team barcelona = new Team("FC Barcelona");
Player brian = new Player("Brian");
Player pekka = new Player("Pekka", 39);
barcelona.addPlayer(brian);
barcelona.addPlayer(pekka);
barcelona.addPlayer(new Player("Mikael", 1));
System.out.println("Total goals: " + barcelona.goals());
}
}
public class Player {
private String name;
private int goal;
public Player(String name){
this.name = name;
}
public Player(String name, int goal){
this.name = name;
this.goal = goal;
}
public String getName(){
return this.name;
}
public int goals(){
return this.goal;
}
public String toString(){
return "Player: " + this.name + ", goals " + this.goal;
}
}
You need to call setMaxSize before adding players to the team.
As you have said before, you are never calling setMaxSize, so there is maxSize is initially 0. Additionally, I suggest making setters and getters for all your data fields in each class, and making each field private.
For example, you have a setter for maxSize, but not getter. It doesn't make much sense to do one but not the other, right? You create better encapsulation and allow for future changes of your code much easier this way, and again it is better practice for future projects.

return ArrayList and printing objects within

I have a problem with an implementation of ArrayLists - I try to print my objects, but right now it only prints the memory adresses. I figure a loop is my savior somehow, but can't for the love of... figure out how to loop through my Course objects to make that happen. What am I missing? It can't be impossible or anything, it's just me being to stupid to figure it out (my forehead has a permanent imprint of my keyboard now).
public class Course{
private String courseID;
private String courseName;
private int ap;
private static ArrayList<Course> courses = new ArrayList<Course>();
public static void main(String[] args) {
Course programmeringGrund = new Course ("725G61", "Programmering Grundkurs", 6);
Course itProjekt = new Course ("725G62", "IT-Projektledning - introduktion", 12);
Course diskretMatematik = new Course ("764G06", "Diskret Matematik och Logik", 6);
Course informatikTeknik = new Course ("725G37", "Informatik, teknik och lärande", 6);
System.out.println(getCourses());
} public Course (String aCourseID, String aCourseName, int anAp){
this.courseID=aCourseID;
this.courseName=aCourseName;
this.ap=anAp;
courses.add(this);
} public static List getCourses(){
return courses;
}
You need to override the toString() method in Course. The "memory address" printed is part of Object's toString() method, and you haven't overridden toString().
public class Course
{
...
#Override
public String toString()
{
return "ID=" + courseID + ", Name=" + courseName;
}
...
}
You have two options:
Traverse the List returned by getCourses() and print out values for each course object. Something like
ArrayList<Course> mycourses = getCourses();
for(Course obj: mycourses)
{
System.out.println("Id is "+obj.courseId);// So on you print everything
}
OR You can override the toString() Method in you Course class as ZouZou mentioned. As you haven't over ridden the toString() method as of now it is printing the memory address for the List

Categories