Sunday, August 5, 2012

Differential Equations EXAM 2 B




Differential Equations EXAM 1 B


Differential Equations "MATH 210" EXAM 1




Recursion

Numerical / Logical


20852
Write a method called fact that recursively calculates the factorial value of its single int parameter. The value returned by fact is a long ..



public long fact(int n) {
if (n <= 1) {
return 1;
} else {
return (fact(n-1) * (long) n);
}
}


20854
The sum of the numbers from 1 to n can be defined recursively as follows:
The sum from 1 to 1 is 1.
The sum from 1 to n is n more than the sum from 1 to n-1. Write a int -method named sum that accepts an int parameter, n, and recursively calculates and returns the sum of the numbers from 1 to n. .


public int sum(int n) {
if (n == 1) {
return 1;
} else {
return (sum(n-1) + n);
}
}


21210
Consider a simple form of integer division: m / k where we are guaranteed that m>=0 and k>0. This can be computed as follows
The quotient is 0 when k is greater than m.
Otherwise, the quotient is one more than (m-k)/k Write ab int -method named quotient that accepts two int parameter, m and k, and recursively calculates and returns the integer quotient of m/k. You can count on m>=0 and k>0. Do not use a division operator here! .


int quotient(int m, int k) {

if(k==1)
return m;
if(k>m)
return 0;
return quotient(m-k, k) +1;
}


20855
Given non-negative integers x and n, x taken to the nth power can be defined as:
x to the 0th power is 1
x to the nth power can be obtained by multiplying x to the n-1'th power with x Write a long -valued method named power that accepts an two int parameters x and n (in that order) and recursively calculates and returns the value of x taken to the n'th power.

int power(int x,int n)
{
if(n==1)
return x;
if(n==0)
return 1;

return power(x, n-1) * x;
}


20853
Two non-negative integers x and y are equal if either:
Both are 0, or
x-1 and y-1 are equal Write a boolean -method named equals that recursively determines whether its two int parameters are equal and returns true if they are and false otherwise.

boolean equals (int x, int y)
{
if( (x<0) || (y<0) ) return false;
if( (x==0) && (y==0) ) return true;
return equals(x-1, y-1);
}

21231
The nth harmonic number is defined non-recursively as: 1 +1/2 + 1/3 + 1/4 + ... + 1/n. Come up with a recursive definition and use it to guide you to write a method definition for a double -valued method named harmonic that accepts an int parameters n and recursively calculates and returns the nth harmonic number.


double harmonic(int N)
{
if (N == 0) {
return 0.0;
} else {
return (1.0/N + harmonic(N-1));
}
}

The Fibonacci series: 0, 1, 1, 2, 3, 5, 8, 13, 21, has as its first 2 values, 0 and 1; each successive value if then calculated as the sum of the previous two values.
The first element in the series is the 0'th element, thus the value 8 is element 6 of the series.
The n'th element of the series, written as fib(n), is thus defined as:
n if n = 0 or n = 1
fib(n-1) + fib(n-2) Write the int-valued method fib, that takes a single int parameter (say n), and recursively

public static int fib(int n)
{
if(n==0)
return 0;
else if(n<=2)
return 1;
return fib(n-1)+fib(n-2);
}


The "odd/even factorial" of a positive integer n is represented as n  and is defined non-recursively as: (n)(n-2)(n-4)...(4)(2) if n is even and is (n)(n-2)(n-4)...(5)(3) (1) if n is odd. For example 7  equals 7*5*3*1 or 105 and 6  equals 6*4*2 or 48. Come up with a recursive definition for n  and use it to guide you to write a method definition for a method called oddevenfact that recursively calculates the odd/even factorial value of its single int parameter. The value returned by oddevenfact is a long ..




long oddevenfact(int x)
{
if (x>2)
return(oddevenfact(x-2) * (long) x);
else
return((long) x);
}







Tuesday, July 17, 2012

Coin Game in Java

