• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 14:35
CET 20:35
KST 04:35
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
[ASL22] Finals Preview: Sweet Victory!12[ASL22] Ro4 Preview: Mirror Mirror6[ASL22] Ro8 Preview: Within Reach5[ASL22] Ro8 Preview: In A Tizzy11[ASL22] Ro16 Preview: Holy Diver5
Community News
Weekly Cups (October 19-25): Clem triples first in 5.0.175Evil Geniuses - shutting down21ISSL (IntoTheTV x SOOP SC2 League): Premier7Weekly Cups (October 12-18): 5.0.17 era begins1[$1250] International ShieldBattery League99
StarCraft 2
General
How do you feel about the mass reverts in the 5.0.17 PTR? No more SC2 streamers on team liquid? Behind the Blue - Team Liquid History Book 5.0.17 Patch Notes - October 7, 2026 Evil Geniuses - shutting down
Tourneys
ISSL (IntoTheTV x SOOP SC2 League): Premier Stellar Fest TWO the Moon (Dec 16-20) Sparkling Tuna Cup - Weekly Open Tournament RSL goes to London! 2026 Offline Finals Nov 21-22 RSL 2026 Offline Finals LCQ Sign-Ups
Strategy
[H] ZvP Mid-Late Game: Stalkers Collossi HT
Custom Maps
[D] the control group Simple Questions/Answers [G] How to playtest arcade maps [A] Nemrods 1/4 players
External Content
Mutation # 550 Graveyard Shift The PondCast: SC2 News & Results Mutation # 549 Endless Sparkles Mutation # 548 Shining Bright
Brood War
General
Rollback Netcode in Brood War Top Players of October 2026 (EBPD Rankings) BW General Discussion Sagi.gg Launcher Released Mathematicians needed to calc "Low Econ Epicness"
Tourneys
[Megathread] Daily Proleagues 2v2v2v2 Tournament Stage 1: Community Poll [IPSL] IPSL Winter 26-27 Ro24 is HERE! [$1250] International ShieldBattery League
Strategy
50,000-Word Terran Builds Memo 2026-10-22 Cliff Jump Revisited (1 in a 1000 strategy) Simple Questions, Simple Answers
Other Games
General Games
Stormgate/Frost Giant Megathread Nintendo Switch Thread Deus Ex 3!? Mobile Legends: Bang Bang Armies of Exigo - YesYes?
Dota 2
Dota 2 Champions League Season 3 Begins April 25!
League of Legends
[TL LoL EUW IHs] Teemo shall perish
Heroes of the Storm
Heroes of the Storm 2.0
Hearthstone
Deck construction bug
TL Mafia
TL Mafia Community Thread
Community
General
US Politics Mega-thread Russo-Ukrainian War Thread The Chess Thread Things Aren’t Peaceful in Palestine Canadian Politics Mega-thread
Fan Clubs
Fan Club Resource Thread Reynor Fan Club The Flash Fan Club
Media & Entertainment
Anime Discussion Thread Movie Discussion! [Manga] One Piece
Sports
Football (Soccer) Thread TeamLiquid Health and Fitness Initiative For 2023 MLB/Baseball 2023
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
Recent Gifted Posts
Blogs
Why I built Repalyzer (1.1.…
BlueStar
Innovate or Game Over?
TrAiDoS
ShieldBattery on Mac
Kraekkling
[ASL22] Ro4 Day1 Ticket Giv…
bITt.mAN
Can Bots Beat Pros?? Starcr…
namkraft
Customize Sidebar...

Website Feedback

Closed Threads



Active: 13254 users

The Big Programming Thread - Page 480

Forum Index > General Forum
Post a Reply
Prev 1 478 479 480 481 482 1032 Next
Thread Rules
1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution.
2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20)
3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible.
4. Use [code] tags to format code blocks.
Manit0u
Profile Blog Joined August 2004
Poland17868 Posts
May 13 2014 23:23 GMT
#9581
size_t won't do. I guess I should use vector<T>::size_type
Time is precious. Waste it wisely.
nunez
Profile Blog Joined February 2011
Norway4003 Posts
Last Edited: 2014-05-13 23:42:37
May 13 2014 23:24 GMT
#9582
i used reference in median since that was what manitou used, this changes sorts the indexes in the actual vector ofc. if this is acceptable then that's better than copy.

