• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 09:37
CEST 15:37
KST 22:37
  • 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
Serral wins HomeStory Cup 2914Serral wins Maestros of the Game 243ByuL, and the Limitations of Standard Play3Team Liquid Map Contest #22: Results and Winners7Code S Season 2 (2026): RO4 and Finals Preview12
Community News
Balance hotfix patch 5.0.16b (July 16)46Reynor: GSL Loss Wasn't About Preparation Format16[IPSL] Spring 2026 Grand Finals - This Weekend!5Weekly Cups (July 6 - 12): Protoss strike back12BSL Season 22 Full Overview & Conclusion8
StarCraft 2
General
Balance hotfix patch 5.0.16b (July 16) [D] Wireframe Casting Removed Clem: "I don't have that much hope in Blizzard" Reynor: GSL Loss Wasn't About Preparation Format Is the larve respawn broken?
Tourneys
Master Swan Open (Global Bronze-Master 2) WardiTV Summer Cup 2026 GSL CK #5 Race War RSL Revival: Season 6 - Qualifiers and Main Event HomeStory Cup 29
Strategy
[G] Having the right mentality to improve
Custom Maps
New Map Maker - Looking for Advice - Love or Hate Work In Progress Melee Maps [D]RTS in all its shapes and glory <3
External Content
The PondCast: SC2 News & Results Mutation # 534 Burning Evacuation Mutation # 533 Die Together Mutation # 532 Nuclear Family
Brood War
General
ASL22 General Discussion BW General Discussion Pros Debate: Zerg Unfairly Nerfed? (ASL S22 map) Etiquete rules in Asl? Recent recommended BW games
Tourneys
[Megathread] Daily Proleagues Escore Tournament - Season 3 Small VOD Thread 2.0 [IPSL] Spring 2026 Grand Finals - This Weekend!
Strategy
Fighting Spirit mining rates Simple Questions, Simple Answers Creating a full chart of Zerg builds Relatively freeroll strategies
Other Games
General Games
Path of Exile General RTS Discussion Thread Nintendo Switch Thread Beyond All Reason Stormgate/Frost Giant Megathread
Dota 2
Looking for a Dota Mentor Official 'what is Dota anymore' discussion
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug
TL Mafia
TL Mafia Power Rank NeO.D_StephenKing vs This Guy From 1 Million Dance TL Mafia Community Thread Vanilla Mini Mafia
Community
General
US Politics Mega-thread The Games Industry And ATVI Russo-Ukrainian War Thread UK Politics Mega-thread YouTube Thread
Fan Clubs
The IdrA Fan Club The HerO Fan Club!
Media & Entertainment
Anime Discussion Thread Movie Discussion! [Req][Books] Good Fantasy/SciFi books Series you have seen recently...
Sports
2024 - 2026 Football Thread MLB/Baseball 2023 McBoner: A hockey love story Tennis[sport] Formula 1 Discussion
World Cup 2022
Tech Support
Simple Questions Simple Answers FPS when play League Of Legend on laptop How to clean a TTe Thermaltake keyboard?
TL Community
Northern Ireland Global Starcraft The Automated Ban List
Blogs
Poker (part 2)
Nebuchad
The Experiences We Want and …
TrAiDoS
An Exploration of th…
waywardstrategy
ramps on octagon
StaticNine
Funny Nicknames
LUCKY_NOOB
Evil Gacha Games and the…
ffswowsucks
StarCraft improvement
iopq
Customize Sidebar...

Website Feedback

Closed Threads



Active: 8828 users

The Big Programming Thread - Page 251

Forum Index > General Forum
Post a Reply
Prev 1 249 250 251 252 253 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.
tuho12345
Profile Blog Joined July 2011
4482 Posts
Last Edited: 2013-02-19 02:40:15
February 19 2013 02:15 GMT
#5001
Hi everyone, I'm a newbie in programming and Java specifically. I had a little experience with VB, which is significantly easier than Java or C I guess. So I'm kinda stuck right here and wish I'd have more time but it's due tomorrow and I'm struggling with other exams as well.

Here below is my code, and my question is how can I calculate the SalesTotal and SalesCount. I understand the logic but I have no idea how Java works.
Thanks

+ Show Spoiler +



/**
Assignment 2, CS49J, Spring 2013
Part B student
A cash register totals up sales and computes change due.
*/
public class CashRegister
{
      private double purchase;
      private double payment;

//... Add your code here
      private double SalesTotal;
      private int SalesCount;
/**
Constructs a cash register with no money in it.
*/
public CashRegister()
{
       purchase = 0;
       payment = 0;
      SalesCount = 0;
       SalesTotal = 0;
}

/**
Records the sale of an item.
@param amount the price of the item
*/
public void recordPurchase(double amount)
{
      double total = purchase + amount;
//... Add your code here
       purchase = total;

}

/**
Enters the payment received from the customer.
@param amount the amount of the payment
*/
public void enterPayment(double amount)
{
      payment = amount;
      SalesCount++;
}

/**
Computes the change due and resets the machine for the next customer.
@return the change due to the customer
*/
public double giveChange()
{
      double change = payment - purchase;
      purchase = 0;
       payment = 0;
      return change;
}

/**
Get the total amount of all sales for the day.
@param return sale total
*/
//... Add your code here
public double getSalesTotal()
{

      SalesTotal = purchase + purchase;
      return SalesTotal;

}



/**
Get the total number of sales for the day.
@param return the number of sales.
*/
//... Add your code here

public double getSalesCount()
{
      return SalesCount;
}
/**
Reset counters and totals for the next days sales.
@param none
*/
//... Add your code here
public void reset()
{
      SalesCount = 0;
      payment = 0;
      purchase = 0;
      SalesTotal = 0;
}
}

