9 Replies - 615 Views - Last Post: 31 October 2011 - 02:09 PM Rate Topic: -----

#1 RyanSorkhpoosh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 18-October 11

finding years, days & months from days given, And winning %

Posted 28 October 2011 - 12:09 PM

I'm working on 2 programs and I have most of the code down, but there is either a small error, or a missing line of code in each one and i cant figure out what it is. For my first program that i'm writing, it is:

import javax.swing.JOptionPane;
import java.text.NumberFormat;

public class Program4
{
	public static void main(String[] args)
	{
		String input;
		int won;
		int lost;
		double perc;
		
		input = JOptionPane.showInputDialog("How many games were won");
		won= Integer.parseInt(input);
		input = JOptionPane.showInputDialog("How many games were lost");
		lost = Integer.parseInt(input);
		perc = won + lost / won;
		
		System.out.println("The teams winning percentage is ");
		NumberFormat formatter = NumberFormat.getNumberInstance();
		formatter.setMaximumFractionDigits(3);
		formatter.setMinimumFractionDigits(3);
		System.out.println(formatter.format(perc));
		System.exit(0); 
	}
}

The output comes out as something like 8.00 or 3.00, when it should be .800 and .300
My second program, I want it to output my age in years months and days:
import javax.swing.JOptionPane;

public class Program10
{
	public static void main(String[] args)

	{
		final int MONTH = 30;
		final int YEAR = 365;

		String input;
		int days;
		int years;
		int months;
		input = JOptionPane.showInputDialog("Whats your age in days");
		daays = Integer.parseInt(input);
		years = 
		days = MONTH * years;
		months = years / 12;

		JOptionPane.showMessageDialog(null, years + " years " + months + " months and " + days + " days" );
	}

}


Im not looking for the exact code for someone to give me, but some help or hints would be nice. I think the issue is that I have the correct code written, but I am missing or have incorrect formulas for each one. Could someone provide me with assistance. Thank you.

This post has been edited by smohd: 28 October 2011 - 02:31 PM
Reason for edit:: Title edited to be more descriptive


Is This A Good Question/Topic? 0
  • +

Replies To: finding years, days & months from days given, And winning %

#2 CapC  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 30
  • Joined: 27-September 11

Re: finding years, days & months from days given, And winning %

Posted 28 October 2011 - 01:14 PM

There is quite a bit that is undesirable here...
1) Make a different class and make the main method a driver only. Call the other class from the driver / test class. (this is better programming practice imo)
2) Your logic with the perc is incorrect. Consider if the player won only 1 game, or none at all. 0 wins 5 losses. Your logic says 0+5/0 = perc but you can not divide by 0. or 1+5/5 = perc, 6/5 = 120%.

Instead try something like :
IF (won = 0) then perc = 0
else if (won >=1)
perc = won  / won + lost;


That should give you a start on the first one.

This post has been edited by smohd: 28 October 2011 - 01:50 PM
Reason for edit:: code tags added

Was This Post Helpful? 0
  • +
  • -

#3 CapC  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 30
  • Joined: 27-September 11

Re: finding years, days & months from days given, And winning %

Posted 28 October 2011 - 01:20 PM

Sorry got interrupted at my desk and posted before reviewing and I can't edit my posts but... proper syntax:

{
     if (won <= 0) {
          then perc = 0;
     } else {
          perc = won / won + lost;
     } 
}


Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: finding years, days & months from days given, And winning %

Posted 28 October 2011 - 01:48 PM

This line perc = won / won + lost; is like saying perc = 1 + lost;! It should be
  perc = won /( won + lost); //you will get the fraction by the way, if you need percentage, you have to multiply by 100
and no need to check for won for zero because we are not dividing by won but (won + lost).

Quote

My second program, I want it to output my age in years months and days:

So what is the problem with it? wrong output or what?
Also look at this part of your code:
input = JOptionPane.showInputDialog("Whats your age in days");
		daays = Integer.parseInt(input);
		years = //what is going on here?
		days = MONTH * years;
		months = years / 12;