edit: you can just use auto i think. integer types round down(right?).

alternatively you could enforce the 'default constructibleness' (LOL) with a static assert like so:
template<class T>
T avg(vector<T> const& v){
static_assert(std::is_default_constructible<T>::value,"default constructibleness lacking");
auto size=v.size();
assert(size>0);
return std::accumulate(std::begin(v),std::end(v),T())/size;
}


if you are commited to size_type, you gotta use typename ...::size_type, since the type is dependant on the template.
conspired against by a confederacy of dunces.
Manit0u
Profile Blog Joined August 2004
Poland17868 Posts
Last Edited: 2014-05-14 21:10:38
May 14 2014 21:06 GMT
#9583
I guess I'm overthinking this...


#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

template<class T>
class Stack
{
public:
T GetSum();
T GetMean();
T GetMedian();
void AddElement(T const&);
void Display();

private:
std::vector<T> elements;
};

template<class T>
void Stack<T>::AddElement(T const& element)
{
elements.push_back(element);
}

template<class T>
void Stack<T>::Display()
{
std::copy(elements.begin(), elements.end(), std::ostream_iterator<T>(std::cout));
std::cout << std::endl;
}

template<class T>
T Stack<T>::GetSum()
{
return std::accumulate(std::begin(elements), std::end(elements), T());
}

template<class T>
T Stack<T>::GetMean()
{
T size = elements.size();
T sum = GetSum();

return sum / size;
}

template<class T>
T Stack<T>::GetMedian()
{
T size = elements.size();
T num = size / 2;

std::nth_element(elements.begin(), elements.begin() + num, elements.end());

return elements[num];
}

int main()
{
// create stacks
Stack<double> doubleStack;
Stack<float> floatStack;
Stack<int> intStack;

// populate stacks
doubleStack.AddElement(7.1);
doubleStack.AddElement(1.3);
doubleStack.AddElement(3.8);
doubleStack.AddElement(8.7);

floatStack.AddElement(7.138);
floatStack.AddElement(1.387);
floatStack.AddElement(3.871);
floatStack.AddElement(8.713);

intStack.AddElement(7);
intStack.AddElement(1);
intStack.AddElement(3);
intStack.AddElement(8);

// display data
std::cout << "Stack of doubles: " << std::endl;
std::cout << doubleStack.Display();
std::cout << "Doubles mean: " << doubleStack.GetMean() << std::endl;
std::cout << "Doubles median: " << doubleStack.GetMedian() << std::endl;

std::cout << "Stack of floats: " << std::endl;
std::cout << floatStack.Display();
std::cout << "Floats mean: " << floatStack.GetMean() << std::endl;
std::cout << "Floats median: " << floatStack.GetMedian() << std::endl;

std::cout << "Stack of ints: " << std::endl;
std::cout << intStack.Display();
std::cout << "Ints mean: " << intStack.GetMean() << std::endl;
std::cout << "Ints median: " << intStack.GetMedian() << std::endl;

return 0;
}


93:35 note: mismatched types `const std::basic_string<_CharT, _Traits, _Alloc>` and `void`

And I'm getting a wall of weird errors, mostly relating to ostream and not being able to convert from void to different chars. What should I do about it?
Time is precious. Waste it wisely.
nunez
Profile Blog Joined February 2011
Norway4003 Posts
Last Edited: 2014-05-14 21:33:48
May 14 2014 21:30 GMT
#9584
dblpost.
conspired against by a confederacy of dunces.
nunez
Profile Blog Joined February 2011
Norway4003 Posts
Last Edited: 2014-05-14 21:35:24
May 14 2014 21:33 GMT
#9585
the Display functions return value is void, so you can't pass the result to std::cout's << operand.
however the printing is done internally in your display function, so you don't have to.
intStack.Display();
would do the trick instead of
std::cout<<intStack.Display();

nitpick on your interface is that getMean, getSum and Display should be const functions.
conspired against by a confederacy of dunces.
Manit0u
Profile Blog Joined August 2004
Poland17868 Posts
Last Edited: 2014-05-14 22:13:02
May 14 2014 21:41 GMT
#9586
On May 15 2014 06:33 nunez wrote:
the Display functions return value is void, so you can't pass the result to std::cout's << operand.
however the printing is done internally in your display function, so you don't have to.
intStack.Display();
would do the trick instead of
std::cout<<intStack.Display();

