Download presentation
Presentation is loading. Please wait.
1
What’s cheating and what is not?
Looking at slides? Looking at code?
2
Review: Reading values from user: int (number of tickets), double (ratio),… Declaring and using variables (updating values of variables) Picking random values (without understanding too much) String concatenation Boolean operators If-else statement Use of loops: while, for Printing +Scope/life of a variable Writing a program from scratch.
3
Creating random number with hidden classes:
Write a program that creates some random numbers for tickets which can be categorized into two classes(normal and marked/hidden class) by checking the average of all its digits (if it falls in-between two boundaries set as minimum mean and maximum mean). We would like our program to get the following inputs from the user: Number of digits to be used for each number Total number of tickets The ratio of number of tickets in the hidden class over number of all tickets (0-1) Minimum value for mean of digits for the hidden class Maximum value for mean of digits for the hidden class
4
Sample outputs
5
The steps we will consider for our review:
Create one ticket Create many tickets Classify tickets Count tickets in each class Set number of samples in each class Put it together
6
public class RandomTickets extends ConsoleProgram {
public void run() { //Getting user inputs int numDigits=readInt("Specify number of digits:"); int numTickets=readInt("Specify number of tickets:"); double minMean=readDouble("Specify min value for means:"); double maxMean=readDouble("Specify max value for means:"); double ratio=readDouble("Ratio(0-1):"); //Computing number of tickets in each class int numMarked=(int)(numTickets*ratio); int numRegular=numTickets-numMarked; //Creating counters int countMarked=0; int countRegular=0; //Main loop: since we do not know how many tickets should be created // we should use a while loop while(countRegular<numRegular || countMarked<numMarked){ //Create a ticket, and compute mean of its digits String ticket=""; int total=0; for(int i=0;i<numDigits;i++){ int randInt=rgen.nextInt(0,9); ticket=ticket+randInt; total=total+randInt; } double mean=1.0*total/numDigits; //Checking if ticket is marked(i.e. belongs to the hidden class) if(mean>minMean && mean<maxMean && countMarked<numMarked){ println(ticket+"-marked"); countMarked=countMarked+1; }else{ if(countRegular<numRegular){ println(ticket); countRegular=countRegular+1; println("Number of marked tickets: "+countMarked); println("Number of regular tickets: "+countRegular); private RandomGenerator rgen=RandomGenerator.getInstance();
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.