Questions tagged [methods]

10227 questions
1

votes
1

answer
333

Views

Inheritance with different parameter lists

I have a base class for a checkers game, and I want to subclass that to implement different tree search algorithms, minmax, alphabeta and so on. So I have something like public class Checkers { .... public void findBestMove(Board b, int depth) { for(mv:b.possibleMoves()) { ... score = evalNode(b.dom...
jackthehipster
1

votes
2

answer
322

Views

Develop a program to analyze the directory structure of a Linux disk and identify any files larger that 500kbytes

I'm trying to develop a program that will analyze the directory structure of a Linux disk and identify any files larger that 500kbytes. #!/usr/bin/perl use File::Find::Rule; use warnings; my $filelist; sub buildFile { open ($filelist, ">", "filelist.txt") || die $!; # File find rule and # Provide sp...
C.Younes
1

votes
2

answer
15

Views

JavaScript Destructive Map Function

I'm trying to make this map function a more destructive version of itself. Meaning the map function modifies the original array instead of a new one. function map (array, callback) { var result = [] for (var i = 0; i < array.length; i++) { result.push(callback(array[i])) } console.log("Array",array,...
Marc Wilk
1

votes
5

answer
10.2k

Views

Finding prime numbers with a custom IsPrime method

I started learning Java about one month ago and today I saw this question I couldn't solve. The question was: Write a method named isPrime, which takes an integer as an argument and returns true if the argument is a prime number, or false otherwise. Demonstrate the method in a complete program. And...
Taix
1

votes
1

answer
1k

Views

Cast a variable as a function/method pointer in Dart? (Delegate?)

Say I have a Map type that stores functions/methods inside of it, like so: Map triggerHandler = { 'x' : (t) => hello(t) }; Now, what I want to do next is to declare that the Type of these Map values are to be functions. I can always do this: Map triggerHandler = { 'x' : (t) => hello(t) }; But this...
willsquire
1

votes
2

answer
74

Views

Returning the last element in a array always

I am very new to javascript and I'm just doing little problems that I can find on the web. My question to you guys is their an array method or something that returns the last element in the array. No matter if new elements gets added to the array or not? I know if we had an array like var array = [...
Billy
1

votes
4

answer
82

Views

Random() method keeps changing final String in Java

I'm trying to create a rock paper scissors game and teach myself more about subroutines in Java. To check whether the logic behind it is right, I wrote in two parts in the program where the current hand position of the opposing player is checked. However, it's different every time, even though I as...
Snupas
1

votes
1

answer
36

Views

What is it called? is it a Java method?

I am completely new at programming and of course I pick Java as my first language. Somethings are just curiosities to me. The main thing that I am getting hung up on is; how do I know what to use if I don't know it exists? or when I do find out something exists, what is the main name of that type so...
kevin simms
1

votes
3

answer
982

Views

How do I call a method from another class component in React.js

So I have to class components: Class1: has a clickbutton Class2: has a method calling my api Basically, what I want is to call a method that sets and edits states inside one class from another class. But I keep failing. Example: Class1.js export class Class1 extends Component { render() { return ( )...
Cédric Bloem
0

votes
0

answer
25

Views

Call C++ class method stored in a DLL

When calling a class method that is loaded from a dll the 'this' pointer changes in my application when I call that function. In my example, the error happens at the line "if (Func2) return (*Func2)( s );" In the debugger the program finds Func2 in the DLL and I can step through Func2, but it's...
DuxburyDutch
1

votes
2

answer
37

Views

How to avoid accessing same index when most values of array are same? Take a look

I am trying to solve a problem related to javascript algorithms. Here's the problem. I want to access the index of an array value by indexOf() method. But I want to know how can I avoid the same index when most values in the array are the same. Take example: let arr1 = [0, 0, 0, 0, 1, 1]; Now as you...
Chetan kumar
0

votes
0

answer
20

Views

Redefine Method In Class C++

I've been trying to create buttons for my new game in c++, and I have created a class for them holding data. I need to be able to allocate a method to each button click class. For example, let's say I have ten buttons, I need each one to output a different word when it is clicked. class button{ floa...
ProgrammerAdam
0

votes
1

answer
12

Views

Problem with the Property-Setter decorator: “private” attribute is not being setted

I am having serious dificulties finding out why my "Setter Decorator" from my custom Class (Data_Setter_Class) is not working properly. I found out that my "Private" Attribute (self.__Data) is not being properly setted by my Decorated Method (self.Data). Therefore, once I instanciate my "Data_Sett...
1

votes
1

answer
140

Views

Angular 7 Add an extension method to primitives

I would like to add few methods to primitives. I have the following file: string-extension.ts: interface String { isNullOrEmpty(this: string): boolean; } String.prototype.isNullOrEmpty = function (this: string): boolean { return !this; }; I have a component which has the following code: constructor...
Ori Refael
1

votes
2

answer
49

Views

How to rename return Tuples Items?

I am new to C# new feature Called as Tuples. I wrote some code on Tuple. when the Tuple is return by a Method (Tuple contains multiples values). i-e string firstNumber = separateNumbers.Item1; string secondNumber = separateNumbers.Item2; string thirdNumber = separateNumbers.Item3; string fourthNumb...
Rehan Shah
1

votes
2

answer
30

Views

best practice to disable a class instanciating

I want to create a utils-class that would contain only static members, e.g. class A(object): X = 0 @staticmethod def f(): print X I also want all of it methods to be used without an instance, i.e. I want the user to call A.f() rather than a = A(); a.f(). One way is using @abc.abstractmethod, e.g. cl...
CIsForCookies
1

votes
4

answer
75

Views

Generating an array in java from percentage

I have an issue about coding logic here. I have to generate an array of Boolean, based on percentage. To clarify, I got a percentage 'X' (int value) and I want to generate a array of Boolean which is composed ox X percents of 1, randomly distributed. Moreover, the length of the array is constant. Fo...
Matthieu Chantepie
1

votes
3

answer
34

Views

How to choose between class methods and properties in react?

Consider the following code in react + ES6: import React, { Component } from 'react'; const myFunction=(x,y)=>{ return x*y }; class MyTest extends Component { // state comes here myProperty=(x,y)=>{ return x*y }; myMethod(x,y){ return x*y } render () { return ( Result of myMethod: {this.myMethod(2,...
Francis
1

votes
0

answer
10

Views

Choosing one of several methods on object instantiation

I have a python class, let's call it MyClass, with several bound methods. I need the behaviour of one of these methods, call it .dynamic_method(), to be drastically different depending on the value of a parameter set during object creation. There will be a handful of variations of dynamic_method whi...
jelcOfDice
1

votes
2

answer
463

Views

Method chaining without parentheses, how to?

I would like to do a method chain, for example like this: Car myCar = new Car(); // Chaining myCar.config.engine.cylinders("4"); But how do I do the chaining, without using parentheses in "config" or "engine"? I can only figure out to do it like this: myCar.config().engine().cylinders("4");
1

votes
1

answer
1.2k

Views

Call model method from different view

I have a model method def full_name_find(user_id) user = User.find(user_id) "#{user.first_name} #{user.last_name}" end that is in the User model. I would like to call it from another view (tickets#show) and I can't seem to get it to work. I tried: where comment has a user_id. I get undefined method...
Cameron Aziz
0

votes
3

answer
17

Views

Passing multiple/unknown number of variable(s) into JavasScript a parameter?

I'm creating a reusable function with multiple/unknown number of variable(s) and one callback. //the number of variable will be dynamic var a = "this is a."; var b = "this is b."; function callback(){ console.log('callback successful'); } //totalUp() will be reusable function function totalUp(allVar...
Vincent1989
0

votes
0

answer
6

Views

Ruby factory method returns a NoMethodError when it's called. What happened?

I'm trying to integrate a factory method into this imperial-to-metric converter. I can make it work independently, but when it's iterated through a hash of ingredients it brings up a NoMethodError. What did I do wrong? I've run independent instance variables through the factory without issue: @item...
Benjamin
1

votes
5

answer
481

Views

Access a class object from a method outside the main

Sorry if the title isn't as accurate as it should be or maybe even falesly translated on my part. I'm german and a beginner in coding so take it easy one me. So I coded a programm in JAVA. It is a programm for a bank account that I picked up in a book and tried to expand. Now, to keep it simple in t...
DetroitRead
4

votes
2

answer
64

Views

Ruby method with parameters in double parenthesis

I have come across the following code in erb library. Note the double (( )): class MyTest def location=((filename, lineno)) @filename = filename @lineno = lineno if lineno end end The following locatia= method is another version without the (( )) for testing: class MyTest def locatia=(filename,...
Peter Camilleri
-1

votes
0

answer
18

Views

How to divide variables in a set method in java

I am setting up a playerclass to describe a player. I need to calculate the bmi however when I use my current code I end up with a bmi of 0.0 as if the formula is running before the variables are set. public class playerclass { String firstname; String lastname int weight; double height; double bmi;...
TBink
1

votes
2

answer
866

Views

TypeScript method type signature redundant

Maybe I'm seriously missing something, but I'm unable to get rid of a syntax problem with all my classes. Here is an example : class Foo { bar: (x: string, y: number) => string = (xx: string, yy: number) : string => { // do some stuff... }; } Since I'm enforcing type declarations using tslint, ALL m...
M'λ'
1

votes
3

answer
2.1k

Views

C# Call a method from a different form class?

Ok, so I have a method in one class and I'm trying to call it from another class. Form1: public void ChangeBack(Color clr) { this.BackColor = clr; } Form2: public void ChangBackColor_Click(object sender, EventArgs e) { if (ColorDialog.ShowDialog() == DialogResult.OK) { Form1.ChangeBack(ColorDialog.C...
Xephyr
-1

votes
2

answer
35

Views

Python module to remove bad or curse words from the line and block

I am working one Python module which should take a line of text and remove if there are any bad words or curse word. it should block the bad word too. Can anyone help me to fix?
deepak v
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
1

answer
286

Views

Terminology: A user-defined function object attribute?

According to Python 2.7.12 documentation, User-defined methods: User-defined method objects may be created when getting an attribute of a class (perhaps via an instance of that class), if that attribute is a user-defined function object, an unbound user-defined method object, or a class method objec...
nalzok
1

votes
1

answer
46

Views

How can I organise this Go code that redefines methods from an embedded type to be less redundant and more maintainable?

I have some example code in which I declare a type foo with some methods which call each other (say: foo.get, which is called by foo.double and foo.toString). I have another type, bar, which embeds foo and redefines get. I am forced to redefine double and toString on bar, so they can see bar.get (r...
cpcallen
1

votes
3

answer
2.1k

Views

Testing Unexported Functions in Go

I have a file called example.go and another test file called example_test.go and they are both in the same package. I would like to test some unexported functions in example.go When I run the test, the unexported functions are undefined in example_test.go. I am wondering what is the best convention...
Jeffrey Yong
1

votes
1

answer
1.6k

Views

Swiper - methods won't work - mySwiper.slideTo is not a function

im using the swiper plugin, but can't seem to get any methods to work. my jQuery and swiper are added like this: at the bottom of my html. my swiper is initialized like this in document ready: mySwiper = new Swiper('.swiper-container', { direction: 'horizontal', nextButton: '.swiper-button-next', pr...
willi jeschar
0

votes
1

answer
19

Views

Make a specific grouping method in class

I have an object whose structure is the following : class Repere { public string Name { get; set; } public List Operations { get; set; } public int Quantite {get;set;} ... } When I want to group a List by its name, I always use the following grouping method : List liste_rep_group = liste_rep.GroupB...
Siegfried.V
1

votes
2

answer
56

Views

Use `self` as a default parameter on static/class methods

Is there a way to set self as a default parameter on static methods? For example enum Foo { static func processSomething(forOwner owner: Any = self) -> Bar { ... } // or static func processSomething(forOwner owner: Any = Caller.self) -> Bar { ... } } I found this SO post but the question is about i...
Zonily Jame
1

votes
1

answer
14

Views

How to save ArrayList state inside method?

I have an Arraylist inside method: Activity{ public boolean onOptionsItemSelected(MenuItem item) { ArrayList mDrinkList = new ArrayList(); mDrinkList.add (new Drink("water", 100)); } } Question is: how to save items that was added to mDrinkList during previous usages of method? This method creates n...
Dzianis
1

votes
1

answer
41

Views

How to run method with JProgressBar

I have a function called CreateAccount. I need it to run and also need to show a progress bar. When I click the button, the method will start. And I need to start showing loading progress bar. Once method is done progress bar also should stop at 100. If the method gets more time to do the job, prog...
1

votes
2

answer
66

Views

Manifold and Structural

I am not sure this is the correct place to ask this but I am experimenting little bit with manifold project and I am not sure that the scenario that I try to achieve is doable with manifold or not? I have a class like following (which will be created from a third party software that I have no contro...
posthumecaver
1

votes
2

answer
47

Views

Class variable scope for static vs class methods

I discovered a weird behaviour (at least weird for me) on python class variables. class Base(object): _var = 0 @classmethod def inc_class(cls): cls._var += 1 @staticmethod def inc_static(): Base._var += 1 class A(Base): pass class B(Base): pass a = A() b = B() a.inc_class() b.inc_class() a.inc_stati...
cabbi

View additional questions