TECH Anyone good with C#?

RetroEvolute

404 Not Found
OT Supporter
Mar 25, 2008
14,857
Manhattan, K.S.
I'm in a programming class and I'm down to the final project, but am realizing that I forgot how to do a lot of stuff.. :wtc:

Thanks.

I've got almost 2 files done.. :dunno:

I'll post the instructions if I get a positive response. :)
 
TS
TS
RetroEvolute

RetroEvolute

404 Not Found
OT Supporter
Mar 25, 2008
14,857
Manhattan, K.S.
Was there an actual question here?

Here's the info and what I've got. I'm just really mixed up cause there's so much to think about.

Instructions said:
You are to write a program that maintains a list of people. The list will be implemented as a resizable array. You should be able to handle insertions and removals into the list, printing the list, and getting the size of the list. You should also allow the user to specify (by means of a command-line argument) whether to keep the list sorted or unsorted. After running, the program will loop, asking the user to repeatedly enter the option of inserting, removing, printing, getting the size, or quitting. Your program should continue this loop until the user selects the quit option. Below is a sample run of the program using the sorted list option:


image.jpg


If your program is invoked with the –unsorted option instead, the information should be stored in the order they are added (not sorted by last name).



Implementation Requirements:
Your program must include the following six classes/interfaces:
·[FONT=&quot]List[/FONT]
·[FONT=&quot]ArrayList[/FONT]
·[FONT=&quot]ArrayListSorted[/FONT]
·[FONT=&quot]Person[/FONT]
·[FONT=&quot]IO[/FONT]
·[FONT=&quot]FinalProject[/FONT]


[FONT=&quot]List[/FONT] is an interface that defines a list of objects. It must define the following methods:

[FONT=&quot]void[/FONT][FONT=&quot] add(object x); [/FONT]
[FONT=&quot]- add x to the list[/FONT]
[FONT=&quot] bool remove(object x); [/FONT]
[FONT=&quot]- remove x from the list (return whether x was in list) [/FONT]
[FONT=&quot]int[/FONT][FONT=&quot] size();[/FONT]
[FONT=&quot] - return the number of elements in the list[/FONT]


[FONT=&quot]ArrayList[/FONT] should implement [FONT=&quot]List[/FONT]. (Remember that if a class implements an interface, it must include implementations of all the methods in the interface.) [FONT=&quot]ArrayList[/FONT] should contain an [FONT=&quot]object[/FONT] array instance variable that will store the list elements, as well as the current size of the array. This class should store the elements in unsorted order. Also, because the list should not have a size limit, you will need to resize the array as the number of elements grows and shrinks.


[FONT=&quot]ArrayListSorted[/FONT] should also implement [FONT=&quot]List[/FONT]. It should be designed similarly to [FONT=&quot]ArrayList[/FONT], but it should store the elements in sorted order.

You may need to see if one object is “less than” another, so you can determine where to place objects to put them in sorted order. To see if [FONT=&quot]object obj1[/FONT] is less than (should come before) [FONT=&quot]object obj2[/FONT], do:

[FONT=&quot]if[/FONT][FONT=&quot] (((IComparable)obj1).CompareTo(obj2) < 0)[/FONT]

(This is actually a bit of a hack, but that’s OK for this class.)


[FONT=&quot]Person[/FONT] should represent a person, including their first name, last name, and age. It should implement the IComparable interface and the CompareTo method, and should override the Equals and ToString methods from object. You will also need to add the following method to Person (just to make it compile):

[FONT=&quot]public[/FONT][FONT=&quot] override int GetHashCode() {[/FONT]
[FONT=&quot] return 0;[/FONT]
[FONT=&quot] }[/FONT]


[FONT=&quot]IO[/FONT] should handle all printing and user input. Make a list of all the different things that need printed, and all the different kinds of user input you want. Make a separate method in [FONT=&quot]IO[/FONT] for each item on your list.


[FONT=&quot]FinalProject[/FONT] is the controller portion of the program that will contain the Main method. It should contain the loop that repeatedly asks the user what they want to do with the array. Remember, [FONT=&quot]FinalProject[/FONT] should call [FONT=&quot]IO[/FONT] to display things or get user input, and it should call [FONT=&quot]List[/FONT] to handle list actions (adding elements, etc.). Remember that when inserting, you should first get the information from the user, then create a new [FONT=&quot]Person[/FONT] object, and then pass that object to [FONT=&quot]List[/FONT] to insert. [FONT=&quot]Final[/FONT] should declare the following variable:

