Activity#1 - Primitive Data Exploration
In a class called PrimitiveData:
- We will practice addition together
- We will practice input
together
- Again, practicing returns together
- Now on your own. Create a program - findMiddle that will take
in two ints (a,b) and print their middle. So if they put in 5,10
it should print out 7.5
- Gas is $2.69 a gallon, if you buy gallons of gas, have it
return how many dollars you need. So if they put in 2, it should
return 6
- Find variance will print out the variance which is the
square of x, plus the square of y
- findNearestFive which will return the nearest five, so if
they put 62, it would return 60.
- Program the method called divisibleByFive that will take in a number
and if it is evenly divisible by 5 it will return true, otherwise
it will return false. (if
you have forgot methods) (if
you have forgotten if statements
Advanced below:
- Program the method printRows() that will print out the numbers from 1 to 100 with 10 numbers on each
line(for
loops)
- Program the method printRows(int begin, int end, int numEachRow) that will print out the numbers from begin to
end with numEachRow on
each line.
- Create a method called find least number of coins that will
take as a parameter a number of cents (like 73) and it will print
out "The least number of coins possible is 2 quarters, 2
dimes and 3 pennies". The header will be:
- public void leastNumOfCoins(int cents).
- ADV: Create a method called isSelfDivisor that given
a 3 digit number, it will return true if every number in it goes
evenly in the number, or false other wise. For example 122 is
a self divisor (1 and 2 both evenly go in). Or 128 is a selfDivisor.
If it has a 0 in it (ie 505), it is NOT a selfDivisor. Extra
credit - heres the ap
version (question 1)
- public void isSelfDivisor(int x)
- ADV: Create a method public void findMagicNumber() that satisfies this puzzle.
Start with the code below:
Create a new class in bluej and replace the code with the following:
/**
* This class is to test primitive data structures
*/
public class PrimitiveDataExamples
{
/**
* Use this to test simple addition of ints and print the result
*/
public void testAdditionTogether()
{
//your code here
System.out.println("here it is " + 3);
}
public void testInputTogether (int a, int b)
{
//your code here
System.out.println("here it is " + 3);
}
public int testReturns (int x)
{
return 0;
}
public void findMiddle (int a, int b)
{
}
public int findHowManyDollars (double gallons)
{
return 0;
}
public void findVariance (double x, double y)
{
}
public int findNearestFive(int x)
{
return 0;
}
/**
* Use this to test if a number is evenly divisible by 5
* @param x the number to test if it is divisible
* @return true if it is divisible by 5, false otherwise
*/
public boolean divisibleByFive(int x)
{
//your code here (currently it returns true)
return true;
}
/**
* Prints out numbers from 1-100 with 10 numbers on each line
*/
public void printRows()
{
//your code here
}
/**
* Prints out numbers from begin-end with numEachRow numbers on each line
* @param begin the number to begin at
* @param end the number to end at
* @param numEachRow the number of numbers on each row.
*/
public void printRows(int begin, int end, int numEachRow)
{
//your code here
}
/**
* Prints out the least number of coins that it would take to make cents
* @param cents the number of cents
*/
public void leastNumOfCoins(int cents)
{
//your code here
}
/**
* given a 3 digit number, it will return true if every number in it goes evenly in the number, or false other wise.
* For example 122 is a self divisor (1 and 2 both evenly go in). Or 128 is a selfDivisor. If it has a 0 in it (ie 505), it is NOT a selfDivisor.
* @param x the number
*/
public boolean isSelfDivisor(int x)
{
//your code here (currently it returns true)
return true;
}
/**
* given any digit number, it will return true if every number in it goes evenly in the number, or false other wise.
* For example 122 is a self divisor (1 and 2 both evenly go in). Or 128 is a selfDivisor. If it has a 0 in it (ie 505), it is NOT a selfDivisor.
* @param x the number (which has any number of digits)
*/
public boolean APisSelfDivisor(int x)
{
//your code here (currently it returns true)
return true;
}
//main method for Eclipse
public static void main(String[] args)
{
PrimitiveDataExamples p=new PrimitiveDataExamples();
p.testAdditionTogether();
// p.testInputTogether(5, 10);
// System.out.println("TestReturns gives back " + p.testReturns(4));
// p.findMiddle(5, 10);
// System.out.println("Dollars needs for gas " + p.findHowManyDollars(12.5));
// p.findVariance(14, 20);
// System.out.println("The nearest five is " + p.findNearestFive(22));
// System.out.println("Divisible by 5 returns " + p.divisibleByFive(34));
// p.printRows();
// p.printRows(20,40,5);
// p.leastNumOfCoins(73);
// System.out.println("Is self divisor returns " + p.isSelfDivisor(122));
}
}
|