nitpick on your interface is that getMean, getSum and Display should be const functions.


Doh... That's what you get for trying to do stuff for fun after an entire day of looking at some completely different and unrelated code...

Edit: There were a couple more errors. Didn't make stuff const because the compiler was complaining a lot and I really don't have time or strength for it today.

Anyway, here's a working program if anyone needs anything like that in the future:
+ Show Spoiler +


#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

template<class T>
class Stack
{
public:
T GetSum();
float GetMean();
float GetMedian();
void AddElement(T const&);
void Display();

private:
std::vector<T> elements;
};

template<class T>
void Stack<T>::AddElement(T const& element)
{
elements.push_back(element);
}

template<class T>
void Stack<T>::Display()
{
std::copy(elements.begin(), elements.end(), std::ostream_iterator<T>(std::cout, " "));
std::cout << std::endl;
}

template<class T>
T Stack<T>::GetSum()
{
return std::accumulate(std::begin(elements), std::end(elements), T());
}

template<class T>
float Stack<T>::GetMean()
{
T size = elements.size();
T sum = GetSum();

return float(sum) / float(size);
}

template<class T>
float Stack<T>::GetMedian()
{
T size = elements.size();
T num = size / 2;

std::nth_element(elements.begin(), elements.begin() + num, elements.end());

return int(size) % 2 ? elements[num] : (elements[num] + elements[num -1]) / 2;
}

int main()
{
// create stacks
Stack<double> doubleStack;
Stack<float> floatStack;
Stack<int> intStack;

// populate stacks
doubleStack.AddElement(7.1);
doubleStack.AddElement(1.3);
doubleStack.AddElement(3.8);
doubleStack.AddElement(8.7);

floatStack.AddElement(7.138);
floatStack.AddElement(1.387);
floatStack.AddElement(3.871);
floatStack.AddElement(8.713);

intStack.AddElement(7);
intStack.AddElement(1);
intStack.AddElement(3);
intStack.AddElement(8);

// display data
std::cout << "Stack of doubles: " << std::endl;
doubleStack.Display();
std::cout << "Doubles mean: " << doubleStack.GetMean() << std::endl;
std::cout << "Doubles median: " << doubleStack.GetMedian() << std::endl;

std::cout << "Stack of floats: " << std::endl;
floatStack.Display();
std::cout << "Floats mean: " << floatStack.GetMean() << std::endl;
std::cout << "Floats median: " << floatStack.GetMedian() << std::endl;

std::cout << "Stack of ints: " << std::endl;
intStack.Display();
std::cout << "Ints mean: " << intStack.GetMean() << std::endl;
std::cout << "Ints median: " << intStack.GetMedian() << std::endl;

return 0;
}

Time is precious. Waste it wisely.
Shield
Profile Blog Joined August 2009
Bulgaria4824 Posts
May 14 2014 22:27 GMT
#9587
Why do methods start with a capital letter? I've checked some C++ code, and all start with a lowercase letter such as here:
http://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp3_OOP.html
netherh
Profile Blog Joined November 2011
United Kingdom333 Posts
May 15 2014 00:05 GMT
#9588
On May 15 2014 07:27 darkness wrote:
Why do methods start with a capital letter? I've checked some C++ code, and all start with a lowercase letter such as here:
http://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp3_OOP.html


C++ doesn't have fixed style rules / guidelines:

median()
Median()
get_median()
getMedian()
GetMedian()

Take your pick.
Shield
Profile Blog Joined August 2009
Bulgaria4824 Posts
May 15 2014 00:26 GMT
#9589
On May 15 2014 09:05 netherh wrote:
Show nested quote +
On May 15 2014 07:27 darkness wrote:
Why do methods start with a capital letter? I've checked some C++ code, and all start with a lowercase letter such as here:
http://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp3_OOP.html


C++ doesn't have fixed style rules / guidelines:

median()
Median()
get_median()
getMedian()
GetMedian()

Take your pick.


Well, that's bad for C++ then. However, (some of) C++ STL methods start with a lowercase letter. So why don't you follow the standard?
Nesserev
Profile Blog Joined January 2011
Belgium2760 Posts
Last Edited: 2014-05-15 00:46:49
May 15 2014 00:45 GMT
#9590
--- Nuked ---
WolfintheSheep
Profile Joined June 2011
Canada14127 Posts
May 15 2014 00:57 GMT
#9591
On May 15 2014 09:26 darkness wrote:
Show nested quote +
On May 15 2014 09:05 netherh wrote:
On May 15 2014 07:27 darkness wrote:
Why do methods start with a capital letter? I've checked some C++ code, and all start with a lowercase letter such as here:
http://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp3_OOP.html


C++ doesn't have fixed style rules / guidelines:

median()
Median()
get_median()
getMedian()
GetMedian()

Take your pick.


Well, that's bad for C++ then. However, (some of) C++ STL methods start with a lowercase letter. So why don't you follow the standard?


There is no universal standard. There is only "currently accepted standards" which seem to change every decade.

And C++ spans basically very age of coding standards. That means Pascal and Camel case, underscores in variables/methods, underscore prefixes for private, and on and on and on.
Average means I'm better than half of you.
Frudgey
Profile Joined September 2012
Canada3367 Posts
May 15 2014 01:20 GMT
#9592
Do you guys know where I could find any information about running a .py file over multiple computers?

Basically I want to know where I can find any information about running a particular python program over multiple computers. I guess to be more clear, I'd want multiple computers running the same program at the same time. One computer is capable of running the program just fine and it doesn't take very long, but I'd be able to collect data much faster if I could have several computers crunching numbers into the program at the same time.

I'm not sure if I'm describing what it is exactly that I want to do in the right way, so if you have any information that you think might be remotely helpful, or if you could point me in the direction to find said information that'd be much appreciated.

Thanks!
It is better to die for The Emperor than live for yourself.
phar
Profile Joined August 2011
United States1080 Posts
May 15 2014 01:39 GMT
#9593
Some starting points:

https://wiki.python.org/moin/ParallelProcessing
http://blog.cloudera.com/blog/2013/01/a-guide-to-python-frameworks-for-hadoop/
Who after all is today speaking about the destruction of the Armenians?
Cyx.
Profile Joined November 2010
Canada806 Posts
May 15 2014 01:58 GMT
#9594
On May 15 2014 10:20 Frudgey wrote:
Do you guys know where I could find any information about running a .py file over multiple computers?

Basically I want to know where I can find any information about running a particular python program over multiple computers.

I guess to be more clear, I'd want multiple computers running the same program at the same time.

phar's post is a good place to start... I just though having these three sentences one after another was a little overkill rofl
sluggaslamoo
Profile Blog Joined November 2009
Australia4494 Posts
Last Edited: 2014-05-15 05:32:05
May 15 2014 05:29 GMT
#9595
On May 15 2014 10:20 Frudgey wrote:
Do you guys know where I could find any information about running a .py file over multiple computers?

Basically I want to know where I can find any information about running a particular python program over multiple computers. I guess to be more clear, I'd want multiple computers running the same program at the same time. One computer is capable of running the program just fine and it doesn't take very long, but I'd be able to collect data much faster if I could have several computers crunching numbers into the program at the same time.

I'm not sure if I'm describing what it is exactly that I want to do in the right way, so if you have any information that you think might be remotely helpful, or if you could point me in the direction to find said information that'd be much appreciated.

Thanks!


This would be much the same as doing merge sort algorithms over multiple cores I presume. Number crunching can mean a lot of things so you might have to be more specific.

Basically some software have multicore capability built in, you will need to decipher these and learn how to code them manually, so that instead of doing multicore, you are doing multiprocessor. Basically there is almost no difference between the two except that data is transferred over the wire rather than exclusively inside the motherboard.

After that it shouldn't be difficult at all, it would be like torrenting, each computer is given a piece of the pie to number crunch, and it all gets inserted into the final collection. This collection then does a basic sort using presumptions made by the smaller pieces to put everything in order very quickly.

http://en.wikipedia.org/wiki/Merge_sort
Come play Android Netrunner - http://www.teamliquid.net/forum/viewmessage.php?topic_id=409008
3FFA
Profile Blog Joined February 2010
United States3931 Posts
Last Edited: 2014-05-15 12:36:28
May 15 2014 12:35 GMT
#9596
For my AP CS class we are creating the DownloadInfo and MusicDownloads classes(along with a main) from 2013 Question 1: http://media.collegeboard.com/digitalServices/pdf/ap/apcentral/ap13_frq_comp_sci.pdf

As part of this she wants us to check that this segment works properly:
public DownloadInfo getDownloadInfo(String title)
{
for(DownloadInfo info : downloadList)
{
if (info.getTitle().equals(title))
{
return info;
}
}
return null;
}


She told me to write the following in main to check it (unless I misunderstood / misheard) :
DownloadInfo temp = music.getDownloadInfo("Pompeii");
if((temp.getTitle()) == null) //returns false, moves on to else as it should(Pompeii is in the musicList)
System.out.println(temp.getTitle() + " was never downloaded");
else
System.out.println(temp.getTitle() + " was downloaded " + temp.getTimesDownloaded() + " times");

DownloadInfo temp2 = music.getDownloadInfo("Jingle Bells");
if((temp2.getTitle()) == null) //returns a Null Pointer Exception due to not being in the list.
System.out.println(temp2.getTitle() + " was never downloaded");
else
System.out.println(temp2.getTitle() + " was downloaded " + temp2.getTimesDownloaded() + " times");

Any help with determining how to fix this would be great. Thanks guys
"As long as it comes from a pure place and from a honest place, you know, you can write whatever you want."
Nesserev
Profile Blog Joined January 2011
Belgium2760 Posts
May 15 2014 12:52 GMT
#9597
--- Nuked ---
Amnesty
Profile Joined April 2003
United States2054 Posts
May 15 2014 23:36 GMT
#9598
On May 15 2014 06:06 Manit0u wrote:
I guess I'm overthinking this...


#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

template<class T>
class Stack
{
public:
T GetSum();
T GetMean();
T GetMedian();
void AddElement(T const&);
void Display();

private:
std::vector<T> elements;
};

.... code removed....


93:35 note: mismatched types `const std::basic_string<_CharT, _Traits, _Alloc>` and `void`

And I'm getting a wall of weird errors, mostly relating to ostream and not being able to convert from void to different chars. What should I do about it?


This is not good design.

Ideally, you wouldn't do this at all. A better design would not having them be members at all because they don't need to be. Then you can use whatever container you wanted too and It would just work. But of course, you cant do that because it would be a failing grade and you need to turn in poop. I had to cringe my way through programming courses before heh.


First It asked for an array class. So make it an array class and not a dynamic array class (vector).
You can't remove elements in your vector approach even though vector can do removes.

You cant use the algorithms on it because you didnt provide the the standard iterator interface.
Its hard to initialize. No constructors taking std::initalizer_list
Don't make display functions member functions. That should be separated. It's hard here because you didnt provide a iterator interface.


#include <algorithm>
#include <iterator>
#include <array>


template<typename T, std::size_t size>
class math_array
{
public:
typedef T* iterator;
typedef const T* const_iterator;

typedef std::reverse_iterator<T*> reverse_iterator;
typedef std::reverse_iterator<const T*> const_reverse_iterator;

iterator begin() { return &data[0]; }
iterator end() { return &data[size]; }

const_iterator begin() const { return &data[0]; }
const_iterator end() const { return &data[size]; }

reverse_iterator rbegin() { return reverse_iterator(end()); }
reverse_iterator rend() { return reverse_iterator(begin()); }

const_reverse_iterator rbegin() const { return reverse_iterator(end()); }
const_reverse_iterator rend() const { return reverse_iterator(begin()); }

T& operator[] (std::size_t index)
{
return data[index];
}
T const& operator[] (std::size_t index) const
{
return data[index];
}

template<class FuncT>
void for_each(FuncT func)
{
std::for_each(begin(),end(), func);
}

T average(T const& i = 0)
{
auto size=v.size();
assert(size>0);
return std::accumulate(std::begin(v),std::end(v),i)/size;
}
/// rest of your math functions already provided...

T data[size];
};


First of all. The only member is a public data. No constructors, everything is public. This makes the class an aggregate type so you can use array initialization on it like you can on regular arrays making it more natural.

Has iterator interface. Overloaded [] operator to make it similar to array.

Here I made a member function called for_each. This is kind of a trade off. It technically does not need to be a member, because there is a global equivalent so this should scream bad design. But after using C# for a long time, i find the member function ForEach's much more readable when you can just pass it a lambda and turn a multi-liner into a one liner. And easier to read code screams good design.



math_array<int,10> f = {3,4,5,56,234,23,4234,234,23423,4234};

f.for_each([](int n)
{
std::cout << n << std::endl;
});




Other thoughts

T average(T const& i = 0) {...}


Having math functions return T is weird. If you have are working with an int's and you run averages and other statistical math functions on it usually you want the decimal places too. Returning float or double causes other problems.
The sky just is, and goes on and on; and we play all our BW games beneath it.
3FFA
Profile Blog Joined February 2010
United States3931 Posts
May 16 2014 00:27 GMT
#9599
On May 15 2014 21:52 Nesserev wrote:
Show nested quote +
On May 15 2014 21:35 3FFA wrote:
For my AP CS class we are creating the DownloadInfo and MusicDownloads classes(along with a main) from 2013 Question 1: http://media.collegeboard.com/digitalServices/pdf/ap/apcentral/ap13_frq_comp_sci.pdf

As part of this she wants us to check that this segment works properly:
+ Show Spoiler +
public DownloadInfo getDownloadInfo(String title)
{
for(DownloadInfo info : downloadList)
{
if (info.getTitle().equals(title))
{
return info;
}
}
return null;
}


She told me to write the following in main to check it (unless I misunderstood / misheard) :
DownloadInfo temp = music.getDownloadInfo("Pompeii");
if((temp.getTitle()) == null) //returns false, moves on to else as it should(Pompeii is in the musicList)
System.out.println(temp.getTitle() + " was never downloaded");
else
System.out.println(temp.getTitle() + " was downloaded " + temp.getTimesDownloaded() + " times");

DownloadInfo temp2 = music.getDownloadInfo("Jingle Bells");
if((temp2.getTitle()) == null) //returns a Null Pointer Exception due to not being in the list.
System.out.println(temp2.getTitle() + " was never downloaded");
else
System.out.println(temp2.getTitle() + " was downloaded " + temp2.getTimesDownloaded() + " times");

Any help with determining how to fix this would be great. Thanks guys

First part of code should work as intended, I think.

In the second part, you're calling temp.getTitle(), even though you just checked that temp == null. You should store the 'search string' ("Pompeii", "Jingle Bells") in a string first, and then reuse those to print the messages (or put the thing in one function, you're basically doing the same thing 2 times).


Yes the first part does work, as I commented.

1) I can't put it in a function because she said I HAVE TO do it in main for this assignment. =/
2) I don't understand why the second part doesn't work. How come calling temp.getTitle() again after checking that temp == null has any effect?
3) Why should I store the 'search string' in a string first?(Probably goes hand in hand with #2)
Lastly, I want to make clear that this is to check whether or not the song titles were downloaded. Pompeii was downloaded, but Jingle Bells was not.
"As long as it comes from a pure place and from a honest place, you know, you can write whatever you want."
Ben...
Profile Joined January 2011
Canada3485 Posts
Last Edited: 2014-05-16 01:17:31
May 16 2014 00:56 GMT
#9600
On May 16 2014 09:27 3FFA wrote:


1) I can't put it in a function because she said I HAVE TO do it in main for this assignment. =/
2) I don't understand why the second part doesn't work. How come calling temp.getTitle() again after checking that temp == null has any effect?
3) Why should I store the 'search string' in a string first?(Probably goes hand in hand with #2)
Lastly, I want to make clear that this is to check whether or not the song titles were downloaded. Pompeii was downloaded, but Jingle Bells was not.

1) That sucks.

2) Because you just checked if the title was null. In the case where it is null, trying to call the getTitle() function to print out that the title was not found will not work because the title doesn't exist in your temp variable in the first place. That is where your null pointer exception is coming from. getTitle() retrieves the title from the song object, but if the title doesn't exist, you can't exactly retrieve it to output it. In the case where it is not null, it will work as you expect. Don't worry, this is a common mistake people just getting used to OO will make. I made it a few times myself back when I was first doing stuff with classes.

This leads to:

3) Since you cannot access the title from within your container of songs (because it is not in your container of music files), having a separate variable that stores the title is useful (for example, if your test fails, you can put
System.out.Println(songTitle + " was not downloaded!")
or something, rather than having to update the error message every time). This will allow more flexibility for your program (if you continue on with CS you will find that having programs that are flexible and can handle many situations are quite useful, and usually expected in assignments). If you replace each time you filled in "Pompeii" and "Jingle Bells" that with a variable storing a song name (just update the variable between songs. IE:
String songTitle = "Pompeii";
...
songTitle = "Jingle Bells";
...

), then you can search for whatever the title you need while only having to update one variable. Ideally, you would create a boolean search function that would return true if the song was found, but it doesn't sound like that is allowed according to your post and the link you provided.

Also, small nit-pick on your comments. You should not use "return" to describe things unless you are actually returning something. In the case of your comments about the if statements, you should use something like "evaluates false" rather than "returns false", since the boolean expression inside the if statement is evaluating whether or not the statement is true, and isn't technically returning anything. It's a small technicality, but it makes it easier for others to understand.

Hopefully I helped a bit.
"Cliiiiiiiiiiiiiiiiide" -Tastosis
Prev 1 478 479 480 481 482 1032 Next
Please log in or register to reply.
Live Events Refresh
Monday Night Weeklies
02:00
#68
RotterdaM844
TaKeTV 341
TKL 299
SteadfastSC188
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
RotterdaM 844
ByuN 620
TKL 299
SteadfastSC 188
BRAT_OK 60
StarCraft: Brood War
Bisu 948
Shuttle 714
Dewaltoss 131
firebathero 97
ProTech51
HiyA 42
Shinee 33
ToSsGirL 23
Aegong 20
scan(afreeca) 16
[ Show more ]
Sacsri 7
Dota 2
qojqva3194
420jenkins1428
monkeys_forever284
PGG 204
capcasts33
Counter-Strike
gobbtv271
Heroes of the Storm
Liquid`Hasu1404
MindelVK9
Other Games
tarik_tv6080
Grubby3350
Liquid`RaSZi2438
ceh91415
Beastyqt861
mouzStarbuck340
Mlord303
C9.Mang0177
Livibee177
XaKoH 93
Mew2King77
Organizations
Counter-Strike
PGL686
StarCraft: Brood War
lovetv 6
[ Show 16 non-featured ]
StarCraft 2
• Reevou 2
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• Laughngamez YouTube
• Migwel
StarCraft: Brood War
• HerbMon 49
• Eskiya23 18
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Dota 2
• WagamamaTV615
League of Legends
• Nemesis3671
Other Games
• imaqtpie585
• Shiphtur527
Upcoming Events
Replay Cast
3h 25m
Rogue vs Classic
Patches Events
17h 25m
PiGosaur Cup
1d 5h
Kung Fu Cup
1d 15h
Replay Cast
2 days
The PondCast
2 days
KCM Race Survival
2 days
IntoTheTV X SOOP
4 days
Reynor vs Solar
ByuN vs SHIN
Clem vs MaxPax
herO vs Rogue
WardiTV Invitational
4 days
IPSL
4 days
WorsT vs nOmaD
Dewalt vs andersOn
[ Show More ]
IntoTheTV X SOOP
5 days
WardiTV Invitational
5 days
IPSL
5 days
Azhi_Dahaki vs ggnore13
Double vs WIZARD
Replay Cast
6 days
Liquipedia Results

Completed

Proleague 2026-11-01
Blizzard Classic Cup 2026
HCC Season 3

Ongoing

Acropolis #5
KCM Race Survival 2026 Season 4
International ShieldBattery League
BSL Season 23
IPSL Winter 2026-27
Proleague 2026-11-02
Calamity Cup Div. A Season 8
IEM Beijing 2026
PGL Masters Bucharest 2026
Stake Ranked Episode 5
1win Private Club #2
Thunderpick World Champ. '26
ESL Pro League Season 24
Stake Ranked Episode 4
1win Private Club #1
Logitech G Play Connect 2026
SL StarSeries Fall 2026
FISSURE Playground #3
BLAST Open Fall 2026
Esports World Cup 2026

Upcoming

1xBet Invitational
SC4ALL II: Brood War
BSL 23: Non-Korean Championship
HSC XXX
Stellar Fest 2: Lunar Cup
SC4ALL II: StarCraft II
Kung Fu Cup 2026 Grand Finals
RSL Offline Finals
BLAST Bounty Winter 2027
eXTREMESLAND 2026
PGL Major Singapore 2026
Stake Ranked Episode 6
BLAST Rivals Fall 2026
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2026 TLnet. All Rights Reserved.