Advertisements

Blog Archives

Write a program SumOfTwoDice.java that prints the sum of two random integers between 1 and 6 (such as you might get when rolling dice).

//2-18-14
//TEST ** (double star) program
import java.lang.Math;

public class DicerollTEST{
public static void main(String[] args) {
int SIDES = 6;                                                                                //declares there 6 sides on a die and int SIDES
int a = 1 + (int) (Math.random() * SIDES);                       //Gives the random roll to one die
int b = 1 + (int) (Math.random() * SIDES);                      //Gives the random roll to the second die
int sum = a + b;                                                                            //Adds the sum of the two dice
System.out.println(sum);                                                       //Prints the sum of the 2 dice
}
}

Advertisements

Write a program that takes two int values a and b from the command line and prints a random integer between a and b.

import java.lang.Math;
import java.util.Scanner;

public class RandomNumbers{

public static void main(String args[])
{
System.out.println(“Enter a number “); //This prompts the user to enter a number
Scanner scan = new Scanner(System.in);
double t = scan.nextDouble(); //Stores the valuse as a double

System.out.println(“Enter a second number “); //This prompts the user to enter a 2nd number
double v = scan.nextDouble(); //Stores the valuse as a double

double w = Math.floor(Math.random()*(t-v+1)+v); //assigns w to a random number in between t & v
System.out.println(w); //prints w

}
}

Write a program SpringSeason.java that takes two int values

import java.util.Scanner;

public class SpringSeason{

public static void main(String args[])
{
System.out.println(“Enter the month “); //This prompts the user to enter a month
Scanner scan = new Scanner(System.in);
int month = scan.nextInt(); //Stores the valuse as an int

System.out.println(“Enter the day “); //This prompts the user to enter day
int day = scan.nextInt(); //Stores the valuse as an int

boolean wootwoot = (month == 3 && day >= 20 && day <= 31)
|| (month == 4 && day >= 1 && day <= 30)
|| (month == 5 && day >= 1 && day <= 31)
|| (month == 6 && day >= 1 && day <= 20);

System.out.println(wootwoot);

}
}

C++ Programming Maze

This is a final project of mine from my C++ class.  Its currently a working program but feel free to make changes if you must and enjoy!  Oh, if you decide to copy the code you have to like the page.

rant away

 

#include <iostream> //programmer rant4u
#include <string>  //
#include <iomanip> //
#include <cmath>

using namespace std;

