## psyking's Profile User Rating:

Group:
Authors
Active Posts:
165 (0.1 per day)
Joined:
17-January 10
Profile Views:
9,512
Last Active:
Nov 08 2013 12:17 PM
Currently:
Offline

### Previous Fields

Country:
Who Cares
OS Preference:
Linux
Favorite Browser:
FireFox
Favorite Processor:
Intel
Favorite Gaming Platform:
PC
Ford
Dream Kudos:
150

### Latest Visitors

psyking has not set their status

1. #### In Topic: Magic Square Program

Posted 4 Dec 2012

tmarr80, on 04 December 2012 - 03:22 PM, said:

```int sum_row(int i)
/*   Pre: i is a valid row of the square
Post: Returns the row sum of the ith row  */
{
int sum = 0;
for(int j=0; j<size; j++)
{
sum = sum+grid[j][j];
}
return sum;
}

```

The problem with this code for this method is that it isn't returning the sum for that specific row, it's returning the diagonal going from top left to bottom right of the square. For this one, you would need to have your sum change like this: sum += grid[i][j]; where i is the row that the has been requested and j is the current column.

tmarr80, on 04 December 2012 - 03:22 PM, said:

```int sum_col(int i)
/*   Pre: i is a valid col of the square
Post: Returns the col sum of the ith col  */
{
int sum = 0;
for(int j=0; j<size; j++)
{
sum = sum+grid[j][j];
}
return sum;
}

```

Here, it is the same as above, but instead it should be reversed (i.e. sum += grid[j][i];).

tmarr80, on 04 December 2012 - 03:22 PM, said:

```int sum_other()
/*   Post: Returns the non-main diagonal sum  */
{
int sum = 0;
for(int j=0; j<size; j++)
{
sum = sum+grid[j][j];
}
return sum;
}

```

Here, you don't need to change how the grid works, just how the for loop is initially started. Instead of assigning 0 to j, assign size-1, and instead of checking if j is less than the size, check if it is greater than or equal to 0, and instead of incrementing at the end of each pass, decrement at the end of each pass j--

2. #### In Topic: problem: While loops with multi-conditions

Posted 3 Dec 2012

anwind, on 03 December 2012 - 12:41 AM, said:

Thank you, how stupid of me.

Don't worry about it, everyone has those kinds of days (I've had plenty of them).
3. #### In Topic: problem: While loops with multi-conditions

Posted 3 Dec 2012

anwind, on 03 December 2012 - 12:00 AM, said:

```#include <iostream>
using namespace std;

// Function of reverse
class Odometer
{
public:

void odometer()
{
milesDriven = 0;
Mpg = 0;
Igallons=0;
}

```

Here, your problem is you are not defining your constructor correctly. It's definition has no type (i.e.
```class ExampleClass
{
public:
ExampleClass(){
// do something
}
};

```
) and it's name needs to match exactly the name of the class (case-sensitive). Because it is not correct, your variables do not really get initialized, which could potentially cause a bug in your program.

anwind, on 03 December 2012 - 12:00 AM, said:

```	void fuelleft(double gallonsleft)
{
gallonsleft = Igallons - milesDriven / Mpg;
}

```

This method accepts a double, then sets the local variable 'gallonsleft' to Igallons - milesDriven/Mpg. Later in your code, you pass Gallonsleft to this method, expecting Gallonsleft to be something other than 0 when the method returns. However, this will not be the case, since you are passing the variable by value and not by reference. Passing the variable by reference will change the value stored in the address of the variable, meaning that Gallonsleft will be changed.

anwind, on 03 December 2012 - 12:00 AM, said:

```	void Imiles(double milesD)
{
milesDriven = milesD + milesDriven;
}

void Odometer::set_mpg(double mpg)
{
mpg = Mpg;
}

```

Here is good as well.

anwind, on 03 December 2012 - 12:00 AM, said:

```	void Odometer::set_gallons(double gallons)
{
gallons = Igallons;
}

```

Here, you are trying to assign the number of gallons remaining in the tank. However, the assignment is wrong, because in C/C++ the variable that you are trying to assign a value to must be on the left side of the equals ('=') sign. The variable Igallons will always remain as zero (0) because you are always assigning Igallons to the local variable gallons.

