I'm doing an assignment for my class where I need to create the functions below for a Path. I'm having trouble on the functions, am I overthinking it? - object

import java.awt.Point;
import java.util.ArrayList;
import java.util.Scanner;
public class Path
{
ArrayList<Point> pointOne;
ArrayList<Point> pointTwo;
public Path() {
pointOne = new ArrayList<Point>();
pointTwo = new ArrayList<Point>();
}
public Path(Scanner s)
{
pointOne = new ArrayList<Point>();
pointTwo = new ArrayList<Point>();
}
public int getPointCount()
{
return 0;
}
public int getX(int n)
{
return n;
}
public int getY(int n)
{
return n;
}
public void add(int x, int y)
{
}
public String toString()
{
}
So far this is what I have for the getX and getY function, please feel free to correct me if im not doing something right.
I've tried to research some different ways to add two points to an arraylist. I found one here but it didn't help as much as I thought it was going to. I am also confused on how to use the scanner to scan in the points and build a path. Am I just being dumb and overthinking this? I'm going to talk to my teacher to see if he can clear anything up but any help would be much appreciated thanks

public class Main {
public static void main {
ArrayList<Point> path = new ArrayList<Point>();
path.add(new Point(x1,y1));
path.add(new Point(x2,y2));
}
}
public class Point {
private double X;
private double Y;
public Point(double x, double y){
X = x;
Y = y;
}
public double getX(){return X;}
public double getY(){return Y;}
}
You see, a path is a list of points, conceptually, specifically an ArrayList of Points here. Point is a type of object with attributes x and y. You don't need to create an type called Path because a Path is just an ArrayList<Point>. You can't extend ArrayLists, probably you don't know yet about how inheritance anyways. But unless you are told specifically to do so, there's no need to make Path a class.
Furthermore, Point could be removed as a type and just made as an ArrayList<Double>. Conceptually, a point is just a list of its numeric coordinates in each direction.
Therefore, a Path could be represented as an ArrayList<ArrayList<Double>>.

Related

"Int should be Void -> Int" when comparing two integers

So this is a new one for me. When I try to compare 2 integers, the error tells me that Int should be Void -> Int, which is something I have never even seen before.
The code:
public static function whenTouchEnds(event:TouchEvent){
for (item in currentTouches){
if (item.getId == event.touchPointID){
currentTouches.remove(item);
trace("removed touch");
break;
}
}
}
Following the Haxe documentation, I also tried:
public static function whenTouchEnds(event:TouchEvent){
for (item in currentTouches){
if (item.getId == event.touchPointID) break;
}
}
And for the sake of trail and error (hobby programmer here) even tried:
public static function whenTouchEnds(event:TouchEvent){
for (item in currentTouches){
var itemID:Int = item.getId;
var touchID:Int = event.touchPointID;
if (itemID == touchID){
currentTouches.remove(item);
trace("removed touch");
break;
}
}
}
They all gave me the same error message "Int should be Void -> Int". Here is the Touch class I created which returns an Integer with the getId function:
class Touch
{
public var id:Int = 0;
public var xPos:Int = 0;
public var yPos:Int = 0;
public function new(Id:Int, X:Int, Y:Int)
{
id = Id;
xPos = X;
yPos = Y;
}
public function getX() : Int
{
return (xPos);
}
public function getY() : Int
{
return (yPos);
}
public function getId() : Int
{
return (id);
}
}
I'm not looking for a simple solution, but rather an explanation of what I am missing here. The more I learn, the better!
Cheers
The culprit is this line:
if (item.getId == event.touchPointID)
Since there's no parentheses, you're not actually calling the getId() function here - you're comparing it to an integer (which doesn't make sense). Try this instead:
if (item.getId() == event.touchPointID)
Void -> Int is Haxe's notation for a function type, specifically a function that takes no parameters (Void) and returns an integer. You're comparing such a function to an Int, hence the error message "Int should be Void -> Int".
A small code style critique: the get* functions in your Touch class don't really seem to serve any purpose, the variables are public anyway. If you ever want to do something more complex than just returning the variable in a getter function, you might want to look into using properties instead.

Change struct values from a different class

I am trying to change a struct values (located in class A) from another class (class B per say) I wrote a method to get the struct (the method is located in class A) but all I get is a shallow copy (the values do not really change...) any help?
Yes, that's what happens with structs. You need to make the changes locally, then shallow copy back again. For example:
public class Foo
{
public Point Location { get; set; }
}
public class Bar
{
private Foo foo = new Foo();
public void MoveFoo()
{
Point location = foo.Location;
location.X += 10;
location.Y += 20;
// Copy it back
foo.Location = location;
}
}
Personally I try to avoid making structs mutable in the first place - but will often given them what I call "pseudo-mutator" methods which return a new value with appropriate changes. So for example, for a Point struct I might have a method like this:
public Point TranslatedBy(int dx, int dy)
{
return new Point(x + dx, y + dy);
}
Then the MoveFoo method above would be:
foo.Location = foo.Location.TranslatedBy(10, 20);

C#: is it possible to create an object that has a value of its "default property" when referenced?

