stefanbund / cis155

3 stars 5 forks source link

Assignment Two Challenges and Discussion #1

Open stefanbund opened 9 years ago

stefanbund commented 9 years ago

Please post discussion where you experience challenges with Assignment 2, including errors and/or design and strategy.

Saiprya commented 9 years ago

I'm stuck on assignment two

I am not sure how to do a second array without it stopping my first array from running or not running my second for loop. I tried by creating another for loop within the first for loop and (when that didn't work) I then tried it out of the first for loop. But it's not working because I can't get the program to run the second for loop. I'm lost...

stefanbund commented 9 years ago

OK, let's work on it here. Go ahead and post the source code that seems to be where your errors come from.

From Stefan

On Oct 27, 2015, at 1:31 AM, Melissa notifications@github.com wrote:

I'm stuck on assignment two

I am not sure how to do a second array without it stopping my first array from running or not running my second for loop. I tried by creating another for loop within the first for loop and (when that didn't work) I then tried it out of the first for loop. But it's not working because I can't get the program to run the second for loop. I'm lost...

— Reply to this email directly or view it on GitHub.

Saiprya commented 9 years ago

I'm not sure where the problem is, but it stops working after it runs through the first loop.

System.out.println ("How many packages would you like to send? "); int numPackages = input.nextInt();

    double[] packages = new double [numPackages];

    System.out.print ("\nEnter the weight of each package in oz: ");
    for (int i=0; i < packages.length; i++)
    {
        System.out.print("\nPackage " + (i+1) + ": ");//having client enter the weight of the package
        packages [i] = input.nextDouble();      
        numWeight = packages [i]; //to calculate the fee
        //using else if statements to find the type
        type = packages [i];
        if (type <= 3.5)
            {type = letter;}
        else if (type <= 64.0)
            {type = small;}
        else if (type <= 94.0)
            {type = large;}
        else if (type > 94.0)
            {type = xlarge;}

        //calculate the fee for the package
        calcFee = calcFee + numWeight * type;
        //format calcFee
        NumberFormat currency = NumberFormat.getCurrencyInstance();
        System.out.print (currency.format(calcFee) + "\n");
    }

        //enter calcFee into the array
        int numSubtotal = input.nextInt();
        double [] sub = new double [numSubtotal];
        for (int j=0; j < packages.length; j++)
        {
            calcFee=sub [j];
            //reset calcFee             
            calcFee=0.0;
        }
stefanbund commented 9 years ago

couple of small details to clear up:

  1. numWeight needs to be preceded by the keyword 'double' numWeight = packages [i]; //to calculate the fee Or was it declared at the start of the app
  2. same issue here -- was xlarge, small and other variables declared and initialized earlier, also, so 'type' ends up with a value when you do: if (type <= 3.5) {type = letter;}

It's ok to copy/paste your entire .java file in the next reply

Saiprya commented 9 years ago

import java.text.NumberFormat; import java.util.*;

public class Multiple2 { public static void main(String[] args) { Scanner input= new Scanner (System.in); final double letter = 1.50; final double small = 5.00 ; final double large = 10.00; final double xlarge = 15.00; double type, numWeight, total;

    String choice = "y";
    while(!choice.equalsIgnoreCase("n"))
    {
        System.out.println ("How many packages would you like to send? ");//sets array length
        int numPackages = input.nextInt();
        double[] packages = new double [numPackages];
        System.out.print ("\nEnter the weight of each package in oz: ");
            for (int i=0; i < packages.length; i++)
            {
                System.out.print("\nPackage " + (i+1) + ": ");//having client enter the weight of the package
                packages [i] = input.nextDouble();      
                numWeight = packages [i]; //to calculate the fee
                //using else if statements to find the type
                type = packages [i];
                if (type <= 3.5)
                    {type = letter;}
                else if (type <= 64.0)
                    {type = small;}
                else if (type <= 94.0)
                    {type = large;}
                else if (type > 94.0)
                    {type = xlarge;}

                //calculate the fee for the package
                double calcFee = 0.0;
                calcFee = calcFee + numWeight * type;
                //format calcFee
                NumberFormat currency = NumberFormat.getCurrencyInstance();
                System.out.print (currency.format(calcFee) + "\n");
               //enter calcFee into the array
                total=calcFee;

                double [] sub = {total};
                for (int j=0; j < sub.length; j++)
                {
                    total+=sub[j];
                    //reset calcFee             
                    //calcFee=0.0;
                }

                System.out.print ("Your total is:" + currency.format(total)+"\n");
            }
    }
}

}

Saiprya commented 9 years ago

I can't figure out how to put the calcFee value into a second array.

Saiprya commented 9 years ago

i'm sorry but i'm still very lost. i posted my full code above...since i've posted i've tried changing the code by creating a method but im still confused how to apply what is calculated into an array. help please

stefanbund commented 9 years ago

Adding the results of an array involves creating the array once you know how many items you wish to store. Then, you insert your latest item into the next available position. An alternative is to create an array list, initially, then simply use the .add method of that array list

From Stefan

On Nov 1, 2015, at 1:14 AM, Melissa notifications@github.com wrote:

i'm sorry but i'm still very lost. i posted my full code above...since i've posted i've tried changing the code by creating a method but im still confused how to apply what is calculated into an array. help please

— Reply to this email directly or view it on GitHub.

michaeljlee92 commented 8 years ago

Is there a new discussion forum for assignment 3?

stefanbund commented 8 years ago

I can make a new discussion forum if you believe it's a good idea. We can also continue to use this form. Which do you think is best?

From Stefan

On Nov 18, 2015, at 1:53 AM, michaeljlee92 notifications@github.com wrote:

Is there a new discussion forum for assignment 3?

— Reply to this email directly or view it on GitHub.

michaeljlee92 commented 8 years ago

I think this is fine. I was just wondering if there was a new post that I wasn't seeing.

michaeljlee92 commented 8 years ago

I am having a hard time implementing the recursion step to my methods. I am trying to get the prime numbers out of an array I get from a file. public static Integer[] recursion(int []x) { ArrayList list=new ArrayList(); for(int i=0; i<x.length; i++) { boolean prime=true; for(int j=2; j<x[i];j++) { if(x[i]%j==0) { prime=false; break; } } if(prime&&x[i]!=1) { list.add(x[i]); } } Integer[] Numbers=list.toArray(new Integer[list.size()]); System.out.println("The Prime Numbers are: "); for(int k=0; k<Numbers.length; k++) { System.out.print(Numbers[k]+" "); } return Numbers; I was thinking of taking out the first for loop and making an "external for loop" with the recursion step, but I am not too sure how to do it. Any hints or suggestions would be greatly appreciated!

Additionally, I am having a hard time getting the output file to work. There are no errors in my file but I can't get the output to work correctly. The following is my output code.

File PrimeNumbers=new File("PrimeNumbers.txt");
    PrintWriter output=new PrintWriter(PrimeNumbers);
    if(!PrimeNumbers.exists())
    {
        System.out.println("Error: cannot open PrimeNumbers.txt");
        System.exit(0);
    }

I am not even sure if there are errors to this, but when I try to output.print, the export file is not created. Please help!

stefanbund commented 8 years ago

This is a good start and contains some good elements of a recursive algorithm . One thing you're doing very well is creating a base class, which is the first if statement. This creates a condition where the function will stop working, then if that is not true, it continues with the rest of it.

A recursive algorithm will include a call to itself at some point. This means that during your second if statement or later, it will actually call your recursion function, inside of it.

This will cause the algorithm to repeat until it works down to the bottom of your pile of data.

As for your file writing procedure, I will take a look at it a bit later

From Stefan

On Nov 19, 2015, at 4:45 PM, michaeljlee92 notifications@github.com wrote:

I am having a hard time implementing the recursion step to my methods. I am trying to get the prime numbers out of an array I get from a file. public static Integer[] recursion(int []x) { ArrayList list=new ArrayList(); for(int i=0; i<x.length; i++) { boolean prime=true; for(int j=2; j<x[i];j++) { if(x[i]%j==0) { prime=false; break; } } if(prime&&x[i]!=1) { list.add(x[i]); } } Integer[] Numbers=list.toArray(new Integer[list.size()]); System.out.println("The Prime Numbers are: "); for(int k=0; k<Numbers.length; k++) { System.out.print(Numbers[k]+" "); } return Numbers; I was thinking of taking out the first for loop and making an "external for loop" with the recursion step, but I am not too sure how to do it. Any hints or suggestions would be greatly appreciated!

Additionally, I am having a hard time getting the output file to work. There are no errors in my file but I can't get the output to work correctly. The following is my output code.

File PrimeNumbers=new File("PrimeNumbers.txt"); PrintWriter output=new PrintWriter(PrimeNumbers); if(!PrimeNumbers.exists()) { System.out.println("Error: cannot open PrimeNumbers.txt"); System.exit(0); } I am not even sure if there are errors to this, but when I try to output.print, the export file is not created. Please help!

— Reply to this email directly or view it on GitHub.

michaeljlee92 commented 8 years ago

The values of the prime numbers are get printed, but an error ensues after running the code. I get an error at the "for(int j=2; j<x[nextnumber];j++)" and the "return recursion(x, nextnumber+1, counter);" if that helps Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100 at assignment32.recursion(assignment32.java:11) at assignment32.recursion(assignment32.java:26)

Here is my code. public static int recursion(int []x, int nextnumber, int counter) while(nextnumber<=x.length) { boolean prime=true; for(int j=2; j<x[nextnumber];j++) { if(x[nextnumber]%j==0) { prime=false; break; } } if(prime&&x[nextnumber]!=1) { System.out.println(x[nextnumber]+" "); counter=+1; }

        return recursion(x, nextnumber+1, counter);
    }
    return counter;
}

Is this because I put "System.out.println" function into the "public static int method"?

michaeljlee92 commented 8 years ago

I had a question about the midterm. Do we have to determine the time it takes to get from one city to another? For example, the time it takes from LAX (LA) to SFO (San Francisco). Or could we just leave it a constant number for each flight? For example, each flight takes approximately an hour.

In addition, is it okay if we use only int[] arrays instead of object or string arrays, or do we have to include strings such as LAX for airport? I was using ints for all my variables int[][] jet3=new int[][]{order1, order2, order3};

stefanbund commented 8 years ago

I think it would be OK to pre-assign distances or times between the cities. It would also be appropriate to assign a random distance or travel time between them, as well.

From Stefan

On Dec 2, 2015, at 10:48 AM, michaeljlee92 notifications@github.com wrote:

I had a question about the midterm. Do we have to determine the time it takes to get from one city to another? For example, the time it takes from LAX (LA) to SFO (San Francisco). Or could we just leave it a constant number for each flight? For example, each flight takes approximately an hour.

In addition, is it okay if we use only int[] arrays instead of object or string arrays, or do we have to include strings such as LAX for airport? I was using ints for all my variables int[][] jet3=new int[][]{order1, order2, order3};

— Reply to this email directly or view it on GitHub.