How JustAnswer Works:

  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.

Ask TheDoctor Your Own Question

TheDoctor
TheDoctor, Professional w/Adv. Degree
Category: Homework
Satisfied Customers: 1485
Experience:  M.S. in Internet Information Systems
59032869
Type Your Homework Question Here...
TheDoctor is online now
A new question is answered every 9 seconds

Write a program which returns the name of every teacher "Student1"

Resolved Question:

Write a program which returns the name of every teacher "Student1" has had from these pairs. In Java. Please solve it in 2 ways:
1. Using loops/arrays
2. Using hashmaps/hashtable.

(Teacher1, Student1) , (Teacher2, Student1), (Teacher3, Student2), (Teacher4, Student1)
Submitted: 1 year ago.
Category: Homework
Expert:  TheDoctor replied 1 year ago.
Hello and thank you for your question. It would be my pleasure to assist you with this.

I will contact you once this is complete.

Thank you
Expert:  TheDoctor replied 1 year ago.
Would you like the hashmap solution and the array solution demonstrated within the same program? Or would you prefer them to be separate programs?
Customer: replied 1 year ago.

separate please.

Expert:  TheDoctor replied 1 year ago.
Hello again!

Please download the files here:

Attachment: 2013-09-06_024954_teachers.zip



In this zip file, you will find three different java files. They are:

TeachersHM.java -- The HashMap version of the code
TeachersAR.java -- The arrays version of the code
Teacher.java -- A version that does both

I created one that does both for you just in case you wanted it. You still have the two that are separate.

I have fully commented the code. In all of the files.

Let me know if you need anything or have any questions. I'm here to help!

If you need any assistance in the future, please feel free to start your questions with **For TheDoctor** to request me directly.

Please remember to rate my answer. Thank you so much and have a wonderful day!
Customer: replied 1 year ago.

for the array, could you do it without using built-in functions?


 


using a logic approach? I need to understand how it works.


 


example:


 


for (int i=0; i<myArray.length; i++)


{


myArray[1]= ........


 


}

Customer: replied 1 year ago.

perhaps using this multi-dimensional array:


 


String[][][][] myArray= {{{{"Teacher1", "Student1"} , {"Teacher2", "Student1"}, {"Teacher3", "Student2"}, {"Teacher4", "Student1"}}}} ;

Expert:  TheDoctor replied 1 year ago.
First, what do you mean by built in functions? There are no special built in functions being used.

Second, that multidimensional array is not accurate, but I will see what I can do...
Customer: replied 1 year ago.

Thanks. If you can break down the program as much as possible to a logical level, that would be great. I need to see something like myArray [1] which would be the index locations...etc

Expert:  TheDoctor replied 1 year ago.
When you loop through an array, you usually use:

myArray[variable], not an actual number. That usually does not make any sense.

Take your above code:

for(int i = 0; i < myArray.length; i++) {

myArray[1]....

Now, in that loop, you will ALWAYS be referring to the SECOND element of the array (arrays always start at index 0). The entire purpose of looping through an array is to get the NEXT ELEMENT of the array on each loop through. That is why the loop variable i exists. When you make a call to:

myArray

It isn't any special built in function. The first time it loops, that is just myArray[0], then myArray[1], then myArray[2] and so on and so forth. It is the correct way to loop through an array. If you explicitly put an integer constant in the index value, then you are not looping through the array correctly.

You loop through arrays with index variables, not index constants. If I had a student who did that, they would receive serious deductions because the program simply wouldn't work correctly. That's not how loop through an array. It won't work.

All that said, you could use a two-dimensional array, loop through the dimensions, and then make an explicit call to the indexes at the deepest level (student/teacher pair). It is not "more logical". It's just adding an additional layer of data.

Take a look at this file:

Attachment: 2013-09-06_033418_teachersar.zip



In this one, we use a two-dimensional array in order to specify the teacher/student pair. Then we MUST loop through the first dimension and explicitly reference the second dimension with index constants. It is closer to what you were asking for and is also a completely valid way of doing this. Though the Hashmap is really the preferred method is cases of key/value pairs.

I don't think we should change it beyond this, as it will start to become convoluted and you will likely receive very low marks. this code is as "logic" and broken down as it gets. It is text-book array looping.

Let me know if this is what you were looking for and please remember to rate my answer.

Thank you again!
Customer: replied 1 year ago.

This looks great, one question, is this expressions just looping through, I have worked with colons in a for loop before, is there another way to replace this expression:


 


for(String[] teacherStudent : students)

Expert:  TheDoctor replied 1 year ago.

Looping through an array using the colon like this is done when there is absolutely no need to reference the index of the first dimension with a variable (which you seem to want to avoid anyway). The way that I did it is the cleaner, better way of doing it. Whenever you can do it like this, you should. It is the equivalent of C#'s and PHP's foreach function. It is a staple of good programming.

But yes, you can do it a dirty way, which will require you to use index variables. like this:

for(int xy = 0; xy < students.length; xy++) {

if(students[xy][1] == students) {

System.out.println(students[xy][0]);

}

}

This does the exact same thing as the code that I just sent you, but uses index variables instead of member instances. As I said, it is dirtier way. The only real reason to do it this way is if you needed the array index (xy) for some reason. And you MUST use xy was the array index. In increments on each loop through (0 - 3), hence the xy++ in the for statement. The first time it loops through, it will perform the code on students[0][1], then students[1][1], then students[2][1], the students[3][1]. By looping, we can use the index incrementing loop variable xy so that we only have to write the conditional code once. Otherwise, we wouldn't be using a loop at all, and that wouldn't meet your requirements.

The only real reason to do it this other was is if you needed the index for something -- which you don't in this case. But either way works.

As a project lead and lead engineer on many projects, I would prefer my developers to use the cleaner way, but that's up to you. I think your instructor would prefer the cleaner way as well. But some instructors are weird about stuff.

If you want to use this other version, here it is:

 

http://wikisend.com/download/279074/TeachersAR2.zip

But you must use xy was the index key for the first dimension of the array. That how looping occurs.

 

Honestly, you should receive full marks for either version. If I were you, I would submit the version with the colon. It is more in-line with what would be done in the real world. No need to use verbose for loops when a foreach will do the job.

 

Anyway. Please do remember to rate my answer.

 

Thank you again.

Customer: replied 1 year ago.

This is not for homework or anything, Im just trying to learn programming myself.


 


For this expression:


if(students[xy][1] == students)


 


getting the following error:


Incompatible operand types String and String[][]


 


Any thoughts?


 

Expert:  TheDoctor replied 1 year ago.
You must have not changed something correctly. Try this version:

Attachment: 2013-09-06_043358_teachersar3.zip



This version uses the code mentioned and works without any errors.

The colon is the preferred method in the real world. If this were PHP, it would be:

foreach($students as $studentTeacher)

In C#, it would be:

foreach(String[] studentTeacher in students)

And in Java, its:

for(String[] studentTeacher : students)

But the copy I just sent you will work without using this type of for loop.

Let me know if you need anything else. And please remember to rate my answer.

If you have further Java, C#, PHP, ActionScript, HTML/CSS, ColdFusion, Ruby, Ruby on Rails -- programming in general questions, please feel free to ask for me in the future!
Expert:  TheDoctor replied 1 year ago.
THIS ANSWER IS LOCKED!
You can view this answer by clicking here to Register or Login and paying $3.
If you've already paid for this answer, simply Login.
TheDoctor, Professional w/Adv. Degree
Category: Homework
Satisfied Customers: 1485
Experience: M.S. in Internet Information Systems
TheDoctor and 4 other Homework Specialists are ready to help you
Customer: replied 1 year ago.

I appreciate it, you are awesome!

Expert:  TheDoctor replied 1 year ago.
Thank you. You are also awesome! It was a pleasure working with you.

JustAnswer in the News:

 
 
 
Ask-a-doc Web sites: If you've got a quick question, you can try to get an answer from sites that say they have various specialists on hand to give quick answers... Justanswer.com.
JustAnswer.com...has seen a spike since October in legal questions from readers about layoffs, unemployment and severance.
Web sites like justanswer.com/legal
...leave nothing to chance.
Traffic on JustAnswer rose 14 percent...and had nearly 400,000 page views in 30 days...inquiries related to stress, high blood pressure, drinking and heart pain jumped 33 percent.
Tory Johnson, GMA Workplace Contributor, discusses work-from-home jobs, such as JustAnswer in which verified Experts answer people’s questions.
I will tell you that...the things you have to go through to be an Expert are quite rigorous.
 
 
 

What Customers are Saying:

 
 
 
  • Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C. Freshfield, Liverpool, UK
< Last | Next >
  • Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C. Freshfield, Liverpool, UK
  • This expert is wonderful. They truly know what they are talking about, and they actually care about you. They really helped put my nerves at ease. Thank you so much!!!! Alex Los Angeles, CA
  • Thank you for all your help. It is nice to know that this service is here for people like myself, who need answers fast and are not sure who to consult. GP Hesperia, CA
  • I couldn't be more satisfied! This is the site I will always come to when I need a second opinion. Justin Kernersville, NC
  • Just let me say that this encounter has been entirely professional and most helpful. I liked that I could ask additional questions and get answered in a very short turn around. Esther Woodstock, NY
  • Thank you so much for taking your time and knowledge to support my concerns. Not only did you answer my questions, you even took it a step further with replying with more pertinent information I needed to know. Robin Elkton, Maryland
  • He answered my question promptly and gave me accurate, detailed information. If all of your experts are half as good, you have a great thing going here. Diane Dallas, TX
 
 
 

Meet The Experts:

 
 
 
  • Manal Elkhoshkhany

    Tutor

    Satisfied Customers:

    4520
    More than 5000 online tutoring sessions.
< Last | Next >
  • http://ww2.justanswer.com/uploads/BU/BusinessTutor/2012-2-2_115741_Kouki2.64x64.jpg Manal Elkhoshkhany's Avatar

    Manal Elkhoshkhany

    Tutor

    Satisfied Customers:

    4520
    More than 5000 online tutoring sessions.
  • http://ww2.justanswer.com/uploads/LI/lindaus/2012-6-10_04811_IMG20120609164157.64x64.jpg Linda_us's Avatar

    Linda_us

    Finance, Accounts & Homework Tutor

    Satisfied Customers:

    3121
    Post Graduate Diploma in Management (MBA)
  • http://ww2.justanswer.com/uploads/ComputersGuru/2010-02-13_051118_Photo41.JPG LogicPro's Avatar

    LogicPro

    Engineer

    Satisfied Customers:

    3035
    Expert in Java C++ C C# VB Javascript Design SQL HTML
  • http://ww2.justanswer.com/uploads/lanis/2009-4-1_233717_phput9xef_c1pm.jpg Lani S.'s Avatar

    Lani S.

    Tutor

    Satisfied Customers:

    2457
    Registered Nurse, Internet Researcher, Private Tutor
  • http://ww2.justanswer.com/uploads/chooser77/2009-08-18_162025_Chris.jpg Chris M.'s Avatar

    Chris M.

    M.S.W. Social Work

    Satisfied Customers:

    2341
    Master's Degree, strong math and writing skills, experience in one-on-one tutoring (college English)
  • http://ww2.justanswer.com/uploads/JawaadAhmed/2009-6-27_12137_SIs_SHadi.jpg F. Naz's Avatar

    F. Naz

    Chartered Accountant

    Satisfied Customers:

    1975
    Experience with chartered accountancy
  • http://ww2.justanswer.com/uploads/JK/jkcpa/2011-1-16_182614_jkcpa.64x64.jpg Bizhelp's Avatar

    Bizhelp

    CPA

    Satisfied Customers:

    1873
    Bachelors Degree and CPA with Accounting work experience
 
 
 

Related Homework Questions