Welcome to Dream.In.Code
Become a Java Expert!

Join 149,578 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,749 people online right now. Registration is fast and FREE... Join Now!




method don't like me.....

 
Reply to this topicStart new topic

method don't like me.....

capty99
17 Oct, 2007 - 11:36 AM
Post #1

the real kya
Group Icon

Joined: 26 Apr, 2001
Posts: 9,252



Thanked: 16 times
Dream Kudos: 550
My Contributions
i didn't have long to do this project,
so didn't have time to troubleshoot, so pretty much made the lamest excuse for a program ever.

i turned in one where i handcoded 3 20 row/2 column arrays because i couldn't figure out how to make my method work.
it was already due, but i want to know why i'm a fucktard.

CODE

import java.util.*;

public class LCMa {
// This part was an attempt to get a method to establish an array
// with prime numbers, but I could not get it to run, so had to do
// it in the time and memory intensive way below
public int[][] arrayMaker(int y)
{
int[][] primesArray = new int[(y/2)][2];
int numberOfPrimes = y/2;
int count = 0;
int num =2;
while(count < numberOfPrimes){
boolean isPrime = true;
for(int divisor = 2; divisor <=  num/2; divisor++){
if(num % divisor == 0){
isPrime = false;
break;
}
}
if (isPrime){
primesArray[count][0] = num;
count++;
num++;
}}
return primesArray;
}


//This begins the working portion of the program
public static void main(String[] args){
System.out.println("Please enter two integers :"); //Prompt for input
Scanner scan = new Scanner(System.in); // Establish Scanner
int numberOne = scan.nextInt();
int numberTwo = scan.nextInt();
int biggestNum = 0;
if(numberOne > numberTwo)
{
biggestNum = numberOne;
}
else{
biggestNum = numberTwo;
}

LCMa table1 = new LCMa();
int[][] numberOneTable = new int[biggestNum/2][2];
numberOneTable = table1.arrayMaker(biggestNum);
LCMa table2 = new LCMa();
int[][] numberTwoTable = new int[biggestNum/2][2];
numberTwoTable = table2.arrayMaker(biggestNum);
LCMa finalArrays = new LCMa();
int[][] finalArray = new int[biggestNum/2][2];
finalArray = finalArrays.arrayMaker(biggestNum);
// this loop grabs the first prime number in the list,
// and sees if it works for the number one
//
for (int i = 0; i < 10; i++){
while(numberOne % numberOneTable[i][0] == 0){
numberOneTable[i][1] += 1;
numberOne = (numberOne/numberOneTable[i][0]);
}
}
for (int j = 0; j < 10; j++){
while(numberTwo % numberTwoTable[j][0] == 0){
numberTwoTable[j][1] += 1;
numberTwo = (numberTwo/numberTwoTable[j][0]);
}
}
for(int k =0; k < 10; k++){
if(numberOneTable[k][1] > numberTwoTable[k][1]){
finalArray[k][1] = numberOneTable[k][1];
}
else{
finalArray[k][1] = numberTwoTable[k][1];
}
}
int total = 1;
System.out.print("1");
for(int m =0; m < finalArray.length; m++){
for(int n =0; n < finalArray[m][1]; n++){
System.out.print(" x " + finalArray[m][0]);
total *= finalArray[m][0];
}
}
System.out.println("\n" + "LCM is : " + total);


}
}




what would be the correct way to make use of that makeArray method.



i have till 9 to finish it but am at work.
son of a..... i better get on a comp there.
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Method Don't Like Me.....
17 Oct, 2007 - 01:36 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well one problem I can immediately see is your count variable. Notice you have it controlling your while loop there. But when do you increment it? If it is prime. What if it wasn't prime? You never increment count and hence get stuck in an infinite loop. You can fix this by first using a for loop (because you know how many repetitions right? numberOfPrimes times) instead of a while loop.

Another problem I noticed was that you set num = 2 and then in your for statement you divide it by 2 which would equal one, then you start your divisor at 2 so your for statement will never work. You start at 2 and 2 <= 1 will always fail and hence your for statement will never execute.

That was just a few things I noticed. smile.gif
User is offlineProfile CardPM
+Quote Post

capty99
RE: Method Don't Like Me.....
17 Oct, 2007 - 05:20 PM
Post #3

the real kya
Group Icon

Joined: 26 Apr, 2001
Posts: 9,252



Thanked: 16 times
Dream Kudos: 550
My Contributions
alright, thanks, yeah i was definitely stuck in an infinite loop i couldn't find the cause of.

the other part i copied from the book,
and couldn't understand why it was like that...

gonna have to go back and look at it ...

appreciate it.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 10:36PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month