anwind, on 03 December 2012 - 12:00 AM, said:

```
private:
double milesDriven;
double Mpg;
double Igallons;

};

int main()
{
Odometer CarOdometer;
double milesdriven = 0;
double MPG=5;
double Gallons=10;
double Gallonsleft=0;

```

anwind, on 03 December 2012 - 12:00 AM, said:

```	while (( Gallons >= 10 ) && ( Gallons <= 30))
{
cout << "Enter gallons in tank (10-30): " << endl;
cin >> Gallons;
if (Gallons <10 || Gallons > 30)
cout << "Error: "<< endl;
}
CarOdometer.set_gallons(Gallons);

```

You will always get stuck on this while loop while you are entering the correct information. As soon as you enter a number that is less than 10 or greater than 30, the while loop will exit. What you should be checking for in the while loop is if they have entered incorrect numbers, then the while loop should keep going; otherwise, it should exit (i.e. Gallons <10).

anwind, on 03 December 2012 - 12:00 AM, said:

```	while ((MPG >= 5) && (MPG <= 70))
{
cout << "Enter Fuel efficiency (MPG) (5-70): " << endl;
cin >> MPG;
if (MPG <5 || MPG > 70)
cout << "Error: "<< endl;
}
CarOdometer.set_mpg(MPG);

```

You have the same problem here as you did in the while loop checking for the number of gallons entered. As long as the user enters a "correct" number (i.e. between 5 and 70) the loop will always continue.

anwind, on 03 December 2012 - 12:00 AM, said:

```	while (milesdriven = -1)
{
cout << "Enter miles for trip (-1 to finish, no more trips) : " << endl;
cin >> milesdriven;

CarOdometer.Imiles(milesdriven);
}

```

Here, you have a different problem for the while loop, as you have created an infinite loop by doing an assignment operation every time the loop checks if it should continue. What you are trying to do is to check if milesdriven is not equal to -1 (i.e. != -1).

anwind, on 03 December 2012 - 12:00 AM, said:

```
CarOdometer.fuelleft(Gallonsleft);
cout << "Gallons left in the tank: "<< Gallonsleft <<" gallons" << endl;

return 0;
}

```

Here, when you call the method fuelleft, you enter the variable Gallonsleft, and later expect it to have changed from 0 gallons remaining to however many gallons remaining -- hence the line immediately following. However, as I stated earlier, this is only going to happen if you pass the variable by reference (i.e. &Gallonsleft) and if the method accepts a double passed by reference (which it currently does not), meaning that your output for that final line will always be: "Gallons left in the tank: 0 gallons".
4. #### In Topic: Java "Calculator" Program-Unidentified Constructors?

Posted 27 Aug 2012

Regamus, on 26 August 2012 - 11:27 PM, said:

No changes are made to the Expression class, but as for the others:

Number:
```public class Number extends Expression {

double num;

public Number(double d) {
// TODO Auto-generated constructor stub
}

public String toString(){

return String.valueOf(num);

}

public double evaluateMe(){

return num;
}

}

```

Sum:
```public class Sum extends Expression {

Expression leftside;

Expression rightside;

public Sum(Number number, Number number2) {
// TODO Auto-generated constructor stub
}

public Sum(Sum s1, Product product) {
// TODO Auto-generated constructor stub
}

public String toString(){

return "(" + leftside.toString() + ")" + "+" + "(" + rightside.toString()+ ")";

}

public double evaluateMe(){

return leftside.evaluateMe() +rightside.evaluateMe();

}

}

```

Once again, thank you for your help!

Those constructors are not wrong, but they do nothing. The reason that you get the NullPointerException when you have the constructors is because you don't give any value to num in the Number class, or leftside and rightside in the Sum class. In the constructor you need to initialise the variables that are used in the classes. And in terms of the constructor arguments, you can get rid of the multiple constructors and use a single constructor that starts similarly to this: public Sum(Expression left, Expression right) This is acceptable because Sum, Number, Product and your other classes inherit from Expression, so they are all effectively the Expression class as well, albeit children.