Is it possible to create an object with a constructor parameter which returns a property value when referenced, without using dot notation? Here's a few examples:
public class myObject
{
public string myObject {get; private set;}
public myObject( string tempstring)
{
this.myObject = tempstring.ToUpper();
}
}
var a = new myObject("somevalue");
Console.WriteLine( myObject ); // outputs the string "SOMEVALUE"
Here's another attempt:
public class myInt
{
public int myInt {get; private set;}
public myInt(string tempInt)
{ this.myInt = Convert.ToInt32(tempInt);
}
}
var a = new myInt("3");
var b = a + a; // ends up being an int datatype value of 6
I know I could always do var b = a.myInt + a.myInt. I guess I could create a static class with a static function that converts a parameter each time to a result, but it wouldn't maintain state.
Just curious. It would make what I am actually trying to do much less difficult.
In the first case, yes. Override the ToString method.
public class myObject
{
public string myValue {get; private set;}
public myObject( string tempstring)
{
this.myValue = tempstring.ToUpper();
}
public override string ToString()
{
return myValue;
}
}
In the second case, sort of. You shouldn't try to overload operators to offer unexpected behavior. Create a method to perform behavior that wouldn't make sense when reading the code. What you are suggesting (returning an int) would definitely not be expected by me to return an int (mostly because of the var rather than a strictly defined type). Using the + operator to return a new myInt object would make sense. Using the + operator return an int would not.
You could overload the + operator to return a new myInt object, and then also add an implicit cast to int. Just make sure it makes sense, and that it is readable.
Within the class, you could use:
public static implicit operator int(myInt m)
{
return myValue;
}
public static myInt operator +(myInt left, myInt right)
{
// requires constructor that takes int
return new myInt(left.myValue + right.myValue);
}
Of course, you could go the direct route, but again only use it when it makes it more readable and not less (note, just like methods operators cannot be overloaded simply by return type, so you'd have to pick between the two).
public static int operator +(myInt left, myInt right)
{
return left.myValue + right.myValue;
}
How about implicit conversions. See http://msdn.microsoft.com/en-us/library/z5z9kes2(VS.71).aspx

Can extension methods modify extended class values?

I was just trying to code the following extension method:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _4Testing
{
static class ExtensionMethods
{
public static void AssignMe(this int me, int value)
{
me = value;
}
}
}
But it is not working, i mean, can I use an extension method to alter values from extended classes? I don't want to change void return type to int, just changing extended class value. Thanks in advance
Your example uses int, which is a value type. Classes are reference types and behaves a bit differently in this case.
While you could make a method that takes another reference like AssignMe(this MyClass me, MyClass other), the method would work on a copy of the reference, so if you assign other to me it would only affect the local copy of the reference.
Also, keep in mind that extension methods are just static methods in disguise. I.e. they can only access public members of the extended types.
public sealed class Foo {
public int PublicValue;
private int PrivateValue;
}
public static class FooExtensions {
public static void Bar(this Foo f) {
f.PublicValue = 42;
// Doesn't compile as the extension method doesn't have access to Foo's internals
f.PrivateValue = 42;
}
}
// a work around for extension to a wrapping reference type is following ....
using System;
static class Program
{
static void Main(string[] args)
{
var me = new Integer { value = 5 };
int y = 2;
me.AssignMe(y);
Console.WriteLine(me); // prints 2
Console.ReadLine();
}
public static void AssignMe(this Integer me, int value)
{
me.value = value;
}
}
class Integer
{
public int value { get; set; }
public Integer()
{
value = 0;
}
public override string ToString()
{
return value.ToString();
}
}
Ramon what you really need is a ref modifier on the first (i.e. int me ) parameter of the extension method, but C# does not allow ref modifier on parameters having 'this' modifiers.
[Update]
No workaround should be possible for your particular case of an extension method for a value type. Here is the "reductio ad absurdum" that you are asking for if you are allowed to do what you want to do; consider the C# statement:
5.AssignMe(10);
... now what on earth do you think its suppose to do ? Are you trying to assign 10 to 5 ??
Operator overloading cannot help you either.
This is an old post but I ran into a similar problem trying to implement an extender for the String class.
My original code was this:
public static void Revert(this string s)
{
char[] xc = s.ToCharArray();
s = new string(xc.Reverse());
}
By using the new keyword I am creating a new object and since s is not passed by reference it will not be modified.
I changed it to the following which provides a solution to Ramon's problem:
public static string Reverse(this string s)
{
char[] xc = s.ToCharArray();
Array.Reverse(xc);
return new string(xc);
}
In which case the calling code will be:
s = s.Reverse();
To manipulate integers you can do something like:
public static int Increment(this int i)
{
return i++;
}
i = i.Increment();

Where do you edit the constructor template on resharper 4.1?

When I create a constructor with parameters using Resharper's 'Generate code' feature, I get something like this:
public class Example{
private int _x;
private int _y;
public Example(int _x, int _y){
this._x = _x;
this._y = _y;
}
}
I would like to use this naming convention instead:
public class Example{
private int _x;
private int _y;
public Example(int x, int y){
_x = x;
_y = y;
}
}
but I don't know if it's possible to edit this constructor template and where.
Options / Languages / Common / Naming Style
You should set your field prefix to underscore.
Go to Resharper -> Live Templates -> C# -> find the class and the ctor templates and right click on them to make the desired adjustments ...
This may help you as well Live Templates help
That is the sample I just created:
internal class MyClass
{
private string myField;
public MyClass(string myField)
{
this.myField = myField;
}
}

Resources