Enable contrast version

Tutor profile: Sachal M.

Inactive
Sachal M.
Software Engineer, Computer Science Tutor
Tutor Satisfaction Guarantee

Questions

Subject: Python Programming

TutorMe
Question:

I'm starting to learn Python after years of working with a strictly typed programming language. I'm a bit confused about how Python type inference works. Let's say I set a variable to a string value. Can I call an arithmetic operation on the variable in the following line?

Inactive
Sachal M.
Answer:

Python is an interpreted programming language, which means that it executes it's instructions directly rather than compiling first. In other word's, it figures out what the type of the variable is at run time, and then will throw an exception if one is raised. So the short of it is, you could write that code. Python would run it. If you're doing something like "hello"/3 then it will show you a message like "unsupported operand type(s)". However, it will not prevent you from beginning instruction execution. Many programmers love this about Python, but for people who are used to statically-typed languages this can feel like losing an important safe guard.

Subject: Java Programming

TutorMe
Question:

In my code I have a method that compares if the user's password is equal to the String in the parameters but it is always returning false. Can you tell me why? I've pasted the code below. public boolean checkPassword(String password) { return this.pw == password; }

Inactive
Sachal M.
Answer:

The important thing we have to pay attention to here is the data type of password. It's a String, which is not a primitive data type. In Java the double equals checks if the value on each side is the same. If you're working with primitives that's great. The value 5 will always be represented by 00000101 no matter what variable it is stored in. However, when you are working with class types like the String class, the value stored in that variable is a memory address pointing to the location in memory where the data is actually stored. So when you use double equals on the String, you're not checking to see if they correspond to the same sequence of char's but rather if they correspond to the same location in memory. String a = "hello"; String b = "hello"; String c = a; So in this example, a == c would evaluate to true since they both correspond to the same memory address, however a == b would evaluate to false. You will learn more about this when you take Computer Organization but for now just remember to use .equals when comparing String's. a.equals(b) would evaluate to true.

Subject: Computer Science (General)

TutorMe
Question:

In Object Oriented Programming, what's the difference between Interfaces and Abstract classes? Both seem to contain essentially just unimplemented methods, so when would you use one or the other?

Inactive
Sachal M.
Answer:

That's a great question. Interfaces are useful when you simply want the functionality to define the group. For example, let's say you want to create a group of objects that are Playable. The only requirement for something that is Playable, is that it have play(),pause(), and stop() methods. If you were to initialize an array of type Playable, you would know that you can call those three methods on any non null element in the array. An important thing to note for Interfaces, is that you can implement multiple interfaces. One object can be defined by many functionality group's. In other words, you can have a Phone that is also an IPod and a Navigator. You should use Abstract classes when you want to define a type with certain behaviors and attributes, and you know what the behaviors are, but not how all of them work. Imagine you're writing an Animal class. You know all Amimal's have certain attributes (instance variables) such as age, weight, eye color. And they all can talk(). But you don't know how all of them talk(). For example, a Cow goes "Moo", the Duck goes "Quack", and the Dog goes "Bark". So you would make talk() an abstract (unimplemented) method and therefore Animal an Abstract class. That way, I know that any object of type Animal that can be initialized has the talk() method implemented and I can call it (since Abstract classes cannot be initialized). This is especially useful for single-inheritance paradigms. --------------------- Usually my explanations would be accompanied by sample code but I didn't see a place for it here.

Contact tutor

Send a message explaining your
needs and Sachal will reply soon.
Contact Sachal

Request lesson

Ready now? Request a lesson.
Start Lesson

FAQs

What is a lesson?
A lesson is virtual lesson space on our platform where you and a tutor can communicate. You'll have the option to communicate using video/audio as well as text chat. You can also upload documents, edit papers in real time and use our cutting-edge virtual whiteboard.
How do I begin a lesson?
If the tutor is currently online, you can click the "Start Lesson" button above. If they are offline, you can always send them a message to schedule a lesson.
Who are TutorMe tutors?
Many of our tutors are current college students or recent graduates of top-tier universities like MIT, Harvard and USC. TutorMe has thousands of top-quality tutors available to work with you.
BEST IN CLASS SINCE 2015
TutorMe homepage
Made in California by Zovio
© 2013 - 2021 TutorMe, LLC
High Contrast Mode
On
Off