Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 588 589 [590] 591 592 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 907717 times)

Bumber

  • Bay Watcher
  • REMOVE KOBOLD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8835 on: January 24, 2016, 03:16:22 am »

I'm doing Java right now and my compiler is complaining about a memory leak when I use it. I'm not sure how to 'plug' the leak and I wanted to ask what i may be missing.

Spoiler (click to show/hide)

EDIT: Forgot to mention that i'm inputting ints into a 2d array. This is basically a calorie tracker program.
please stop using spoilers or quotes for code
Why? Not everyone wants to scroll through a bunch of someone else's code. Spoilers is the polite choice.
Logged
Reading his name would trigger it. Thinking of him would trigger it. No other circumstances would trigger it- it was strictly related to the concept of Bill Clinton entering the conscious mind.

THE xTROLL FUR SOCKx RUSE WAS A........... DISTACTION        the carp HAVE the wagon

A wizard has turned you into a wagon. This was inevitable (Y/y)?

Rose

  • Bay Watcher
  • Resident Elf
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8836 on: January 24, 2016, 03:22:56 am »

Code tags are the polite choice, really.
Logged

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #8837 on: January 24, 2016, 03:35:19 am »

Code tags inside a spoiler are best for long things.
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

Rose

  • Bay Watcher
  • Resident Elf
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8838 on: January 24, 2016, 04:11:40 am »

Code tags already add their own scrollbar, though.


Code: [Select]
import java.util.Scanner;


public class CalorieTracker {

   static int DAYS = 7;
    static int FOOD = 4;

public static void main(String []args){
       
       
       
       
       
        String theDays [] = {"Sunday","Monday","Tuesday","Wednesday" ,"Thursday","Friday","Saturday"};
       
        String eatingFOOD[] = {"Breakfast", "Lunch", "Dinner", "Snacks"};
       
        int[][] dayForCalories = new int [DAYS][FOOD];
       
       
       
       [b] Scanner input = new Scanner(System.in);[/b] //THIS IS WHERE THE LEAK IS**********

       
        //change later from i to rows and cols
        for (int rows = 0; rows <= 7; rows++){//gives days
            for (int cols = 0; cols <= 4; cols++){//gives food intake per time.
               
               
            System.out.println("How much did you eat on, " + theDays + " for " + eatingFOOD[cols]);
           
                dayForCalories[rows][cols] = input.nextInt();
               
               
            }
        }
       
     }

}
Logged

Bumber

  • Bay Watcher
  • REMOVE KOBOLD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8839 on: January 24, 2016, 04:15:52 am »

Code tags already add their own scrollbar, though.
It's kind of pain for those who want to read the large code segments. I suppose you could just copy-paste into your text editor of choice.
Logged
Reading his name would trigger it. Thinking of him would trigger it. No other circumstances would trigger it- it was strictly related to the concept of Bill Clinton entering the conscious mind.

THE xTROLL FUR SOCKx RUSE WAS A........... DISTACTION        the carp HAVE the wagon

A wizard has turned you into a wagon. This was inevitable (Y/y)?

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #8840 on: January 24, 2016, 04:43:30 am »

clearly pastebin is required for larger code segments
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Parsely

  • Bay Watcher
    • View Profile
    • My games!
Re: if self.isCoder(): post() #Programming Thread
« Reply #8841 on: January 24, 2016, 04:49:04 am »

Code tags already add their own scrollbar, though.
It's kind of pain for those who want to read the large code segments. I suppose you could just copy-paste into your text editor of choice.
If you have really big code you should probably be using pastebin. If you don't like scrolling you can always paste it into full screen Notepad++.

Edit:
* GUNINANRUNIN ends up parroting things that have already been said. ;v
Logged

nullBolt

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8842 on: January 24, 2016, 05:04:19 am »

I'm a university student doing programming. I'm in a Team Project. We're doing a Client-Database software. I build the database and ask someone to fill it up with dummy data. He then goes and changes the names of all the tables, the dummy data is literally keyboard bashing and, worst of all, we can't use the login function because the hashvalues for the passwords stored in the database are nonsense.

I had to fix everything. Then again, I suppose it's an improvement over the past few times of "I left it at home" like we're still at school and I'm his teacher and we don't have ten billion places to upload this stuff.

It'd be faster if I just did it all myself.

At least one of my other team members is competent and diligent, I guess.

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #8843 on: January 24, 2016, 05:21:27 am »

* GUNINANRUNIN ends up parroting things that have already been said. ;v
I like how you not only posted things already mentioned, but you managed to get it close to verbatim. That's quite a feat :P
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

