There's also a menu that's called to choose what operation what you want done.
Main Class:===========================================================
//Christopher Smith
//CSC 15
//Lab 11 Classes Main
import java.util.Scanner;
public class lab11 {
public static void main (String[] args){
int pick, answer;
int x = 0;
int y = 0;
Scanner kb = new Scanner(System.in);//keyboard input
lab11calc my_subs = new lab11calc(); //object for sub class
while (true) //infinite loop
{
System.out.println();
System.out.println("---Menu---");
System.out.println("1: Exit Program");
System.out.println("2: Enter Values x & y");
System.out.println("3: Find and display summation");
System.out.println("4: Find and display product");
System.out.println("5: Check primes");
System.out.print(" Selection: ");
pick = kb.nextInt();
System.out.println();
if (pick==1) return;
else if (pick==2){
System.out.println("Enter x");
x = kb.nextInt();//enter x value
System.out.println("Enter y");
y = kb.nextInt();}//enter y value
else if (pick==3){
answer = my_subs.SumThem(x,y);//passes both x and y
System.out.println("Summation is "+answer);}//returns ans from other class
else if (pick==4){
answer = my_subs.MultiplyThem(x,y);//passes both x and y
System.out.println("Product is "+answer);}//displays returned answer from other class
else if (pick==5){
answer = my_subs.Prime(x,y);
System.out.println("Prime is "+answer);}
}
}
}
Sub Class:===============================================================
//Christopher Smith
//CSC 15
//Lab 11 Classes Subs
public class lab11calc {
public int SumThem(int start, int end){//int start and end takes the x and y variables from main class
int sum, count;
sum = 0;
for (count = start; count <= end; count++)
{
sum += count;
}
return sum; //return the sum of all the numbers
}
public int MultiplyThem(int start, int end){ //int start and end takes the x and y variables
int product, count;
product = 1;
for (count = start; count <=end; count++)
{
product *= count;
}
return product;//return the product of all the numbers
}
public int Prime(int start, int end){
int i; //variable for determining prime numbers
int count=0;//variable for incrementing numbers between select numbers
int primeCount = 0;//amount of prime numbers
for (count = start; count <=end; count++)//rolls through all my values between select numbers
{
for (i=2;i<=count;i++)//increments my i count for testing prime
{
if (count%i==0)//checks prime
{
primeCount++; //increments primeCount to count amount of prime numbers
}
}
}
return primeCount;//returns primeCount value for input in main
}
}
No comments:
Post a Comment