Hi all,

I'm a beginning CS student and am working on an assignment today and am hoping someone wouldn't mind checking my work. I've typing out each assignment problem followed by the code I wrote. Also, please note I'm not required to write complete methods just the code for the stated problem.

Thanks in advance for any comments/corrections/clarifications I may recieve!

  1. I have an array of 100 integers (date type int) called A. I want to know the sum of the first fifteen positions. Declare and initialize a local variable called sum and use a loop to calculate the sum of the first fifteen locations of array A.
int sum = 0;
for(i = 0;  i <= 14;  i++){
sum += A;
}
  1. Given the same array A in question 1, I want to multiply the values in the array, starting with the first location, until the product is greater than 500. Declare a local variable called product and initialize it to 1 (not to 0!) and use a while loop that will multiply successive array locations until the product exceeds 500.
int product = 1;
int i = 0;
while(i < A.length && product<=500){
product = product*A;
i++;
}
  1. Given the same array A in question 1, I want to find the first position in the array that contains the number 3, and I know there is at least one location in the array that contains a 3. Declare a
    local variable called answer, and use a loop to look at each location until you find a 3. The loop should end when the first location containing 3 is found. After the loop, answer should contain the index of the location that contained the 3.
int answer;
int count = 0;
boolean found = false;
while(count<A.length && !found){
if(A[count] == 3){
found = true;
answer = count;
}
else{
count++;
}
}
return answer;
  1. Given the same array A in question 1, I want to find the first position in the array that contains the number 50, but this time I don’t know for certain that there is a location containing the
    number 50. Declare a local variable called answer and use a loop to search for the number 50. After the loop ends, answer should either contain the index of the location that contained the first 50 or answer should contain a -1 if there was no 50 found in the array.
int answer;
int count = 0;
boolean found = false;
while(count<A.length && !found){
if(A[count] == 50){
found = true;
answer = count;
}
else{
count++;
}
}
if(!found){
answer = -1
}
return answer;

5.Given the same array A in question 1, declare and initialize two local integer variables sum1 and sum2. Using a loop sum1 should be the sum of all the odd array locations and sum2 should be the sum of all the even array locations, including the first location. Remember that you can use the remainder operator to determine if a number is even or odd.

int sum1 = 0;
int sum2 = 0;
for(int i = 0; i < A.length; i++){
if(A % 2 == 0){
sum2 += A;
}
else{
sum1 += A;
}
}

6.Given three integers A, B, and C, I want to divide A by B and assign the quotient to C, but only if B does not contain a 0. Use an if statement that will assign C the value 0 if B is 0 or assign C the value of A divided by B if B is not 0.

if(B == 0){
C = 0;
}
else{
C = A/B;
}
  1. I am writing a program that will play the card game Blackjack (21). There is an internal method called dealACard() that I can call to have a card dealt; there is a boolean variable called hitMe that the player sets by clicking a button; and there is an int variable called handValue that is automatically updated by the dealACard() method. Write a loop that will call the method dealACard() as long as hitMe contains a true value and handValue is less than 21.
    (HINT: Do not make this harder than it needs to be. You can accomplish this in two lines of code.)
while(handValue < 21 && hitMe == true){
dealACard();
}

You can test your own code by providing multiple example inputs and seeing what the output is.

Instead of asking us you can write out the main method and other required methods for each of your assignments one by one and (as mentioned) test the code for multiple inputs. If the outputs exactly match what you have been expecting the program to do to the input then the code, you can assume, is correct.

This will not only be a mechanism to "test" your assignments but, this will also help you understand your code better, where does it fail and why does it fail, if at all it fails. It will also help your deugging skills if you are required to correct a failing program.

You wrote the code. Does it compile? If it does, is it so difficult to run it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.