InRaged
Profile Joined February 2007
1047 Posts
February 19 2013 02:23 GMT
#5002
On February 19 2013 08:37 darkness wrote:
Show nested quote +
On February 19 2013 08:36 Abductedonut wrote:
On February 19 2013 08:28 darkness wrote:
I just want to express my appreciation of how much easier Java is than C. I also think I'm going to ask for help in the near future, haha. At least now I know why my C lecturer asks us to implement only math stuff. His major was math before switching to Computer Science. -.-

Edit: Ok, here's a simple problem.

Input two positive integers a and b (separated by one space). a and b can be very large (up to 99 digits). Output a+b. You are not allowed to use floating-point data types, including float, double, and long double.


Could you give a hint what to use or what to look for? Except for GMP (which may not be accepted by the Online Judge) and floating-point data types.


Keep in mind that your computer can only store 64-bit or 32-bit numbers. Your best bit is to store the numbers in an array and write the addition operation yourself. It's quite easy. Actually, I find this really interesting, so if you like I could write it for you.


Yeah, go ahead if you don't mind. I find C very challenging, sometimes even really hard because Java provides so much more support in my opinion. I try to learn it anyway.

Yeah, Java has significantly richer standart library, where is in C you have to write lots of stuff pretty much from zero. But once you have enough knowledge and experience with C, then it won't be more challenging than Java, and yet your apps will have much better performance.

On February 19 2013 09:23 Abductedonut wrote:
Show nested quote +
On February 19 2013 08:37 darkness wrote:
On February 19 2013 08:36 Abductedonut wrote:
On February 19 2013 08:28 darkness wrote:
I just want to express my appreciation of how much easier Java is than C. I also think I'm going to ask for help in the near future, haha. At least now I know why my C lecturer asks us to implement only math stuff. His major was math before switching to Computer Science. -.-

Edit: Ok, here's a simple problem.

Input two positive integers a and b (separated by one space). a and b can be very large (up to 99 digits). Output a+b. You are not allowed to use floating-point data types, including float, double, and long double.


Could you give a hint what to use or what to look for? Except for GMP (which may not be accepted by the Online Judge) and floating-point data types.


Keep in mind that your computer can only store 64-bit or 32-bit numbers. Your best bit is to store the numbers in an array and write the addition operation yourself. It's quite easy. Actually, I find this really interesting, so if you like I could write it for you.


Yeah, go ahead if you don't mind. I find C very challenging, sometimes even really hard because Java provides so much more support in my opinion. I try to learn it anyway.


This is by no means a pretty solution (I didn't include comments in the for loop that does the actual adding on purpose, variables are named stupidly, etc etc). Also, by no means is the code robust or error free. I didn't really test all the cases. I just wrote the program and verified that it kinda worked. It's up to you from here. Here you go.

Also, this code might go a little above your head. Sorry if it does!

+ Show Spoiler +

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[]
{
char a[100], b[100], temp[100], sum[101];
memset(sum,0,101);
scanf("%s",a);
scanf("%s",b);

//Swap the strings
if ( strlen(b) > strlen(a) )
{
strcpy(temp,b);
strcpy(b,a);
strcpy(a,temp);
}

// We could reuse temp but that's bad practice
char temp2[100];
strcpy(temp2,b);
//Fill b with zeros until strings are aligned
if( strlen(a) != strlen(b) )
{
int difference = strlen(a) - strlen(b);
memset(b,'0',difference);
strcpy(b+difference,temp2);
}

printf("A is: %s \n",a);
printf("B is: %s \n \n",b);
char carry = '0';
short int a_int = 0, b_int = 0, carry_int = 0;
char temp3[3];
temp3[0] = '\0';
temp3[1] = '\0';
temp3[2] = '\0';


for ( int i = 0; i < strlen(b); i++ )
{
a_int = a[strlen(a)-i-1] - '0'; //A small hack to convert a character to an int
b_int = b[strlen(b)-i-1] - '0';
carry_int = carry - '0';

sprintf(temp3,"%s%d",temp3,(a_int+b_int+carry_int));
sum[ strlen(a)-i-1 ] = (temp3[1] == NULL) ? temp3[0] : temp3[1];
carry = (temp3[1] != NULL) ? temp3[0] : '0';

temp3[0] = '\0';
temp3[1] = '\0';
}

printf("Sum is: %s \n",sum);
system("PAUSE");
return 0;
}

In his exercise numbers are supposed to be separated with space and if you try to sum 9 and 9 you'll end up with 8 instead of 18 ;P

Here's what I ended up with:
+ Show Spoiler +

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int sumNumberStrings(char *aStr, int aLen, char *bStr, int bLen, char *output);
void reverse(char *str);

int main(int argc, char *argv[])
{
char input[200];
char output[201];
char *delim;
int r;

gets(input);

delim = strchr(input, ' ');
if (delim == 0)
{
printf("bad string!\n");
return 1;
}

r = sumNumberStrings(input, delim - input, // number 1
delim + 1, strlen(delim + 1), // number 2
output);

if (r != 0) // sumNumberString failed - quit
{
printf("bad string!\n");
return 1;
}

printf("result: %s\n", output);
return 0;
}

int sumNumberStrings(char *a, int aLen, char *b, int bLen, char *output)
{
char *aEnd, *bEnd, *r;
int sum, carry, i;

sum = carry = i = 0;
aEnd = a + aLen - 1;
bEnd = b + bLen - 1;

while(aEnd >= a || bEnd >= b || sum != 0)
{
int aVal, bVal;

if ((aEnd >= a && !isdigit(*aEnd)) || (bEnd >= b && !isdigit(*bEnd))) return -1;

aVal = *aEnd - '0';
bVal = *bEnd - '0';

if (aEnd >= a) aEnd--;
else aVal = 0;
if (bEnd >= b) bEnd--;
else bVal = 0;

sum = aVal + bVal;

output[i++] = (sum + carry) % 10 + '0';
carry = (sum + carry) / 10;
}

if (output[i-1] == '0') i--;
output[i] = '\0';

// reverse output string
i--;

for (r = output; r < output + i; r++, i--)
{
char tmp = output[i];
output[i] = *r;
*r = tmp;
}

return 0;
}


I'm only studying C as well, so please do critique. By the way, darkness, should this program be able to sum floating point numbers? Anyway, adding such functionality would a be great exercise for you!
phar
Profile Joined August 2011
United States1080 Posts
Last Edited: 2013-02-19 02:29:42
February 19 2013 02:24 GMT
#5003
On February 19 2013 11:15 tuho12345 wrote:

Hi everyone, I'm a newbie in programming and Java specifically. I had a little experience with VB, which is significantly easier than Java or C I guess. So I'm kinda stuck right here and wish I'd have more time but it's due tomorrow and I'm struggling with other exams as well.

Here below is my code, and my question is how can I calculate the SalesTotal and SalesCount. I understand the logic but I have no idea how Java works.
Thanks

+ Show Spoiler +
/**
Assignment 2, CS49J, Spring 2013
Part B student
A cash register totals up sales and computes change due.
*/
public class CashRegister
{
private double purchase;
private double payment;

//... Add your code here
private double SalesTotal;
private int SalesCount;
/**
Constructs a cash register with no money in it.
*/
public CashRegister()
{
purchase = 0;
payment = 0;
//...Add your code here
SalesCount = 0;
SalesTotal = 0;
}

/**
Records the sale of an item.
@param amount the price of the item
*/
public void recordPurchase(double amount)
{
double total = purchase + amount;
//... Add your code here
//SalesTotal += total;
purchase = total;
}

/**
Enters the payment received from the customer.
@param amount the amount of the payment
*/
public void enterPayment(double amount)
{
payment = amount;
}

/**
Computes the change due and resets the machine for the next customer.
@return the change due to the customer
*/
public double giveChange()
{
double change = payment - purchase;
purchase = 0;
payment = 0;
return change;
}

/**
Get the total amount of all sales for the day.
@param return sale total
*/
//... Add your code here
public double getSalesTotal()
{

SalesTotal = SalesTotal + purchase;
return SalesTotal;

}



/**
Get the total number of sales for the day.
@param return the number of sales.
*/
//... Add your code here
public double getSalesCount()
{

return SalesCount;
}
/**
Reset counters and totals for the next days sales.
@param none
*/
//... Add your code here
public void reset()
{
SalesCount = 0;
payment = 0;
purchase = 0;
SalesTotal = 0;
}
}

In the future when posting code, please use proper indentation so that we can read it more easily.

Bad:
int foo() {
int bar;
return bar + 1;
}


Good:
int foo() {
int bar;
return bar + 1;
}



Also I'm not clear on exactly what you're asking. What are you trying to do? What part are you confused on? Do you know how a variable inside a class works?
Who after all is today speaking about the destruction of the Armenians?
tuho12345
Profile Blog Joined July 2011
4482 Posts
February 19 2013 02:48 GMT
#5004
On February 19 2013 11:24 phar wrote:
Show nested quote +
On February 19 2013 11:15 tuho12345 wrote:

Hi everyone, I'm a newbie in programming and Java specifically. I had a little experience with VB, which is significantly easier than Java or C I guess. So I'm kinda stuck right here and wish I'd have more time but it's due tomorrow and I'm struggling with other exams as well.

Here below is my code, and my question is how can I calculate the SalesTotal and SalesCount. I understand the logic but I have no idea how Java works.
Thanks

+ Show Spoiler +
/**
Assignment 2, CS49J, Spring 2013
Part B student
A cash register totals up sales and computes change due.
*/
public class CashRegister
{
private double purchase;
private double payment;

//... Add your code here
private double SalesTotal;
private int SalesCount;
/**
Constructs a cash register with no money in it.
*/
public CashRegister()
{
purchase = 0;
payment = 0;
//...Add your code here
SalesCount = 0;
SalesTotal = 0;
}

/**
Records the sale of an item.
@param amount the price of the item
*/
public void recordPurchase(double amount)
{
double total = purchase + amount;
//... Add your code here
//SalesTotal += total;
purchase = total;
}

/**
Enters the payment received from the customer.
@param amount the amount of the payment
*/
public void enterPayment(double amount)
{
payment = amount;
}

/**
Computes the change due and resets the machine for the next customer.
@return the change due to the customer
*/
public double giveChange()
{
double change = payment - purchase;
purchase = 0;
payment = 0;
return change;
}

/**
Get the total amount of all sales for the day.
@param return sale total
*/
//... Add your code here
public double getSalesTotal()
{

SalesTotal = SalesTotal + purchase;
return SalesTotal;

}



/**
Get the total number of sales for the day.
@param return the number of sales.
*/
//... Add your code here
public double getSalesCount()
{

return SalesCount;
}
/**
Reset counters and totals for the next days sales.
@param none
*/
//... Add your code here
public void reset()
{
SalesCount = 0;
payment = 0;
purchase = 0;
SalesTotal = 0;
}
}

In the future when posting code, please use proper indentation so that we can read it more easily.

Bad:
int foo() {
int bar;
return bar + 1;
}


Good:
int foo() {
int bar;
return bar + 1;
}



Also I'm not clear on exactly what you're asking. What are you trying to do? What part are you confused on? Do you know how a variable inside a class works?

Sorry, I just edited. My question is I'm trying to total the amount of money at the end of the day. It called SalesTotal or getSalesTotal method, I don't know how to add up the total amount of money after all that transaction. I can't use the tester tool to print it out.

public double getSalesTotal()
{

      SalesTotal += purchase ;
      return SalesTotal;

}
phar
Profile Joined August 2011
United States1080 Posts
February 19 2013 03:55 GMT
#5005
On February 19 2013 11:48 tuho12345 wrote:
Show nested quote +
On February 19 2013 11:24 phar wrote:
On February 19 2013 11:15 tuho12345 wrote:

Hi everyone, I'm a newbie in programming and Java specifically. I had a little experience with VB, which is significantly easier than Java or C I guess. So I'm kinda stuck right here and wish I'd have more time but it's due tomorrow and I'm struggling with other exams as well.

Here below is my code, and my question is how can I calculate the SalesTotal and SalesCount. I understand the logic but I have no idea how Java works.
Thanks

+ Show Spoiler +
/**
Assignment 2, CS49J, Spring 2013
Part B student
A cash register totals up sales and computes change due.
*/
public class CashRegister
{
private double purchase;
private double payment;

//... Add your code here
private double SalesTotal;
private int SalesCount;
/**
Constructs a cash register with no money in it.
*/
public CashRegister()
{
purchase = 0;
payment = 0;
//...Add your code here
SalesCount = 0;
SalesTotal = 0;
}

/**
Records the sale of an item.
@param amount the price of the item
*/
public void recordPurchase(double amount)
{
double total = purchase + amount;
//... Add your code here
//SalesTotal += total;
purchase = total;
}

/**
Enters the payment received from the customer.
@param amount the amount of the payment
*/
public void enterPayment(double amount)
{
payment = amount;
}

/**
Computes the change due and resets the machine for the next customer.
@return the change due to the customer
*/
public double giveChange()
{
double change = payment - purchase;
purchase = 0;
payment = 0;
return change;
}

/**
Get the total amount of all sales for the day.
@param return sale total
*/
//... Add your code here
public double getSalesTotal()
{

SalesTotal = SalesTotal + purchase;
return SalesTotal;

}



/**
Get the total number of sales for the day.
@param return the number of sales.
*/
//... Add your code here
public double getSalesCount()
{

return SalesCount;
}
/**
Reset counters and totals for the next days sales.
@param none
*/
//... Add your code here
public void reset()
{
SalesCount = 0;
payment = 0;
purchase = 0;
SalesTotal = 0;
}
}

In the future when posting code, please use proper indentation so that we can read it more easily.

Bad:
int foo() {
int bar;
return bar + 1;
}


Good:
int foo() {
int bar;
return bar + 1;
}



Also I'm not clear on exactly what you're asking. What are you trying to do? What part are you confused on? Do you know how a variable inside a class works?

Sorry, I just edited. My question is I'm trying to total the amount of money at the end of the day. It called SalesTotal or getSalesTotal method, I don't know how to add up the total amount of money after all that transaction. I can't use the tester tool to print it out.

Show nested quote +
public double getSalesTotal()
{

      SalesTotal += purchase ;
      return SalesTotal;

}

Do you know how variables inside a class work?

public class Foo {
private int fooVal;

public void setFoo(int val) {
this.fooVal = val;
}

public int getFoo() {
return foo;
}

public static blahblah main(more blah) {
Foo foo = new Foo();
foo.setFoo(5);
System.out.println("Number is: " + foo.getFoo()); // Prints 5;
foo.setFoo(7);
System.out.println("Number is: " + foo.getFoo()); // Prints 7;
}
Who after all is today speaking about the destruction of the Armenians?
tuho12345
Profile Blog Joined July 2011
4482 Posts
February 19 2013 05:45 GMT
#5006
On February 19 2013 12:55 phar wrote:
Show nested quote +
On February 19 2013 11:48 tuho12345 wrote:
On February 19 2013 11:24 phar wrote:
On February 19 2013 11:15 tuho12345 wrote:

Hi everyone, I'm a newbie in programming and Java specifically. I had a little experience with VB, which is significantly easier than Java or C I guess. So I'm kinda stuck right here and wish I'd have more time but it's due tomorrow and I'm struggling with other exams as well.

Here below is my code, and my question is how can I calculate the SalesTotal and SalesCount. I understand the logic but I have no idea how Java works.
Thanks

+ Show Spoiler +
/**
Assignment 2, CS49J, Spring 2013
Part B student
A cash register totals up sales and computes change due.
*/
public class CashRegister
{
private double purchase;
private double payment;

//... Add your code here
private double SalesTotal;
private int SalesCount;
/**
Constructs a cash register with no money in it.
*/
public CashRegister()
{
purchase = 0;
payment = 0;
//...Add your code here
SalesCount = 0;
SalesTotal = 0;
}

/**
Records the sale of an item.
@param amount the price of the item
*/
public void recordPurchase(double amount)
{
double total = purchase + amount;
//... Add your code here
//SalesTotal += total;
purchase = total;
}

/**
Enters the payment received from the customer.
@param amount the amount of the payment
*/
public void enterPayment(double amount)
{
payment = amount;
}

/**
Computes the change due and resets the machine for the next customer.
@return the change due to the customer
*/
public double giveChange()
{
double change = payment - purchase;
purchase = 0;
payment = 0;
return change;
}

/**
Get the total amount of all sales for the day.
@param return sale total
*/
//... Add your code here
public double getSalesTotal()
{

SalesTotal = SalesTotal + purchase;
return SalesTotal;

}



/**
Get the total number of sales for the day.
@param return the number of sales.
*/
//... Add your code here
public double getSalesCount()
{

return SalesCount;
}
/**
Reset counters and totals for the next days sales.
@param none
*/
//... Add your code here
public void reset()
{
SalesCount = 0;
payment = 0;
purchase = 0;
SalesTotal = 0;
}
}

In the future when posting code, please use proper indentation so that we can read it more easily.

Bad:
int foo() {
int bar;
return bar + 1;
}


Good:
int foo() {
int bar;
return bar + 1;
}



Also I'm not clear on exactly what you're asking. What are you trying to do? What part are you confused on? Do you know how a variable inside a class works?

Sorry, I just edited. My question is I'm trying to total the amount of money at the end of the day. It called SalesTotal or getSalesTotal method, I don't know how to add up the total amount of money after all that transaction. I can't use the tester tool to print it out.

public double getSalesTotal()
{

      SalesTotal += purchase ;
      return SalesTotal;

}

Do you know how variables inside a class work?

public class Foo {
private int fooVal;

public void setFoo(int val) {
this.fooVal = val;
}

public int getFoo() {
return foo;
}

public static blahblah main(more blah) {
Foo foo = new Foo();
foo.setFoo(5);
System.out.println("Number is: " + foo.getFoo()); // Prints 5;
foo.setFoo(7);
System.out.println("Number is: " + foo.getFoo()); // Prints 7;
}

yes, i see the difference between Static and non static.
phar
Profile Joined August 2011
United States1080 Posts
Last Edited: 2013-02-19 05:53:56
February 19 2013 05:52 GMT
#5007
Sort of... static method and static variable mean slightly different things though:


public class Foo {
private static int fooVal; // shared between all instances of Foo

public void setFoo(int val) {
this.fooVal = val;
}

public int getFoo() {
return foo;
}

public static blahblah main(more blah) {
Foo foo = new Foo();
Foo other = new Foo();
foo.setFoo(5);
System.out.println("Number is: " + foo.getFoo()); // Prints 5;
System.out.println("Number is: " + other.getFoo()); // Prints 5;
other.setFoo(7);
System.out.println("Number is: " + foo.getFoo()); // Prints 7;
System.out.println("Number is: " + other.getFoo()); // Prints 7;
}


You shouldn't need a static variable if you only have one instance of whatever class it is you're using.
Who after all is today speaking about the destruction of the Armenians?
Wry
Profile Joined October 2011
Canada25 Posts
Last Edited: 2013-02-19 06:10:28
February 19 2013 06:04 GMT
#5008
On February 19 2013 11:15 tuho12345 wrote:
Hi everyone, I'm a newbie in programming and Java specifically. I had a little experience with VB, which is significantly easier than Java or C I guess. So I'm kinda stuck right here and wish I'd have more time but it's due tomorrow and I'm struggling with other exams as well.

Here below is my code, and my question is how can I calculate the SalesTotal and SalesCount. I understand the logic but I have no idea how Java works.
Thanks

+ Show Spoiler +



/**
Assignment 2, CS49J, Spring 2013
Part B student
A cash register totals up sales and computes change due.
*/
public class CashRegister
{
      private double purchase;
      private double payment;

//... Add your code here
      private double SalesTotal;
      private int SalesCount;
/**
Constructs a cash register with no money in it.
*/
public CashRegister()
{
       purchase = 0;
       payment = 0;
      SalesCount = 0;
       SalesTotal = 0;
}

/**
Records the sale of an item.
@param amount the price of the item
*/
public void recordPurchase(double amount)
{
      double total = purchase + amount;
//... Add your code here
       purchase = total;

}

/**
Enters the payment received from the customer.
@param amount the amount of the payment
*/
public void enterPayment(double amount)
{
      payment = amount;
      SalesCount++;
}

/**
Computes the change due and resets the machine for the next customer.
@return the change due to the customer
*/
public double giveChange()
{
      double change = payment - purchase;
      purchase = 0;
       payment = 0;
      return change;
}

/**
Get the total amount of all sales for the day.
@param return sale total
*/
//... Add your code here
public double getSalesTotal()
{

      SalesTotal = purchase + purchase;
      return SalesTotal;

}



/**
Get the total number of sales for the day.
@param return the number of sales.
*/
//... Add your code here

public double getSalesCount()
{
      return SalesCount;
}
/**
Reset counters and totals for the next days sales.
@param none
*/
//... Add your code here
public void reset()
{
      SalesCount = 0;
      payment = 0;
      purchase = 0;
      SalesTotal = 0;
}
}



Not completely sure what SalesTotal is, but I am assuming it means the total amount of all the purchases? (eg. 3 customers buy items worth $3.00, $2.50 and $1.75, SalesTotal = 6.25 and SalesCount = 3).

First, when creating 'getter' methods, such as getSalesTotal(), you should only really be returning the value you desire and not calculating it in that method. A more logical way to do it would be to add the purchase amount to the sales total as you receive payment or record purchases.

Also, you may want to think of some error cases because it seems that a customer can input amounts less than the item cost and your register will accept that.

Ex. Item worth $100:
recordPurchase(100)
enterPayment(50)
giveChange();

= You lose $50.

Edit: unless the main handles that, though you might want to specify that in comments. (now that I look at it again, it's not completely nonsensical with a main function that handles the register).
tuho12345
Profile Blog Joined July 2011
4482 Posts
February 19 2013 06:22 GMT
#5009
On February 19 2013 15:04 Wry wrote:
Show nested quote +
On February 19 2013 11:15 tuho12345 wrote:
Hi everyone, I'm a newbie in programming and Java specifically. I had a little experience with VB, which is significantly easier than Java or C I guess. So I'm kinda stuck right here and wish I'd have more time but it's due tomorrow and I'm struggling with other exams as well.

Here below is my code, and my question is how can I calculate the SalesTotal and SalesCount. I understand the logic but I have no idea how Java works.
Thanks

+ Show Spoiler +



/**
Assignment 2, CS49J, Spring 2013
Part B student
A cash register totals up sales and computes change due.
*/
public class CashRegister
{
      private double purchase;
      private double payment;

//... Add your code here
      private double SalesTotal;
      private int SalesCount;
/**
Constructs a cash register with no money in it.
*/
public CashRegister()
{
       purchase = 0;
       payment = 0;
      SalesCount = 0;
       SalesTotal = 0;
}

/**
Records the sale of an item.
@param amount the price of the item
*/
public void recordPurchase(double amount)
{
      double total = purchase + amount;
//... Add your code here
       purchase = total;

}

/**
Enters the payment received from the customer.
@param amount the amount of the payment
*/
public void enterPayment(double amount)
{
      payment = amount;
      SalesCount++;
}

/**
Computes the change due and resets the machine for the next customer.
@return the change due to the customer
*/
public double giveChange()
{
      double change = payment - purchase;
      purchase = 0;
       payment = 0;
      return change;
}

/**
Get the total amount of all sales for the day.
@param return sale total
*/
//... Add your code here
public double getSalesTotal()
{

      SalesTotal = purchase + purchase;
      return SalesTotal;

}



/**
Get the total number of sales for the day.
@param return the number of sales.
*/
//... Add your code here

public double getSalesCount()
{
      return SalesCount;
}
/**
Reset counters and totals for the next days sales.
@param none
*/
//... Add your code here
public void reset()
{
      SalesCount = 0;
      payment = 0;
      purchase = 0;
      SalesTotal = 0;
}
}



Not completely sure what SalesTotal is, but I am assuming it means the total amount of all the purchases? (eg. 3 customers buy items worth $3.00, $2.50 and $1.75, SalesTotal = 6.25 and SalesCount = 3).

First, when creating 'getter' methods, such as getSalesTotal(), you should only really be returning the value you desire and not calculating it in that method. A more logical way to do it would be to add the purchase amount to the sales total as you receive payment or record purchases.

Also, you may want to think of some error cases because it seems that a customer can input amounts less than the item cost and your register will accept that.

Ex. Item worth $100:
recordPurchase(100)
enterPayment(50)
giveChange();

= You lose $50.

Edit: unless the main handles that, though you might want to specify that in comments. (now that I look at it again, it's not completely nonsensical with a main function that handles the register).

Thank you Wry, it's so simple haha. I made that change to the SalesCount but not the SalesTotal, so silly.
Oaky
Profile Joined August 2012
United States95 Posts
February 19 2013 06:57 GMT
#5010
Im curious how long, and how much effort, would it take to get relatively competent at javascript/php or some other web dev. language so i could offer to redo parts of my schools hideous website. I am intermediate with python.
SOOOOOOO MANY BANELINGS!
obesechicken13
Profile Blog Joined July 2008
United States10467 Posts
Last Edited: 2013-02-19 07:16:09
February 19 2013 07:15 GMT
#5011
On February 19 2013 15:57 Oaky wrote:
Im curious how long, and how much effort, would it take to get relatively competent at javascript/php or some other web dev. language so i could offer to redo parts of my schools hideous website. I am intermediate with python.

Making sites look good is more about graphic design than html or css imo.

What prior experience do you have?
I think in our modern age technology has evolved to become more addictive. The things that don't give us pleasure aren't used as much. Work was never meant to be fun, but doing it makes us happier in the long run.
Oaky
Profile Joined August 2012
United States95 Posts
February 19 2013 07:21 GMT
#5012
On February 19 2013 16:15 obesechicken13 wrote:
Show nested quote +
On February 19 2013 15:57 Oaky wrote:
Im curious how long, and how much effort, would it take to get relatively competent at javascript/php or some other web dev. language so i could offer to redo parts of my schools hideous website. I am intermediate with python.

Making sites look good is more about graphic design than html or css imo.

What prior experience do you have?

Pretty minimal, been teaching myself python for a few weeks.
SOOOOOOO MANY BANELINGS!
AmericanUmlaut
Profile Blog Joined November 2010
Germany2597 Posts
February 19 2013 08:34 GMT
#5013
On February 19 2013 15:57 Oaky wrote:
Im curious how long, and how much effort, would it take to get relatively competent at javascript/php or some other web dev. language so i could offer to redo parts of my schools hideous website. I am intermediate with python.

That kind of depends on what you mean by "relatively competent." If you just want to make a nice-looking website, you really don't need to go crazy with Javascript and PHP - your PHP just needs to generate decent HTML, you'll want to use CSS to make your layout look nice, and Javascript is probably only necessary if you want to animate something, which is pretty easy if you just use jQuery (which has become pretty much the standard Javascript library, so it's very useful to be familiar with).

Without looking at your webpage, I can only go on what I've seen in other beginners, but I would guess that you're having two problems: Your HTML is likely not well organized, and you either don't have a good sense of what makes a page look nice or you don't have a strong enough grasp of CSS to get the effect that you want. Without seeing code it's hard to help with those things, but if you want to post a link to what you're working on, or a chunk of code that's giving you trouble, then I'd be happy to take a look.
The frumious Bandersnatch
Shield
Profile Blog Joined August 2009
Bulgaria4824 Posts
Last Edited: 2013-02-20 15:16:06
February 19 2013 22:01 GMT
#5014
Hey guys, I need a bit of help for C.

What I expect to do:

I'm trying to implement ROT13: http://en.wikipedia.org/wiki/ROT13
Every char (int in this case) is either incremented 13 digits or decremented based on conditions. Then, I'm trying to copy each character to 'string' which I output in the end.

What happens:
I get a 'Segmentation fault' and also these warnings:
test.c: In function Б─≤rot13Б─≥:
test.c:12: warning: passing argument 2 of Б─≤strcatБ─≥ makes pointer from integer without a cast
/usr/include/string.h:136: note: expected Б─≤const char * __restrict__Б─≥ but argument is of type Б─≤charБ─≥
test.c:16: warning: passing argument 2 of Б─≤strcatБ─≥ makes pointer from integer without a cast
/usr/include/string.h:136: note: expected Б─≤const char * __restrict__Б─≥ but argument is of type Б─≤charБ─≥
test.c:19: warning: passing argument 2 of Б─≤strcatБ─≥ makes pointer from integer without a cast
/usr/include/string.h:136: note: expected Б─≤const char * __restrict__Б─≥ but argument is of type Б─≤charБ─≥




tofucake
Profile Blog Joined October 2009
Hyrule19228 Posts
February 19 2013 22:03 GMT
#5015
you declare int c

then compare int c to several const chars (for one)

Another: you need to cast to a pointer to a character, not to a character.
Liquipediaasante sana squash banana
Shield
Profile Blog Joined August 2009
Bulgaria4824 Posts
February 19 2013 22:11 GMT
#5016
On February 20 2013 07:03 tofucake wrote:
you declare int c

then compare int c to several const chars (for one)

Another: you need to cast to a pointer to a character, not to a character.


Do you mean this comparison: (c >= 'A' && c <= 'M') || (c >= 'a' && c <= 'M')? If yes, I think it's perfectly fine with gcc.
I'm not sure how to do the 2nd thing you mentioned though. Pointers are still new to me.
InRaged
Profile Joined February 2007
1047 Posts
February 19 2013 22:19 GMT
#5017
Casting to pointer won't solve problem.

strcat.
strcat expects zero-terminated string for both of its arguments.
You give it int, it tries to follow the adress inside int, which is obviously invalid, and so you get segfault.

You need some temp string:
char *tmp = "a";
Then you put your adjusted char there
tmp[0] = c;
and then pass it to strcat
strcat(string, tmp);
Shield
Profile Blog Joined August 2009
Bulgaria4824 Posts
Last Edited: 2013-02-20 15:16:37
February 19 2013 22:28 GMT
#5018
On February 20 2013 07:19 InRaged wrote:
Casting to pointer won't solve problem.

strcat.
strcat expects zero-terminated string for both of its arguments.
You give it int, it tries to follow the adress inside int, which is obviously invalid, and so you get segfault.

You need some temp string:
char *tmp = "a";
Then you put your adjusted char there
tmp[0] = c;
and then pass it to strcat
strcat(string, tmp);


It doesn't give warnings now, but it still says:
-bash-4.1$ ./a.out
abcd
Segmentation fault

delHospital
Profile Blog Joined December 2010
Poland261 Posts
February 19 2013 22:43 GMT
#5019
On February 20 2013 07:19 InRaged wrote:
Casting to pointer won't solve problem.

strcat.
strcat expects zero-terminated string for both of its arguments.
You give it int, it tries to follow the adress inside int, which is obviously invalid, and so you get segfault.

You need some temp string:
char *tmp = "a";
Then you put your adjusted char there
tmp[0] = c;
and then pass it to strcat
strcat(string, tmp);

I'd recommend
char tmp[] = "a";
instead of
char *tmp = "a";

The first one allocates a 2 character array, which is just what we need, while the second one allocates a pointer to a string constant, which could be stored in read-only memory.

Other than that, this should work, but also keep in mind that your "char string[SIZE]" array may contain garbage at the start, which would cause problems with strcat. The easiest way to clear it is (google to see what it does):
memset(string, 0, SIZE);

Also, your program will explode if it receives more than 99 characters.
InRaged
Profile Joined February 2007
1047 Posts
Last Edited: 2013-02-19 23:16:35
February 19 2013 22:58 GMT
#5020
Thank you, delHospital. Should be char tmp[];

darkness, If you can use strncat instead of strcat then instead of tmp variable you could do
strncat(string, (char *)&c, 1);
The casting is required in such case, cause your c is int and not char (but it isn't as portable, see Mstring's responce). Last argument tells strncat how much bytes to copy.

Also there is a bug in your algorithm. Check your if conditions to find it ;P
Prev 1 249 250 251 252 253 1032 Next
Please log in or register to reply.
Live Events Refresh
Epic.LAN
13:00
Epic.LAN 48 Playoff Stage
epiclan23
Liquipedia
WardiTV Invitational
12:00
Summer Cup 2026 - Group A
WardiTV785
LiquipediaDiscussion
CranKy Ducklings
10:00
Master Swan Open #105
CranKy Ducklings58
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
Lowko528
Rex 109
BRAT_OK 101
MindelVK 27
RushiSC 22
StarCraft: Brood War
Calm 8092
Rain 2270
Shuttle 1994
Jaedong 1206
Hyuk 962
firebathero 743
Mini 638
BeSt 524
Horang2 513
Larva 211
[ Show more ]
Stork 211
Rush 204
ZerO 160
hero 133
Hyun 119
Last 118
Sharp 105
Pusan 99
ggaemo 95
JulyZerg 64
JYJ 56
Killer 49
Mong 49
Free 43
sorry 37
soO 36
Movie 29
Shine 27
Aegong 25
HiyA 23
ToSsGirL 17
Hm[arnc] 17
NaDa 14
yabsab 13
Barracks 12
IntoTheRainbow 9
Noble 9
ajuk12(nOOB) 8
Rock 7
Purpose 6
Icarus 5
Dota 2
Gorgc5704
XcaliburYe198
League of Legends
Doublelift3611
Counter-Strike
byalli1695
allub430
Heroes of the Storm
Khaldor136
Trikslyr14
Other Games
gofns20204
singsing1912
B2W.Neo814
RotterdaM196
Hui .136
XaKoH 127
DeMusliM126
ZerO(Twitch)13
BlysK5
Organizations
Other Games
gamesdonequick2306
BasetradeTV169
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
[ Show 15 non-featured ]
StarCraft 2
• CranKy Ducklings SOOP79
• musti20045 18
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• Pr0nogo 4
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Dota 2
• C_a_k_e 540
League of Legends
• Jankos3329
Upcoming Events
IPSL
2h 23m
Dragon vs Hawk
RSL Revival
19h 23m
Classic vs Trap
herO vs SHIN
Sparkling Tuna Cup
20h 23m
OSC
23h 23m
IPSL
1d 2h
Bonyth vs Ret
WardiTV Weekly
1d 21h
Monday Night Weeklies
2 days
PiGosaur Cup
3 days
The PondCast
3 days
Replay Cast
4 days
[ Show More ]
CrankTV Team League
4 days
Replay Cast
5 days
CrankTV Team League
5 days
Korean StarCraft League
6 days
RSL Revival
6 days
Liquipedia Results

Completed

Escore Tournament S3: W3
HSC XXIX
Eternal Conflict S2 E2

Ongoing

IPSL Spring 2026
Acropolis #4
YSL S3
CSL 2026 Summer (S21)
KCM Race Survival 2026 Season 3
RSL Revival: Season 6
CranK Gathers Season 4: BW vs SC2 Team League
SCTL 2026 Spring
Stake Ranked Episode 3
XSE Pro League 2026
IEM Cologne Major 2026
Stake Ranked Episode 2
CS Asia Championships 2026
Asian Champions League 2026
IEM Atlanta 2026
PGL Astana 2026

Upcoming

Escore Tournament S3: W4
ASL S22 SEASON OPEN Day 2
Escore Tournament S3: W5
CSLAN 4
Blizzard Classic Cup 2026
HSC XXX
SC4ALL II: StarCraft II
Kung Fu Cup 2026 Grand Finals
Light Tournament 2026
Eternal Conflict S2 Finale
Eternal Conflict S2 E3
Logitech G Connect 2026
StarSeries Fall 2026
FISSURE Playground #5
BLAST Open Fall 2026
Esports World Cup 2026
BLAST Bounty Summer 2026
BLAST Bounty Summer Qual
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.