import java.util.Scanner;
import java.util.Random;
public class CoinGame
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter '0' if you want to play against a computer or '1' if you want to play against another player.");
int start = keyboard.nextInt();
int score = 0;
int n = 1;
String a;
String b;
String ht = "ht";
Random coin = new Random();
if (start == 1) {
System.out.println("Welcome to the coin-guessing game.");
System.out.println("Player 1's objective is to guess te opposite of what Player 2 will guess.");
System.out.println("Player 2's objective is to guess what Player 1 has guess that Player 2 will not guess.");
System.out.println(" In other words, Player 2 wants to guess Player 1's input.");
System.out.println("Every set that goes in Player 1's favor will decrease the score by 1.");
System.out.println("Every set that goes in Player 2's favor will increase the score by 1.");
System.out.println("Player 1 wins if the score reaches -5 while Player 2 wins if the score reaches +5.");
while( score*score < 25) {
System.out.println(" ");
System.out.println("Set " + n);
System.out.println("Now, Player 1, with Player 2 not looking, enter 'h' (for heads) or 't' (for tails).");
a = keyboard.next();
char c = a.charAt(0);
while (c != 'h' & c != 't'){
System.out.println("Player 1, you did not enter 'h' (for heads) or 't' (for tails).");
System.out.println("Please try again.");
a = keyboard.next();
c = a.charAt(0);
}
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println("40 blank lines were just printed so that Player 2 should not see Player 1's input.");
System.out.println("Now, Player 2, guess Player 1's input by entering 'h' (for heads) or 't' (for tails).");
b = keyboard.next();
char d = b.charAt(0);
System.out.println("Player 1 had entered " + c + " while Player 2 had entered " + d + ".");
n = n+1;
if (c == d)
score = score + 1;
else
score = score - 1;
if (score > 0)
System.out.println("The score is now " + score + " in Player 2's favor.");
if (score < 0)
System.out.println("The score is now " + score + " in Player 1's favor.");
if (score == 0)
System.out.println("The score is now tied at " + score + ".");
if (score == 4)
System.out.println("Uh-oh, looks like this game is about to be over. Match Point!");
if (score == -4)
System.out.println("Uh-oh, looks like this game is about to be over. Match Point!");
}
if(score == 5)
System.out.println("Player 2 Wins!");
else
System.out.println("Player 1 Wins!");
}
if (start == 0) {
System.out.println("Welcome to the coin-guessing game.");
System.out.println("Your objective is to guess te opposite of what the computer will guess.");
System.out.println("The computer's guesses will be randomly generated.");
System.out.println("Every set that goes in your favor will decrease the score by 1.");
System.out.println("Every set that goes in the computer's favor will increase the score by 1.");
System.out.println("You win if the score reaches -5 while the computer wins if the score reaches +5.");
while( score*score < 25) {
System.out.println(" ");
System.out.println("Set " + n);
System.out.println("Please enter 'h' (for heads) or 't' (for tails).");
a = keyboard.next();
char c = a.charAt(0);
while (c != 'h' & c != 't'){
System.out.println("You did not enter 'h' (for heads) or 't' (for tails).");
System.out.println("Please try again.");
a = keyboard.next();
c = a.charAt(0);
}
int f = coin.nextInt(2);
String e = ht.substring(f,f+1);
char d = e.charAt(0);
System.out.println("You entered " + c + " while the computer had entered " + d + ".");
n = n+1;
if (c == d)
score = score + 1;
else
score = score - 1;
if (score > 0)
System.out.println("The score is now " + score + " in the computer's favor.");
if (score < 0)
System.out.println("The score is now " + score + " in your favor.");
if (score == 0)
System.out.println("The score is now tied at " + score + ".");
if (score == 4)
System.out.println("Uh-oh, looks like this game is about to be over. Match Point!");
if (score == -4)
System.out.println("Uh-oh, looks like this game is about to be over. Match Point!");
}
if(score == 5)
System.out.println("Sorry, You Lost.");
else
System.out.println("You Win!");
}
}
}

Monday, July 2, 2012

box of produce JAVA CODE


Description:
* This program reads in three random vegetables from a predefined list and asks if the user would like
* to substitute any produce. The user can substitute produce as many times as they like.
* Once the user confirms the content of the box, the contents of the final box is output.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

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

// reads in the ListOfProduce file
Scanner file = null;
try {
file = new Scanner(new FileInputStream("text"));
}
catch (FileNotFoundException e) {}