[FONT=&quot]List list = null;[/FONT]

Then, depending on what command-line arguments the user supplies, it will instantiate [FONT=&quot]list[/FONT] to be either a new [FONT=&quot]ArrayList[/FONT] (unsorted) or a new [FONT=&quot]ArrayListSorted[/FONT] (sorted). It should also create an [FONT=&quot]IO[/FONT] object and call its methods for printing and getting input.


Array resizing should be done whenever the number of elements is the same as the current array size. In this case, double the size of the array. You should also resize the array any time the current array size is at least twice as big as the number of elements. In this case, decrease the size of the array by half. The array should start with 10 spots, and should never get smaller than that (even if it is less than half full).

Inserting and removing elements may require you to shift elements in your array using [FONT=&quot]Array.Copy[/FONT]. If inserting into an unsorted list, the correct spot to put a new element is the next available spot in the list. If you are inserting into a sorted list, first find the correct position in the array. Then, shift all elements beyond that position up one spot, and insert the element. When removing (sorted or unsorted), find the correct position of the element. Then, shift all elements beyond that position down one spot to fill in the element you’re removing. Your array should NEVER contain empty spaces between elements.

All command-line arguments to a program are stored in the [FONT=&quot]string[[/FONT][FONT=&quot]] args[/FONT] array (which is a parameter to the main method). For example, if the program is invoked with:

[FONT=&quot]Final -sorted[/FONT]

Then the “-sorted” option is stored in [FONT=&quot]args[/FONT][FONT=&quot][[/FONT][FONT=&quot]0][/FONT].[FONT=&quot].[/FONT] You can get the number of command-line arguments by saying “[FONT=&quot]args.Length[/FONT]”.

You should print an error message if any of the following occur:
·Your program should handle and print warning messages for the following errors:
·User forgets to select –sorted or –unsorted command-line option
·User tries to remove an element not in the list
·User inputs an invalid option in the main loop
·User does not provide last name, first name, and age when inserting

You should allow people to be stored with the same names. (If their last names are the same, you should sort by first name.) If the user specifies that last name to remove, remove the FIRST person with that last name.

You should be able to compile your code from the command-line like this:

[FONT=&quot]csc[/FONT][FONT=&quot] *.cs[/FONT]

What I have so far:

FinalProject.cs-
Code:
using System;

public class Test {
    public static void Main() {
        List list = null;
        ArrayList list = new ArrayList();
        
        for (int i = 0; i <= 100; i++) {
            list.add(i);
        }
        Console.WriteLine(list.ToString());
        
        for (int i = 1; i <=100; i+=2) {
            list.remove(i);
        }
        
        Console.WriteLine(list.ToString());
        Console.ReadLine();
    }
}
Currently only running a set of numbers from 1-100, and then again but only evens to test the other scripts.

List.cs-
Code:
public interface List {
    void add(object x);
    bool remove(object x);
    int size();
}
ArrayList.cs-
Code:
using System;

public class ArrayList : List {
    private object[] arr;
    private int length;
    
    public ArrayList() {
        arr = new object[10];
        length = 0;
    }
    
    //factor = 0.5, make half as big
    //factor = 2, make twice as big
    private void resize(double factor) {
        object[] newarr = new object[(int)(arr.Length*factor)];
        Array.Copy(arr, 0, newarr, 0, length);
        arr = newarr;
    }
    
    public void add(object obj) {
        if (arr.Length == length) resize(2);
        arr[length] = obj;
        length++;
    }
    
    //resizing: if array less than half full, and has more than 10 elements
    public bool remove(object x) {
        for (int i = 0; i < length; i++) {
            if (arr[i].Equals(x)) {
                Array.Copy(arr, i+1, arr, i, length-i-1);
                length--;
                if (length < arr.Length/2 && arr.Length > 10) {
                    resize(0.5);
                }
                
                return true;
            }
        }
        
        return false;
    }
    
    public int size() {
        return length;
    }
    
    public override string ToString() { //from object
        string s = "";
        for (int i = 0; i < length; i++) {
            s += arr[i].ToString() + "\n";
        }
        return s;
    }
}
I just don't know where to begin. Kinda overwhelmed right now.. :(
 

CodeX

Do you need help understanding the OO concepts of classes/interfaces/objects/etc... or do you need help with the syntax of the language?

The requirements for the project are a bit ridiculous, you could implement a program with the same functionality with much less overhead, but I assume that is just to test you on the varous aspects of it.
 

CodeX

An Array list is an object, not a class

Array List as described here is a class. An object is an instance of a class or structure. He would need to make an instance of the arraylist class. which implements the list interface.... it's kind of redundant, and I assume the only reason they want you to do that is to maintain two seperate implementations of list, one that sorts the elements and one that does not, but that distinction is hardly enough reason to make two seperate classes to implement them imo.

I haven't used C# but in normal C++ OO this seems like list would be an abstract base class and arrayList and arrayListSorted would be child classes that inherit from list but overload the methods and implement them differently. If that were the case I would make the methods in list pure virtual and not even bother with a generic implementation.
 
TS
TS
RetroEvolute

RetroEvolute

404 Not Found
OT Supporter
Mar 25, 2008
14,857
Manhattan, K.S.
Do you need help understanding the OO concepts of classes/interfaces/objects/etc... or do you need help with the syntax of the language?

The requirements for the project are a bit ridiculous, you could implement a program with the same functionality with much less overhead, but I assume that is just to test you on the varous aspects of it.

Yeah. They really just kinda threw us into this stuff pretty quick. This is my first programming class I've ever taken and it's hard to grasp what everything does, and why/how.

Concepts of the classes/interfaces/objects would probably help. Beyond that, I have a pretty good concept of how the code flows. I mean... This is the 8th project I've technically had to do for the class, but it really goes over EVERYTHING and I realize I've forgotten/never completely understood some things.

My main problems are how to call classes, and what all the static, void, etc prefixes really mean. It seemed like I was just told. Oh, you need this here.. And I just obeyed. :ugh:

Thanks for the replies.
 

CodeX

Well basically a class is a collection of variables and methods. Think of it as defining a type of something, with distinct properties and actions that can be performed on it. You then use that class to create objects of that type, with each object having its own seperate set of member variables and each being able to call the same set of methods.

For example I re-wrote Super Mario 3 and an example of a class that I used was Goomba. The Goomba class contained all of the information needed to describe one specific goomba in the game world, such as its location and movement direction, current frame of animation, the boundaries of its "patrol" area, etc. The class also contained methods that could be carried out on the goomba, such as a method to check for collisions with other objects, a method to perform the death animation when mario jumps on him, and a method to to make the goomba enter what I called "berserk mode" (lol).

Now, to use this class I created an instance of it (an object) for each goomba on the current level. So you would have something like this:

Goomba goomba1 = new Goomba();
Goomba goomba2 = new Goomba();
Goomba goomba3 = new Goomba();
Goomba goomba4 = new Goomba();

Now, each of those 4 goombas are of the type Goomba (just like int x is of the type int) and each of them maintains a seperate set of the member variables of that class (location on the screen, etc.). In addition each of them can call the same set of methods defined in that class, except that the variables used in those methods will automatically point to the set of variables maintained by the specific object that called it.

So, if I were to call a method Move(int direction, int distance); on goomba1 I would do so like this: goomba1.Move(LEFT, 10); Now, the implementation of the Move method is defined and works the same for all goombas, but when it is called it uses the set of variables of the specific object that called it, this way when I tell goomba1 to move, only goomba1 moves, and not all of them, or a different one.

Make any sense?
 
TS
TS
RetroEvolute

RetroEvolute

404 Not Found
OT Supporter
Mar 25, 2008
14,857
Manhattan, K.S.
Well basically a class is a collection of variables and methods. Think of it as defining a type of something, with distinct properties and actions that can be performed on it. You then use that class to create objects of that type, with each object having its own seperate set of member variables and each being able to call the same set of methods.

For example I re-wrote Super Mario 3 and an example of a class that I used was Goomba. The Goomba class contained all of the information needed to describe one specific goomba in the game world, such as its location and movement direction, current frame of animation, the boundaries of its "patrol" area, etc. The class also contained methods that could be carried out on the goomba, such as a method to check for collisions with other objects, a method to perform the death animation when mario jumps on him, and a method to to make the goomba enter what I called "berserk mode" (lol).

Now, to use this class I created an instance of it (an object) for each goomba on the current level. So you would have something like this:

Goomba goomba1 = new Goomba();
Goomba goomba2 = new Goomba();
Goomba goomba3 = new Goomba();
Goomba goomba4 = new Goomba();

