Questions tagged [oop]

18773 questions
1

votes
1

answer
74

Views

I (think) I'm getting objects returned when I expect my array to have just 2 properties available

Probably the worst way to ask a question, but I'm new and trying my best to explain my problem. I'm implementing a Ruby Blackjack game. You can see the repo / source for what I have here: https://bitbucket.org/subem81/blackjack This is the particular section of concern (kept in the "Hand" module whi...
Mike Waldrup
1

votes
4

answer
4.4k

Views

What is internal state?

What is this "internal state" people talk about all the time precisely? The term really irritates me. The internet couldn't provide me with a definition yet.
user2176127
1

votes
2

answer
142

Views

How do you find the root prototype constructor for a constructor in a prototype chain?

I have a chain of constructors using prototype inheritance in JavaScript like so: var Test = function(){}; var Foo = function(){}; var Bar = function(){}; var Baz = function(){}; Bar.prototype = new Baz(); Foo.prototype = new Bar(); Test.prototype = new Foo(); I want to write a function getRootConst...
Brad Urani
1

votes
5

answer
126

Views

Conflicting methods on interface multiple inheritance

I have the following interfaces (in Java, but it's more of an OO question, not language-specific, I'm interested in answers for any language) public interface A { int foo(); } and public interface B { char foo(); } If I now want to make the following class: public class C implements A,B{ public int...
adam434
1

votes
2

answer
559

Views

understanding single responsibility principal SRP in clean code example

currently i'm reading the clean code Book of Uncle Bob,in the function section when looking to the following example:- public Money calculatePay(Employee e) throws InvalidEmployeeType { switch (e.type) { case COMMISSIONED: return calculateCommissionedPay(e); case HOURLY: return calculateHourlyPay(e)...
Mostafa Darwish
1

votes
3

answer
287

Views

PHP: Check whether object is clone (of any object) or not?

class Object {} $object = new Object(); $clone = clone $object; // no! printf("%d\n", $object == $clone); printf("%d\n", $object === $clone); // no! no no.. printf("%d\n", $object instanceof $clone); printf("%d\n", $clone instanceof $object); // cos, this is true printf("%d\n", $object instanceof $...
K-Gun
1

votes
3

answer
471

Views

PHP OOP, undefined property. How can I get this to work?

Okay so I'm basically trying to improve with PHP OOP, however I'm not too sure how I should do this. Could someone please point out the issue? The code returns the following errors: Notice: Undefined property: registration::$userExist Fatal error: Call to a member function fetch() on a non-object Th...
Exhibitioner
1

votes
2

answer
426

Views

base class pointer, invoke method based on derived type

I understand that the following code doesn't work -- can't convert base to foo. Is there something I can do, or some pattern to employ which would get me close the behavior I'm trying to achieve in the code below? IOW, if I have a base class pointer to a derived type, how can I invoke a specific met...
ash
1

votes
1

answer
67

Views

How do I create completely new objects without any interference with old objects in python?

I am very new to python facing this problem defined below. I am creating several graphs from this class named "Node" class Node(object): label = "" nexts = () prevs = () pos_tag = "" visited = False # black = False (unexplored), white = True (explored) score = 0 My Graph generating function is...
Barbarian
1

votes
3

answer
370

Views

How to replace contents of arraylist with the contents of another arraylist?

I want to replace the contents of one arraylist with the contents of another completely. For instance, ArrayList old = new ArrayList(); ArrayList newlist = new ArrayList(); old.add("Hi"); old.add("World"); newlist.add("League") newlist.add("OfLegends"): old = newlist; When I try that, it does this...
Bob
0

votes
2

answer
36

Views

How can I turn this array into an ArrayList that does the same thing?

I want to make turn this program's array into an ArrayList. So far I know that the array will turn into ArrayList list = new ArrayList(); and that each list[i] will turn into: list.get(i) however I am not sure what the following line will be in order to satisfy the ArrayList version list[i] = new S...
cindy
1

votes
1

answer
912

Views

Java: Is it bad practice to have a circular dependency within the same package? [closed]

Is it bad practice to have a circular dependency between classes that are in the same Java package? If not, I would like a reputable reference to a guideline somewhere that says so.
Danish Khan
1

votes
1

answer
2.2k

Views

Initialization of static const member map<> in class definition

I'm gonna to have a const std::map in my class, and I want it be static to reuse this data structure in other instance. Unfortunately, it won't compile and what I found in cpp primer is: However, we can provide in-class initializers for static members that have const integral type and must do so for...
Frank Wang
1

votes
3

answer
1.8k

Views

How to compare Object Arrays using Arrays.equals() in java?

in this example showing how to do it : Student[] stud1 = new Student[2]; Student[] stud2 = new Student[2]; Student[] stud3 = new Student[2]; boolean b; stud1[0]= new Student("Johnny","Bravo"); stud1[1]= new Student("Ace","Ventura"); stud2[0]= new Student("Ash","Ketchum"); stud2[1]= new Student("Mike...
Fayçal Salhi
1

votes
4

answer
18

Views

Java get superclass method to use subclass instance method when called by subclass

I am attempting to get a superclass method to run on subclass methods/fields when called by the subclass. For instance public class CoffeeMachine() { protected int cost = 3; protected int getCost() { return cost; } } public class FancyCoffeeMachine() extends CoffeeMachine{ protected int cost = 6; }...
aimlesslegs
0

votes
4

answer
34

Views

A function in a class without any decorator or `self`

I have following class with a function: class A: def myfn(): print("In myfn method.") Here, the function does not have self as argument. It also does not have @classmethod or @staticmethod as decorator. However, it works if called with class: A.myfn() Output: In myfn method. But give an error if...
rnso
1

votes
2

answer
31

Views

I need to create a map from an farm object list using the salary from Emploee class

This is the code I wrote so far for the other things I need to do. The main problem is that even with so much info I can't understand how to do it. Create a map from the farm list which has like key the salary and like value the list of employee that has that salary. Display the map content, and fo...
R.Teodor
0

votes
2

answer
22

Views

C# how to make it so that a function that can sort strings or ints without overloading? [duplicate]

This question already has an answer here: creating a generic sort method 2 answers As the title says, I had to write some code to sort either array/list of strings or integers. My OOP knowledge is really rusty since I haven't used C#/Java for a long while. Is there a way to make it so I just need...
l337h4x0r
1

votes
1

answer
15

Views

Python: Calling multiple inits in Multiple inheritance

I have 2 classes: A (which needs 1 argument to initialize) and B (which needs 2 arguments to initialize), and a third class C which derives from both A and B. class A: def __init__(self, sval): print("A: rcd value: ", sval) self.aval = sval class B: def __init__(self, sval, tval): print("B: rcd 2...
rnso
0

votes
0

answer
14

Views

Update excel file and if it doesn't exist create one? JAVA netbeans

I'm trying to gain more knowledge of Java, so I'm working on a project just for fun. Sadly, I have had a problem for the last few days and haven't been able to solve it yet, so I thought of trying out here. PROBLEM* I have this code below, and it works. I'm sure it's probably not the best way of ach...
2

votes
1

answer
29

Views

i have problem to make inherit from other class

I just created three class for inheritance but something went wrong but I try to solve it but not work I want to know what make this Error and how i should solve it I should inherit from other classes the Classes: Property House Rental HouseRental why House Object takes no parameters in Book python...
abdulsalam jamaea
0

votes
0

answer
5

Views

Can two compositions own the same object?

I'm trying to make a class diagram for an Online Auction System and I'm having this problem. Bids belong to both the Auction and the Buyer (Correct me if i'm wrong). So can I say that User is composed of bid AND auction is composed of Bid or is this against the rules of UML? I'm confused
Matthew Fallon
0

votes
2

answer
19

Views

Inject class in other class

I've got a Tag class and a TagCollection class to store multiple tags. Now the TagRepository class needs to construct a new Tag object to return. But when I try to create a new Tag inside the TagRepository it returns an error: Tag is not defined This is how I include all the classes in the main Jav...
Schotsl
-1

votes
0

answer
13

Views

How can I combine Web Development with Object-Oriented-Programming (if this, then that; etc)?

For example, let's say I am building a site where users take a quiz to figure out something about them based on their answers. (For example, a quiz to see which dorm would be suited for students at a certain university). I know the front-end side would be the HTML/CSS/JS that displays the quiz quest...
-1

votes
1

answer
38

Views

The “if” condition in “set” method isn't working

So which floor the lift is on should be able to be read and changed, but only within the allowed range for just that house the lift is installed in. I'm trying to get an "If" condition working looking for a boolean true value from method "validFloor". Based on my very beginner knowledge of Java, I a...
Jan cseng
0

votes
1

answer
16

Views

How do you create a LinkedList, which contains loops, in Python?

I'm trying to come up with a test case for the solution given by hackerrank for detecting cycles in a linkedlist with python. The solution given by hackerrank is: class Node(object): def __init__(self, data = None, next_node = None): self.data = data self.next = next_node def has_cycle(head): fast =...
zcahgg1
1

votes
2

answer
45

Views

Overriding Interface Default Methods

I am having some trouble getting the following code to work: Interface: public interface UOWProcessor { public default Integer countUOW(Object args) { return 1; } } Implementation: public class ListUOWProcessor implements UOWProcessor { private Integer total; @Autowired private UOWProcessor uowProce...
8t12c7081
1

votes
1

answer
99

Views

Analyzing cardinality of types in Java/OOP [closed]

In languages like Haskell, Purescript and Elm it can be powerful to think of types as sets, described here. This tool helps you pick which data structure best fits for your problem. It also allows you to analyze how many impossible states there are. Is it possible to take this idea and transfer it t...
Marc
1

votes
2

answer
34

Views

Rewrite word count function as OOP in python 3

I have written a simple function for counting the occurrences of words in a sentence or phrase. def count(phrase): phrase = phrase.lower() lst = phrase.split() dct = dict() for word in lst: if word not in dct: dct[word] = 1 else: dct[word] += 1 return dct Note that right now I'm not concerned w...
Jabernet
1

votes
3

answer
50

Views

How do I store an object in a dynamically generated variable?

I have a text file with a series of clients. Each line has a different client. Each client has an ID, a username, and a password. I want to create a "Client" class, and generate objects in that class in a loop. Each object would have a username and a password, and would be stored in a variable that...
1

votes
3

answer
42

Views

What will happen if the initialization method's parameter does not get claimed in the class?

Normally a Java class will be: class A { String name; public A(String name_parameter) { this.name = name_parameter; } public static void main(String[] args) { A aaa = new A("ok"); System.out.println(aaa.name); // >>> ok } } Where I first claim a String name in the class, then when initialize the in...
1

votes
6

answer
450

Views

How can I simply inherit methods from an existing instance?

Below I have a very simple example of what I'm trying to do. I want to be able to use HTMLDecorator with any other class. Ignore the fact it's called decorator, it's just a name. import cgi class ClassX(object): pass # ... with own __repr__ class ClassY(object): pass # ... with own __repr__ inst_x...
Harley Holcombe
1

votes
2

answer
42

Views

How to use only certain class object values in java?

I'm still learning java and finding it pretty difficult and I have been quite stuck with this one for a while. Say you have a class which has a constructor a bit like this: public Fruit(String Name, String Type, double Price, int Stock) { this.Name = Name; this.Type = Type; this.Price = Price; this...
user9686775
0

votes
0

answer
10

Views

memcached not storing array of complex items with specific keys

Somehow I can't store an array of complex items if i put user_ as prefix in the key my php code $this->skills = [ 'defensiv' => DefensivSkill::getAllUserSkills($this), 'offensiv' => OffensivSkill::getAllUserSkills($this), 'spezial' => SpezialSkill::getAllUserSkills($this), 'pve' => PVESkill::g...
Kapsonfire
1

votes
1

answer
32

Views

How to add items in a dynamic array

I need make a function that return dynamic[] This works fine for me public static dynamic[] GetDonutSeries(this Polls p) { return new dynamic[] { new {category = "Football",value = 35}, new {category = "Basketball",value = 25}, new {category = "Volleyball",value = 20}, new {category = "Rugby",valu...
Turi
1

votes
1

answer
283

Views

Use final on traits in PHP

What i want is the ability to make "final traits" with the behaviour as described below. I realise this is not possible with traits(or is it? that would make me so happy), but I'm just trying to convey what I want to do. So, i want to have a trait that is trait Content { public final function getPo...
SomeNorwegianGuy
-1

votes
0

answer
33

Views

Adding static implementations to interfaces [on hold]

On my current team, existing code has started polluting interfaces with static methods. I'm trying to stop this practice, and I can give lots of good arguments, but am having a hard time finding reputable articles that support my claim. Does anyone know of any? for example class IFoo { // Good virtu...
swinefeaster
1

votes
1

answer
45

Views

using functions inside constructors php

In an effort to keep my code clean, I am attempting to replace a whole bunch of code in my constructor with a function. I believe I am calling the function correctly but i'm not able to assign values to the variables as intended. public function __construct($docID) { self::getDocumentInfo($docID)...
the1gofer
1

votes
2

answer
73

Views

Differences between generic methods in java

Suppose I have this interface: public interface Lookup { public T find(String name); } Are there differences between the following methods? 1) public void process(String name, Lookup table) { System.out.println(table.find(name)); } 2) public void process(String name, T table) { System.out.println(...
Jordan
1

votes
2

answer
81

Views

How do I create a Python class for these DNS database methods?

I'm trying to write some code to zone transfer a DNS zone, then parse the records and put them into a database. I've never used classes before in python, and I figured this could be as good of a time as any to learn how to use them. Not sure what I'm doing wrong here. This is running on Python versi...
dobbs

View additional questions