From what I see, you need to convert days entered into years, month and days(using the assumption you made of 365 and 30 days for year and month resp). This is just simple
- to get number of years, take days entered and divide them by YEAR
- to get months, take the remainder of above calculation(days entered % YEAR) and divide it by MONTH.
- And to get the days, is just the remainder of the second one>>>
Was This Post Helpful? 1
  • +
  • -

#5 CapC  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 30
  • Joined: 27-September 11

Re: finding years, days & months from days given, And winning %

Posted 28 October 2011 - 01:56 PM

View Postsmohd, on 28 October 2011 - 01:48 PM, said:

and no need to check for won for zero because we are not dividing by won but (won + lost).


My intentions of
 {
     if (won <= 0) {
          then perc = 0;
     }}
was not to check for 0 -- as you are correct, we are dividing for won+lost but if the user entered -8 for whatever reason -8 wins and 5 losses would result in (-8/-3) instead of (8/13).

This post has been edited by smohd: 28 October 2011 - 02:14 PM
Reason for edit:: Code tags fixed

Was This Post Helpful? 0
  • +
  • -

#6 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: finding years, days & months from days given, And winning %

Posted 28 October 2011 - 02:12 PM

Sure you can test for that, my eyes go to when you said if(won = 0) in the first post(and we have no Then in java ;) ).
Was This Post Helpful? 0
  • +
  • -

#7 CapC  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 30
  • Joined: 27-September 11

Re: finding years, days & months from days given, And winning %

Posted 28 October 2011 - 02:17 PM

View Postsmohd, on 28 October 2011 - 02:12 PM, said:

my eyes go to when you said if(won = 0) in the first post(and we have no Then in java ;) ).


Haha yes absolutely. For whatever reasons I can't edit my posts using FF at my work pc. My apologize or that would have been correct to be less confusing.
Was This Post Helpful? 0
  • +
  • -

#8 RyanSorkhpoosh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 18-October 11

Re: finding years, days & months from days given, And winning %

Posted 31 October 2011 - 12:05 PM

Sorry for the late reply. I was busy with some stuff. Anyways, i put in that formula you told me, which was perc = won /( won + lost);
, but it does not seem to work. Maybe I misread what you said, but the output always comes out as 0.000, does not matter what i type into the input anymore. this is what the code looks like now.
import javax.swing.JOptionPane;
import java.text.NumberFormat;

public class Program4
{
	public static void main(String[] args)
	{
		String input;
		int won;
		int lost;
		double perc;
		
		input = JOptionPane.showInputDialog("How many games were won");
		won= Integer.parseInt(input);
		input = JOptionPane.showInputDialog("How many games were lost");
		lost = Integer.parseInt(input);
		perc = won / (won + lost) * 100;




		
		System.out.println("The teams winning percentage is ");
		NumberFormat formatter = NumberFormat.getNumberInstance();
		formatter.setMaximumFractionDigits(3);
		formatter.setMinimumFractionDigits(3);
		System.out.println(formatter.format(perc));
		System.exit(0); 
			
	}
}

Is there any fix because i still cant figure it out at this point. Again, instead of getting outputs like 8.000 or 16.000, i want 0.800, etc. Can anyone give the correct formula because i think thats the only issue theprogram has.
Was This Post Helpful? 0
  • +
  • -

#9 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: finding years, days & months from days given, And winning %

Posted 31 October 2011 - 12:21 PM

This line will give you the percentage in %, like 80% and so.perc = won / (won + lost) * 100;
If you need it in decimal, then remove * 100, so 80% => 0.8
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: finding years, days & months from days given, And winning %

Posted 31 October 2011 - 02:09 PM

View PostRyanSorkhpoosh, on 28 October 2011 - 03:09 PM, said:

The output comes out as something like 8.00 or 3.00, when it should be .800 and .300

You will have to divide perc by 100.0 if you want a percentage :)

months = days / 30;
years = months / 12;
months %= 12;


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1