Now, each of those 4 goombas are of the type Goomba (just like int x is of the type int) and each of them maintains a seperate set of the member variables of that class (location on the screen, etc.). In addition each of them can call the same set of methods defined in that class, except that the variables used in those methods will automatically point to the set of variables maintained by the specific object that called it.

So, if I were to call a method Move(int direction, int distance); on goomba1 I would do so like this: goomba1.Move(LEFT, 10); Now, the implementation of the Move method is defined and works the same for all goombas, but when it is called it uses the set of variables of the specific object that called it, this way when I tell goomba1 to move, only goomba1 moves, and not all of them, or a different one.

Make any sense?
Makes sense. I'm well on my way figuring things out. Thanks.

Any more tips that have helped you out in the past when programming that may help me as well?
 
TS
TS
RetroEvolute

RetroEvolute

404 Not Found
OT Supporter
Mar 25, 2008
14,857
Manhattan, K.S.
Hey, is there anyone that could possibly help me with the

Instructions said:
[FONT=&quot]Person[/FONT] should represent a person, including their first name, last name, and age. It should implement the IComparable interface and the CompareTo method, and should override the Equals and ToString methods from object. You will also need to add the following method to Person (just to make it compile):

[FONT=&quot]public[/FONT][FONT=&quot] override int GetHashCode() {[/FONT]
[FONT=&quot] return 0;[/FONT]
[FONT=&quot] }[/FONT]

I've gotten pretty much the whole thing taken care of, but I'm not sure how to do this part, and it's kinda crucial to entire program.. :sad2:

Also, how to swap between -sorted and -unsorted via command prompt.

Thanks guys. I'd be screwed without you. Ha :bowdown:
 

CodeX

alot of that is specific to C#, which I don't know, I know C++. The IComparable interface and the Equals and ToString methods appear to be something already established in the languages common library, but you are being asked to overload them to be used for this specific class (the Person class). I assume that when a class implements and interface it is basically the same thing as a parent/child inheritence relationship in the OO terms that I am familiar with. If that is the case I could help you understand it better but without knowing for sure I am hesitant.

Passing in command line arguments is again dependant on the language, in C++ you do this:

void main(int argc, char* argv[])
{
//Rest of program
}

Where argc is then the space delimited argument count and argv is a pointer to an array of C-Style strings containing the arguments. In other languages like visual basic and java it is completely different and I assume it is somewhat different in C# as well.
 
TS
TS
RetroEvolute

RetroEvolute

404 Not Found
OT Supporter
Mar 25, 2008
14,857
Manhattan, K.S.
alot of that is specific to C#, which I don't know, I know C++. The IComparable interface and the Equals and ToString methods appear to be something already established in the languages common library, but you are being asked to overload them to be used for this specific class (the Person class). I assume that when a class implements and interface it is basically the same thing as a parent/child inheritence relationship in the OO terms that I am familiar with. If that is the case I could help you understand it better but without knowing for sure I am hesitant.

Passing in command line arguments is again dependant on the language, in C++ you do this:

void main(int argc, char* argv[])
{
//Rest of program
}

Where argc is then the space delimited argument count and argv is a pointer to an array of C-Style strings containing the arguments. In other languages like visual basic and java it is completely different and I assume it is somewhat different in C# as well.


Thanks. I've got that working now.

One more.. This may be simple for anyone with a solid background is programming, but I'm confused:
Code:
Console.Write("Input a person <last first age> to insert: ");
        string personinfo = Console.ReadLine();
        char[] delimeter = {' '};
        string[] tokens = personinfo.Split(delimeter);
        return tokens;

ERROR CS0029: Cannot implicitly convert type 'string[]' to 'string'

:dunno:
 

Limp_Brisket

Active Member
Jan 2, 2006
48,290
Utah
Thanks. I've got that working now.

One more.. This may be simple for anyone with a solid background is programming, but I'm confused:
Code:
Console.Write("Input a person <last first age> to insert: ");
        string personinfo = Console.ReadLine();
        char[] delimeter = {' '};
        string[] tokens = personinfo.Split(delimeter);
        return tokens;
ERROR CS0029: Cannot implicitly convert type 'string[]' to 'string'

:dunno:

i don't see anything wrong there, my guess is that in your function signature you put the return value as string but you're returning tokens which is string[].
 

Limp_Brisket

Active Member
Jan 2, 2006
48,290
Utah
How do you mean? Ha.

The bolded is pretty much what the error tells me, isn't it? :dunno:

well i don't know what function that code segment you pasted is in, but imagine it was like this

Code:
public string someFunction(){
      string[] tokens;
      return tokens;
}
that would give you an error because in the signature you said the function was going to return a string and then you returned a string array.
 
TS
TS
RetroEvolute

RetroEvolute

404 Not Found
OT Supporter
Mar 25, 2008
14,857
Manhattan, K.S.
well i don't know what function that code segment you pasted is in, but imagine it was like this

Code:
public string someFunction(){
      string[] tokens;
      return tokens;
}
that would give you an error because in the signature you said the function was going to return a string and then you returned a string array.

Ahh, thank you. That did it. :bigthumb:
 
TS
TS
RetroEvolute

RetroEvolute

404 Not Found
OT Supporter
Mar 25, 2008
14,857
Manhattan, K.S.
wow, OT actually helping someone in the comp forum. Jesus christ!

I know. OT ftw.

Btw, I just turned the project in. I didn't get everything done perfectly, but it's complete enough, I think. Should get a good majority of the points.

And thanks for all the help. I would be really hurting if it weren't for it.

:h5:
 

whup

I wish you had children and.. so that I could step
Feb 12, 2007
1,592
This project would have been really simple with .NET 3.5 and Linq. C# 4 isn't even that far off either.

I guess Linq and generics might be a bit much at first?
 

critter783

OT Supporter
Jul 15, 2005
1,782
This project would have been really simple with .NET 3.5 and Linq. C# 4 isn't even that far off either.

I guess Linq and generics might be a bit much at first?

It probably would've been, but generics aren't the answer to everything. This seems to be a pretty good exercise to illustrate the concepts being presented.
 

whup

I wish you had children and.. so that I could step
Feb 12, 2007
1,592
It would have been ideal for this. I can't remember the last time I used ArrayList.

Generics isn't the answer to everything but when it comes to collections I've found it almost always is.

It's just a bit concerning to see this being taught, how far behind it is with the current state of C# and code being written today, though I can't say I'm surprised heh.
 

SPACECATAZ

Active Member
Dec 22, 2006
2,482
I pretty much help anyone who posts here about programming. :coolugh:

What's a good way to learn/retain C++ programming?

I'm reading a book called C++ Primer Plus, but it seems like all of the fundamentals I am learning are going in one ear and out the other. I have to take a class on it next semester and it looks like I'm already trouble. How did you go about learning C++? Thanks
 

CodeX

What's a good way to learn/retain C++ programming?

I'm reading a book called C++ Primer Plus, but it seems like all of the fundamentals I am learning are going in one ear and out the other. I have to take a class on it next semester and it looks like I'm already trouble. How did you go about learning C++? Thanks

Do it.

Write programs in your free time, start with something trivial, then think "what small thing would make this program better?" Then figure out how to do that. This way you are forced to keep learning how to do new things, and in doing so will almost inevitably learn the deeper more difficult aspects of the language.

Keep doing that, program on a daily basis if even for only a half hour. Make programs that interest you. I wrote games, the first ones I wrote were text based DOS games, then I thought, "I want to do graphics" so I learned how to write an inline assembly putpixel routine using DOS Mode X. Later, I taught myself the majority of the DirectX and OpenGl API's and now, if I ever had the time, I could make some pretty good looking games, a far cry from the shit I started with, but it is a natural progression, and it takes time depending on how much you put into it.

If you were looking for book suggestions I don't have any, thats not how I learn and imo thats not a good way to learn to program, you learn to program by practicing it.

As far as figuring things out, cplusplus.com is a great resource for language specific information, the msdn is also an excellent resource if you are developing for windows, other than that, ask on forums. If you can't figure out how to do something there are forums with hundreds of highly qualified people, most of whom will help if you ask the question in a way indicating that you actually care to learn, and not have it done for you.

And for the love of god, EXPERIMENT. You can't hurt anything by doing something wrong, the worst that can happen is a compile time error (unless you are writing assembly, then the worst that can happen is you will have to restart your computer)
 

Users who are viewing this thread

About Us

  • Please do not post anything that violates any Local, State, Federal or International Laws. Your privacy is protected. You have the right to be forgotten. Site funded by advertising, link monetization and member support.
OT v15.8.1 Copyright © 2000-2022 Offtopic.com
Served by fu.offtopic.com

Online statistics

Members online
395
Guests online
55
Total visitors
450

Forum statistics

Threads
369,597
Messages
16,898,207
Members
86,875
Latest member
Theodor