The Artima Developer Community
Sponsored Link

Java Answers Forum
Java Assignment Help (Urgent!)

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Jeff

Posts: 2
Nickname: daous
Registered: Jan, 2003

Java Assignment Help (Urgent!) Posted: Jan 8, 2003 5:36 PM
Reply to this message Reply
Advertisement
Hi, I'm having some major problems with the following question of a java assignment. For some reason, the program I wrote works for some values, but not for others. The program works fine for sets of values such as (second number is the divisor): 4 and 2, 1 and 2, 3 and 4, 4 and 7
Some of the many sets of values that don't work include:
1 and 100, 5 and 7 (a weird one,this one should've worked..), 4 and 5.
Modifying my program (ie fixing it) would be greatly appreciated. Thanks in advance. If at all possible, an explanation of what was changed would also be appreciated. (If you have enough time or if it won't take too long).

The Assignment Question: Create a Java program containing a method named digits(). This method will have two parameters of type long. The first parameter will be divided by the second parameter, the divisor. The method will output the result of the division, with the result either terminating or repeating. If it repeats, digits() will output two groups of size N, where N is one less than the divisor. The driver (the main method) will input two positive integers from the user, rejecting negative inputs and not allowing the second value to be 0. Use a loop that will repeat until the first value is 0.

My "incomplete" but executable program:

import java.awt.*;
import hsa.Console;

public class Assign4_4
{ static Console c;
static long num1,num2,value1 = 0,value2 = 0,result1,result2,r,F = 0,N,A,count;

public static void main(String [] args)
{ c = new Console();

c.setTextColor(Color.red);
c.println("\n[Assign4_4]");
c.setTextColor(Color.gray);
c.println("\nDescription of Program: ");
c.setTextColor(Color.blue);
c.println("This Program will ask for two positive integers of type long and will output the");
c.println("division between the first value and the second value, along with an indication of the result being either terminating or repeating.");
c.println("Negative values and entering 0 as the second value will be rejected.");
c.println("The Program ends when the first value entered is equal to 0.");
c.setTextColor(Color.black);

while(true)
{ result1 = 0;
result2 = 0;
c.println("\nPlease enter the first value: ");
num1 = c.readLong();
if(num1 == 0)
break;
else if(num1 < 0)
{ c.println("Invalid input. Please enter a positive integer.");
num1 = c.readLong();
}
c.println("Please enter the second value: ");
num2 = c.readLong();
if(num2 < 1)
{ c.println("Invalid input. Please enter a positive integer greater than 0.");
num1 = c.readLong();
}
value1 = num1;
value2 = num2;
result1 = digits(num1,num2);
if(F == 1)
{ c.print("\n");
c.print(value1);
c.print(" divided by ");
c.print(value2);
c.print(" is ");
c.print("0.");
c.print(result1);
c.println("\nThe result consists of terminating decimals.");
}
else if(F == 2)
{ c.print("\n");
c.print(value1);
c.print(" divided by ");
c.print(value2);
c.print(" is ");
c.print("0.");
c.print(result1);
c.println("\nThe result consists of repeating decimals.");
}
else if(F == 3)
{ result2 = num1/num2;
c.print("\n");
c.print(value1);
c.print(" divided by ");
c.print(value2);
c.print(" is ");
c.print(result2);
c.println("\nThe result is a whole number.");
}
}
}

public static long digits(long num1, long num2)
{ r = num1%num2;
if(r == 0)
{ F = 3;
return r;
}
A = num1*10/num2;
while(true)
{ result2 = num1*10/num2;
r = num1*10%num2;
result1 = result1*10 + result2;
num1 = r;
if(r == 0)
{ F = 1;
return result1;
}
else if(r == A)
{ F = 2;
count = 0;
while(count<(((num2 - 1) * 2)-1))
{ result2 = num1*10/num2;
r = num1*10%num2;
result1 = result1*10 + result2;
num1 = r;
count++;
}
return result1;
}
}
}
}

Topic: com package for Java required Previous Topic   Next Topic Topic: count the number of tokens in the first line

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use