Parsely

  • Bay Watcher
    • View Profile
    • My games!
Re: if self.isCoder(): post() #Programming Thread
« Reply #8844 on: January 24, 2016, 05:29:15 am »

* GUNINANRUNIN ends up parroting things that have already been said. ;v
I like how you not only posted things already mentioned, but you managed to get it close to verbatim. That's quite a feat :P
* GUNINANRUNIN forces Orange into the blender v:<
Logged

itisnotlogical

  • Bay Watcher
  • might be dat boi
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8845 on: January 24, 2016, 06:00:50 am »

Code tags inside a spoiler are best for long things.

On most browsers I've tried, that seems to squish the code box to just one line being viewable at a time. changes from what I remember it doing last time, forcing me to edit my post and making me feel dumb

Spoiler (click to show/hide)
Logged
This game is Curtain Fire Shooting Game.
Girls do their best now and are preparing. Please watch warmly until it is ready.

Parsely

  • Bay Watcher
    • View Profile
    • My games!
Re: if self.isCoder(): post() #Programming Thread
« Reply #8846 on: January 24, 2016, 08:10:47 am »

Code tags inside a spoiler are best for long things.

On most browsers I've tried, that seems to squish the code box to just one line being viewable at a time. changes from what I remember it doing last time, forcing me to edit my post and making me feel dumb

Spoiler (click to show/hide)
Truly it is not logical.
Logged

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #8847 on: January 24, 2016, 08:48:03 am »

I don't like code blocks either because they get squished so much that the scrollbar blocks the single line.

edit: Inheritance question.

Let's say I want to have two types of objects that can run collision tests on each other, a CollidableCircle and a CollidableRectangle. They have radically different properties and ways to check collision. In the end, I will need to detect a Circle-Circle, Circle-Rectangle, and Rectangle-Rectangle collision. How do I make it so I can have a single variable that can hold either kind of collision object?

First attempt and second attempt didn't work: having an interface or parent class that asks for a Collided(CollidableCircle other) and Collided(CollidableRect other) method to be implemented. Reason being that if you set the variable type to either the interface or the superclass, the Collided() function doesn't know what inherited types you are trying to collide with each other. @_@ Times like this I miss Python... This is C# btw.

edit2: apparently the dynamic keyword fixes my problem, but I actually have no idea how it worked. :v Is there a statically typed way to solve this problem, though?

current solution:
abstract class CollisionShape
{
  public bool Collided(CollisionShape other)
  {
    dynamic Other = other;
    return CollidedImpl(Other);
  }
  abstract public bool CollisionImpl(CollisionCircle other);
  abstract public bool CollisionImpl(CollisionRect other);
}

class CollisionCircle: CollisionShape
{
  override public bool CollisionImpl(CollisionCircle other) { //whatever }
  override public bool CollisionImpl(CollisionRect other) { //whatever }
}

class CollisionRect: CollisionShape
{
  override public bool CollisionImpl(CollisionCircle other) { //whatever }
  override public bool CollisionImpl(CollisionRect other) { //whatever }
}

(manual syntax highlighting for fun)
« Last Edit: January 24, 2016, 10:38:33 am by Skyrunner »
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Rose

  • Bay Watcher
  • Resident Elf
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8848 on: January 24, 2016, 10:44:16 am »

If you need to have a different function for every type of colission, then maybe it might be better to use a static function in the base collider class that takes either of the two shapes as an argument?

Example:
Code: [Select]
abstract class CollisionShape
{
  public bool Collided(CollisionShape other)
  {
    dynamic Other = other;
    return CollidedImpl(Other);
  }
  static public bool CollisionImpl(CollisionCircle a, CollisionCircle b);
  static public bool CollisionImpl(CollisionRect a, CollisionRect b);
  static public bool CollisionImpl(CollisionCircle a, CollisionRect b);
}
Logged

nullBolt

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8849 on: January 24, 2016, 10:45:41 am »

In Java, this is a standard polymorphism problem.

An array for CollisionShape should be able to hold both CollisionCircle and CollisionTriangle as long as you declare them as being of the type CollisionShape.

So:

Code: [Select]
CollisionTriangle exampleTriangle = new ColissionShape(whatever);
HashMap<Integer, CollisionShape> map = new HashMap<Integer, ColissionShape>();
map.add(exampleTriangle);

This should function fine in Java but I just wrote it out quickly and it obviously hasn't been tested.

I imagine this should work in all OOP languages but I've never used C#.
Pages: 1 ... 588 589 [590] 591 592 ... 796