String item1 = file.next();
String item2 = file.next();
String item3 = file.next();
String item4 = file.next();
String item5 = file.next();
Scanner user = new Scanner(System.in);

BoxOfProduce1 CSA = new BoxOfProduce1();

System.out.println("Your box of produce contains: ");
System.out.println("[ "+CSA.getProduce1() + ", "
+ CSA.getProduce2() + ", " + CSA.getProduce3()+" ]");

String loop = "n";
System.out.println("\nAre you happy with your selection? (Y/N)\n>");
loop = user.next();

//begins loop if user is not satisfied with their selection
while (loop.equalsIgnoreCase("n"))
{
Scanner user2 = new Scanner(System.in);
Scanner user3 = new Scanner(System.in);

System.out.println("\nYou can choose from the following items: \n(1)" + item1 +" \n(2)" + item2+" \n(3)"+item3+" \n(4)"+item4+" \n(5)"+item5);
System.out.println("Which item do you prefer? Please select an item number.\n>");
int choice = user2.nextInt();
String item = null;

//given user input for their choice, sets the item to this new choice
if (choice==1)
item =item1;
if (choice==2)
item =item2;
if (choice==3)
item=item3;
if (choice==4)
item =item4;
if (choice==5)
item =item5;
System.out.println("You prefer "+item.toLowerCase());

System.out.println("\nWhich item would you like to replace?");
System.out.println("(1)"+CSA.getProduce1() + " "
+ "(2)" + CSA.getProduce2() + " " + "(3)"+CSA.getProduce3()+"\n>");
int replace = user3.nextInt();

if (replace == 1)
{
CSA.setProduce1(item);
}
if (replace == 2)
{
CSA.setProduce2(item);
}
if (replace == 3)
{
CSA.setProduce3(item);
}


System.out.println("\nYour new selection contains: ");
System.out.println(CSA.getProduce1() + ", "
+ CSA.getProduce2() + ", " + CSA.getProduce3());

System.out.println("\nAre you happy with your selection? (Y/N)");
loop = user.next();

}


System.out.println("Enjoy your selection.");

}
}

*************************************

file #2

Description:
* This program defines the class, accessor, and mutator variables.
* It takes values from the file, "ListOfProduce," puts them into an array with numbers corresponding to each item.
* It produces three random numbers and accesses the corresponding value of the array.
*


import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import java.io.*;


public class BoxOfProduce1
{
private String produce1, produce2, produce3 ;

public BoxOfProduce1()
{
this.produce1 = "";
this.produce2 = "";
this.produce3 = "";

//creates an array from the read in file (ListOfProduce)
ArrayList<String> produce = new ArrayList<String>();

Scanner file = null;
try {
file = new Scanner(new FileInputStream("text"));
}
catch (FileNotFoundException e)
{
}
String item1 = file.next();
String item2 = file.next();
String item3 = file.next();
String item4 = file.next();
String item5 = file.next();

produce.add(item1);
produce.add(item2);
produce.add(item3);
produce.add(item4);
produce.add(item5);

// creates 3 random variables from a possible 5 which correspond to the values in the array
Random r= new Random();
int x1 = r.nextInt(5);
int x2 = r.nextInt(5);
int x3 = r.nextInt(5);

this.produce1 = produce.get(x1);
this.produce2 = produce.get(x2);
this.produce3 = produce.get(x3);


}
public BoxOfProduce1 (String newProduce1, String newProduce2, String newProduce3)
{
this.produce1 = newProduce1;
this.produce2 = newProduce2;
this.produce3 = newProduce3;
}

//accessor1
public String getProduce1()
{
return this.produce1;
}

//mutator1
public void setProduce1 (String newProduce1)
{
this.produce1 = newProduce1;
}
//accessor2
public String getProduce2()
{
return this.produce2;
}
//mutator2
public void setProduce2(String newProduce2)
{
this.produce2 = newProduce2;
}
//accessor3
public String getProduce3()
{
return this.produce3;
}
//mutator3
public void setProduce3(String newProduce3)
{
this.produce3 = newProduce3;
}
public String toString()
{
return "Produce 1: " + this.produce1 + "Produce 2: " + this.produce2 +" Produce 3: " + this.produce3;
}
}