In an example class I've written in Java, which I will call ExampleClass, I will have a private String variable, override the default ToString function and create a default constructor that accepts a String as an argument, and I will implement a Comparable. This is all to show what a simple constructor might look like -- hopefully you will be able to configure your classes off of something that you learn from this.
```/**
* An example class meant to demonstrate initialising variables
* inside of a constructor. It implements the Comparable class
* and overrides the default Object ToString method.
*/
public class ExampleClass implements Comparable<ExampleClass>
{
/**
* The private String variable used in this ExampleClass
* to help in demonstrating initialising a variable inside a
* constructor.
*/
private String m_StringVariable;

/**
* Creates and initialises an ExampleClass object.
* @param A String to be stored in this ExampleClass object.
*/
public ExampleClass(String argument)
{
// to prevent a NullPointerException from occuring
// if any other code, like m_StringVariable.compareTo
// or other String function is called, initialise the
// internal variable
this.m_StringVariable = argument;
}

/**
* Overrides the default Object ToString function,
* and returns the string that was provided as an
* argument to the constructor.
* @return Returns the internal String
*/
@override
public String ToString()
{
// if m_StringVariable was initialised with a NULL (or is it null in Java?)
// String, it is still a valid String so there is no need to do any checking
return m_StringVariable;
}

/**
* This function is used to determine a classes natural ordering
* taking another object of ExampleClass type and comparing the
* Strings.  This function will throw a NullPointerException
* if m_StringVariable is NULL (null), if other is NULL (null) or
* if other.ToString returns NULL (null)
* @param an ExampleClass to compare to this one
* @return Returns -1 if this object is less than the other one, 0 if they are equal or 1 if this object is greater
*/
@override
public int compareTo(ExampleClass other)
{
// since String also implements comparable,
// all that has to be done is to return
// m_StringVariable.compareTo(other)
return m_StringVariable.compareTo(other.ToString());
}
}

```

An example use of this class in sample code:
```String hello = "Hello";
String world = "World";

ExampleClass e1 = new ExampleClass(hello);
ExampleClass e2 = new ExampleClass(world);
//ExampleClass e3 = NULL;
//ExampleClass e4 = new ExampleClass(NULL);

System.out.print(e1); // will output "Hello"
System.out.println(e2); // will output "World";
//System.out.println(e3); // causes a compilation error
//System.out.println(e4); // probably causes a compilation error, but definitely a runtime error

System.out.println("e1.compareTo(e2): " + e1.compareTo(e2)); // outputs 1
//System.out.println("e1.compareTo(e3): " + e1.compareTo(e3)); // throws a NullPointerException since e3 is NULL
//System.out.println("e1.compareTo(e4): " + e1.compareTo(e4)); // throws a NullPointerException since e4.ToString is NULL

```

I haven't had a chance to test any of this code, so it probably doesn't work (and it's been a while since I've written anything in Java + no sleep in a few days) but I hope it helps you.
5. #### In Topic: [SOLVED]Window's window does nothing on creation

Posted 26 Aug 2012

snoopy11, on 26 August 2012 - 10:15 PM, said:

psyking, on 27 August 2012 - 02:36 AM, said:

Oh don't worry I will make it +2 as I think he deserves it for spotting
it in the debris of your obfuscated windows code.

Yeah, I agree, he does for finding it in that mess.

snoopy11, on 26 August 2012 - 10:15 PM, said:

psyking you really need to concentrate on how to write prettier code,
after all you might need to update or plain old modify this code one day.

Writing hidden code is all well and good but it does look like it has been
written by someone who had no sleep for two days or even worse...
Putting in a few comments also would not hurt, breaking everything into well defined blocks of code
would not hurt either.

Yeah, I will be going back through it adding comments and making it much prettier after I get a major sleep session in.

### My Information

Member Title:
Age:
22 years old
Birthday:
February 8, 1992
Gender:
Location:
Everywhere there is a computer connected to the internet
Interests:
Running Cross Country and Track, running my own business, programming websites and software, and other things as well.
Full Name:
Null

E-mail:
Private