Magic
A Swift alternative for Objective-C's DLog macro.
Arthur Sabintsev | Lead iOS Developer at The Washington Post | Bio & Portfolio
I have a class of entirely private variables; only certain variables will need some data validation on the way in.
Right now I have:
public class Example {
//**** Variables / Properties ****/
private $one;
private $two;
private $three;
//**** getter **** //
public function __get( $property ) {
if(property_exists($this,$property) {
return $this->$property;
}
}
// **** setter **** //
public function __set($property, $value) {
if(property_exists($this,$property)) {
$this->$property = $value;
}
}
}
What if I want to do data validation on $two but just return $one and $three without any data validation?
I ask this because my class has many more variables than this and I hate writing individual set and get methods for every property of the class when only some of them require specific behavior.
Source: (StackOverflow)
I didn't want to have to ask, but I can not figure out this assignment, and neither could the TA when I asked for help.
I have to take input from a text file, feed the integers in the file into an array list, and test to see if it is a n x n magic square. n is equal to the square root of the array list's length. If it isn't a perfect square it immediately fails the magic square test.
Anyway I have it almost completed; I just don't seem to understand what my professor is telling/asking us to do in the final step of the magic square test.
All the testing before these final four steps works flawlessly. I'll post my current code after the steps.
Let rowSums and colSums be two arrays of length n with entries all zeros. Also, let sumDiagMajor and sumDiagMinor, representing the sum
of the entries along the top-left to bottom-right and top-right to
bottom-left diagonals, respectively, of the table.
Let index = 0
Repeat until index = n2 (a) Increment rowSums[row] by ArrayList{index} (b) increment colSums[col] by ArrayList{index} (c)
If row = col, then increment sumDiagMajor by ArrayList{index}. (d) If
row + col = n−1, then increment sumDiagMinor by ArrayList{index} (e)
Increment index by 1
If sumDiagMajor is equal to sumDiagMinor and each entry of rowSums and colSums, the the table is a magic square; otherwise, it is not.
int rowSums[] = new int[_n];
int colSums[] = new int[_n];
int sumDiagMajor = 0;
int sumDiagMinor = 0;
int row, col;
row = col = 0;
for (int index = 0; index < (n*n); index++)
{
rowSums[row] = rowSums[row] + magicSquare.get(index);
colSums[col] = colSums[col] + magicSquare.get(index);
if (row == col)
{
sumDiagMajor = sumDiagMajor + magicSquare.get(index);
}
if ((row + col) == (n - 1))
{
sumDiagMinor = sumDiagMinor + magicSquare.get(index);
}
}
System.out.println(sumDiagMajor);
System.out.println(sumDiagMinor);
My questions include, am I properly incrementing the arrays rowSums, and rowCols? He never actually states what to do with rows or cols, so is initializing them to zero the best option?
If I did everything correct so far, how can sumDiagMajor ever equal sumDiagMinor because rows will always equal cols, so the second nested if statement will never run. Therefore it will rule out everything test as being a magic square?
Sorry for the long post, but this is very confusing.
Source: (StackOverflow)
I am developing an ASP.NET MVC application with SQL Server. I do my best to write good code by using built in elegance and efficiency.
However, in the database I prefer to further naming in underscore_case, like id_item_something
. On the C# code side, everything is CamelCase, like IdItemSomething
.
Are there some magic transformation that could be configured for not having to write a lot of ...ToTable().Property(...).HasColumnName(...)
? Is the DefaultControllerFactory
the right place to look at?
I have seen such transformations in the JavaScript-jQuery-CSS domain ("background-color" is addressed by "backgroundColor"), and in ASP.NET there is at least the PluralizingTableNameConvention
.
Thank you for any hint.
Source: (StackOverflow)
I have an old MSDOS software realized in magic 5.6 with an btrieve 5.10a database, that should be modernized(completely redone using a modern DB).
For this I would love to just get the table structures and some understanding in the structure of the magic program.
But unfortunately I was not able to find any documentation on magic nor was I able to get the structure with column names from the tables(.btr but no ddf files).
Any idea on how to get a step further?
Source: (StackOverflow)
I am looking to integrate MYOB desktop services with Magic XPA 3.0. Can anyone help about this?
Source: (StackOverflow)
i need some help as for some reason this thing goes out of bounds if n is not declared above for example if n=3; then there's no problems but if I rely on user input for the value of n it goes out of bounds.
import java.io.*;
public class MagicSquare_Cubz {
BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
int n;
int [][] array; // = new int [z][z];
int i=0;
int j = n/2;
int k = 1;
public static void main(String args[]) throws IOException{
new MagicSquare_Cubz();
}
public MagicSquare_Cubz() throws IOException {
System.out.print("MAGIC SQUARE\n");
System.out.print("============\n");
System.out.print("Enter a number: ");
n=Integer.parseInt(in.readLine());
//n=o;
array = new int [n][n];
//problem.. for some reason the array goes out of bounds if n is not declared...
disp();
if(n%2!=0){
exe();
disp();
}
else{
System.out.print("Enter odd numbers only");
}
}
public void exe() throws IOException{
System.out.println("IT WORKS " + n);
while(k<=n*n){
array[i][j] = k++;
i--; // Making one step upward
j++; // Moving one step to the right
if(i<0 && j>n-1){ // Condition for the top-right corner element
i = i+2;
j--;
}
if(i<0) // Wrapping around the row if it goes out of boundary
i = n-1;
if(j>n-1) // Wrapping around the column if it goes out of boundary
j = 0;
if(array[i][j]>0){ // Condition when the cell is already filled
i = i+2;
j--;
}
}
}
public void disp() throws IOException{
for(int x=0 ; x<n ; x++){
for(int y=0 ; y<n ; y++){
System.out.print(array[x][y]+ " ");
}
System.out.println("");
}
}
}
Source: (StackOverflow)
I wrote a small abstract class that is called Task. I like to have every task logic's class to extend it.
Within my abstract class "Task" I like to call a used defined method "execute" that is defined in every class.
I tried to use the magic method __call
but it is not working.
If you notice in my method I am echoing a message which never prints on the screen.
Here is my abstract Task class
<?php
namespace App\Modules\Surveys\Tasks;
use App\Modules\Surveys\Tasks\Support\Traits\HtmlHelper;
abstract class Task
{
/*
|
| This task base class provides a central location to place any logic that
| is shared across all of your tasks.
|
*/
use HtmlHelper;
/**
* checks wether a get method execute exists and calls it
*
* @param string $name
* @param array $args optional
* @return mixed
*/
public function __call($name, $args = [])
{
echo 'Attempt to execute task';
if (method_exists($this, 'execute')) {
return call_user_func_array('execute', $args);
} else {
throw new \Exception('execute method does does not exists in your task! ' . get_class($this) );
}
}
}
?>
Here is a logical class
<?php
namespace App\Modules\Surveys\Tasks\Interviews;
use App\Modules\Surveys\Tasks\Task;
use App\Modules\Surveys\Models\SurveyInterview;
use Exception;
class ResumeInterview extends Task
{
protected $surveyId;
protected $callId;
protected $myInterview;
/**
* Create a new task instance.
*
* @return void
*/
public function __construct($surveyId, $callId)
{
$this->surveyId = intval($surveyId);
$this->callId = intval($callId);
}
/**
* Resume existing interview if one exists using the giving $surveyId and $callId
*
* @return void
*/
protected function execute()
{
//find the current interview if one exits
$myInterview = SurveyInterview::surveyAndCall($this->surveyId, $this->callId)->first();
$this->setInterview($myInterview);
if( $this->wasResumed() ){
//At this point existing interview was found
if($myInterview->status != 'Pending'){
//At this point the interview is completed and should not be conducted
throw new Exception('This interview can not not be retaken. It\'s current status is "' . $myInterview->status . '"');
}
}
}
/**
* Return the current interview
*
* @return App\Models\Survey\SurveyInterview
*/
public function getInterview()
{
return $this->myInterview;
}
/**
* It checks whether ot the the interview was resumed
*
* @return boolean
*/
public function wasResumed()
{
return $this->getInterview() ? true : false;
}
/**
* It sets the interview
*
* @param Illuminate\Support\Collection $myInterview
* @param void
*/
protected function setInterview($myInterview)
{
$this->myInterview = $myInterview;
}
}
How would I automatically call the execute method if it exists, otherwise throw an exception?
Source: (StackOverflow)