//declare array for game board and fill it    // gameboard
char gameBoard[20][20] =
{ {‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘, ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘}
, {‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’, ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘|’}
, {‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘, ‘-‘ , ‘-‘ , ‘#’ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘|’}
, {‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘#’, ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘|’}
, {‘|’ , ‘O’ , ‘|’ , ‘#’ , ‘|’ , ‘-‘ , ‘-‘ , ‘#’ , ‘|’ , ‘-‘ , ‘|’, ‘#’ , ‘|’ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘#’ , ‘|’}
, {‘|’ , ‘-‘ , ‘-‘ , ‘-‘ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’, ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘|’}
, {‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘|’, ‘#’ , ‘|’ , ‘#’ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘|’}
, {‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘-‘ , ‘#’ , ‘|’ , ‘-‘, ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘|’}
, {‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘|’ , ‘#’, ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘-‘ , ‘-‘ , ‘#’ , ‘|’}
, {‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘-‘ , ‘|’ , ‘#’, ‘#’ , ‘|’ , ‘-‘ , ‘-‘ , ‘-‘ , ‘|’ , ‘|’ , ‘#’ , ‘|’}
, {‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘|’ , ‘#’, ‘#’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘|’ , ‘#’ , ‘|’}
, {‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘-‘ , ‘-‘ , ‘-‘ , ‘#’ , ‘|’ , ‘#’, ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘-‘ , ‘-‘ , ‘|’ , ‘#’ , ‘|’}
, {‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘|’ , ‘#’, ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘|’}
, {‘|’ , ‘#’ , ‘-‘ , ‘-‘ , ‘|’ , ‘#’ , ‘|’ , ‘-‘ , ‘#’ , ‘|’ , ‘#’, ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘-‘ , ‘-‘ , ‘#’ , ‘-‘ , ‘|’}
, {‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘|’ , ‘#’, ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘|’}
, {‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘|’ , ‘#’, ‘|’ , ‘#’ , ‘|’ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘#’ , ‘|’}
, {‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘|’ , ‘#’, ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘|’}
, {‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘#’, ‘|’ , ‘#’ , ‘|’ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘#’ , ‘|’}
, {‘|’ , ‘X’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’, ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘|’}
, {‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘, ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘}};

void up(int &, int &); // pass the players position to all
void down(int &, int &); // functions by reference
void left(int &, int &);
void right(int &, int &);   //declaring functions

int main()
{
char ch1;                    // variables
int playerCOL = 1; // variable to hold players x coord remember higher numbers move right
int playerROWS= 18; // variable to hold players y coord remember higher numbers move down
bool game = false; // bool to hold tell when the game is over

// at start of program clear the screen and output the map
do            // starting the do while loop
{
system(“cls”);             //clearing the screen
for(int i = 0; i < 20; i++)           //refreshing the output screen
{
for(int j = 0; j < 20; j++)
{
cout << gameBoard[i][j] << ” “;
}
cout <<endl;
}
cout <<“Your currently at location ” << playerROWS << ” ” << playerCOL <<endl;   //asking the user for input
cout << “Which direction would you like to move:  WSAD ?” <<endl;
cin >> ch1;      //input

if (ch1 == ‘w’)                   //series of if checks, then calls the function of movement up
{
up(playerCOL, playerROWS);
}
else if (ch1 == ‘a’)
{
left(playerCOL, playerROWS);
}
else if (ch1 == ‘s’)
{
down(playerCOL, playerROWS);
}
else if (ch1 == ‘d’)
{
right(playerCOL, playerROWS);      // end of directional checks
}
else
{
cout <<“error”<<endl;            //if the user enters wrong letter for direction
}
if(playerCOL == 1 && playerROWS == 4)   //if the player reaches the ‘O’ and wins
{
game = true;
}
}
while(game == false);          //triggers end of game

cout << ” Congrats you have won the game”<<endl;         //display when you win
system(“pause”);
return 0;
} // end main
void up(int & COL, int & ROWS) // function that moves the player in the up direction
{
if(gameBoard[ROWS-1][COL] == ‘#’)           //moves your marker up
{
ROWS–;
gameBoard[ROWS][COL] = ‘X’;
gameBoard[ROWS+1][COL] = ‘#’;
}
else
{
cout <<“Bad Move” <<endl;            //displaying your bad move if you cant move there
}

} // end up()
void down(int & COL, int & ROWS) // function that moves the player in the down direction
{

if(gameBoard[ROWS+1][COL] == ‘#’ ||gameBoard[ROWS+1][COL] ==’O’)   //moves your marker down
{
ROWS++;
gameBoard[ROWS][COL] = ‘X’;
gameBoard[ROWS-1][COL] = ‘#’;
}
else
{
cout <<“Bad Move” <<endl;           //displaying your bad move if you cant move there
}

} // end down()

void left(int & COL, int & ROWS) // function that moves the player in the left direction
{

if(gameBoard[ROWS][COL-1] == ‘#’)   //moves your marker left
{
COL–;
gameBoard[ROWS][COL] = ‘X’;
gameBoard[ROWS][COL+1] = ‘#’;

}
else
{
cout <<“Bad Move” <<endl;           //displaying your bad move if you cant move there
}

} // end left()

void right(int & COL, int & ROWS) // function that moves the player in the right direction
{
if(gameBoard[ROWS][COL+1] == ‘#’)          //moves your marker right
{
COL++;
gameBoard[ROWS][COL] = ‘X’;
gameBoard[ROWS][COL-1] = ‘#’;

}
else
{
cout <<“Bad Move” <<endl;    //displaying your bad move if you cant move there
}

} // end right()

C++ Recursion

In this code I’m not using the proper recursion method, or I’m not fully understanding what “recursion” is actually about.  Some help on this matter would be much appreciated.

#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
using namespace std;

// declare global variables
int sum, num1, num2, result, result1;

// function prototypes
void functionRecursion(int, int);
void functionCall(int);
void functionExit();
void functionLessThan(int);
void functionGreaterThan(int);
int functionCounter(int); // recursion function

int main()
{

functionRecursion(num1, num2);
functionCall(result);
functionCounter(result);
system(“pause”);
return 0;
}

void functionRecursion(int num1, int num2)
{

cout <<“please enter two numbers:  “<<endl;
cin >> num1 >> num2;

result1 = num1 * num2;
result = num1 * 5;
cout << result <<endl;
cout << result1 <<endl;
}
void functionCall(int result)
{
if(result < 500)
{
functionLessThan(result);
}
else;
{
functionGreaterThan(result);
}
}
void functionLessThan(int result)
{
cout << “Your answer is less then 500” <<endl;
int functionCounter(result);
system (“pause”);
exit (0);
}
void functionGreaterThan(int result)
{
cout << “Your answer is greater than 500” <<endl;
system (“pause”);
exit (0);
}

int functionCounter(int result)
{
int result2 = result;  //setting result2 to equal result for the function counter (switch)
for(int result2 = 0; result2 < 500 ; result2++);
{
cout <<“your answer is:   ” << result2 <<endl;
return result2;  //returning result2
}
}

%d bloggers like this: