The code displays 0 instead of every 3rd input in the given values. What should I do in order to print out the 5 3 8 6 in the input?
The output should be like this (burst time reference only):
import java.util.Scanner;
public class PreemptivePriorityScheduling {
static int[] processId, arrivalTime, burstTime, priority, waitingTime, turnAroundTime;
static int n;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of processes: ");
n = input.nextInt();
processId = new int[n];
arrivalTime = new int[n];
burstTime = new int[n];
priority = new int[n];
waitingTime = new int[n];
turnAroundTime = new int[n];
getInput(input);
computeWaitingTime();
computeTurnAroundTime();
displayTable();
displayAverages();
}
public static void getInput(Scanner input) {
for (int i = 0; i < n; i++) {
System.out.print("Enter process ID, arrival time, burst time, and priority for process " + (i + 1) + ": ");
processId[i] = input.nextInt();
arrivalTime[i] = input.nextInt();
burstTime[i] = input.nextInt();
priority[i] = input.nextInt();
}
}
public static void computeWaitingTime() {
int complete = 0, minPriority, minIndex = 0;
for (int i = 0; i < n; i++) {
minPriority = Integer.MAX_VALUE;
for (int j = 0; j < n; j++) {
if (arrivalTime[j] <= complete && priority[j] < minPriority && burstTime[j] > 0) {
minPriority = priority[j];
minIndex = j;
}
}
complete += burstTime[minIndex];
waitingTime[minIndex] = complete - arrivalTime[minIndex] - burstTime[minIndex];
burstTime[minIndex] = 0;
}
}
public static void computeTurnAroundTime() {
for (int i = 0; i < n; i++) {
turnAroundTime[i] = waitingTime[i] + burstTime[i];
}
}
public static void displayTable() {
System.out.println("Process ID\tArrival Time\tBurst Time\tPriority\tWaiting Time\tTurn Around Time");
for (int i = 0; i < n; i++) {
System.out.println(processId[i] + "\t\t" + arrivalTime[i] + "\t\t" + burstTime[i] + "\t\t" + priority[i] + "\t\t" + waitingTime[i] + "\t\t" + turnAroundTime[i]);
}
}
public static void displayAverages() {
int totalWaitingTime = 0, totalTurnAroundTime = 0;
for (int i = 0; i < n; i++) {
totalWaitingTime += waitingTime[i];
totalTurnAroundTime += turnAroundTime[i];
}
System.out.println("\nAverage Waiting Time: " + (float)totalWaitingTime / n);
System.out.println("Average Turn Around Time: " + (float)totalTurnAroundTime / n);
}
}
Related
import java.util.*;
public class Combination {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
String input = sc.next();
System.out.printf("You entered: %d\n", Integer.parseInt(input));
sc.close();
int[] arr = new int[100];
for (int i = 0; i < arr.length; i++) {
arr[i] = i + 1;
}
for (int i = 0; i <= 97; i++) {
int num_i = arr[i];
for (int j = i + 1; j <= 98; j++) {
int num_j = arr[j];
for (int k = j + 1; k <= 99; k++) {
int num_k = arr[k];
if (num_i + num_j + num_k == Integer.parseInt(input))
System.out.printf("(%d, %d, %d)", num_i, num_j, num_k);
}
}
}
}
}
When I get a number input, I want to make a code that represents this number as the sum of three numbers.
The code is complete, but there are several combinations. I want to print out only one combination. How can I edit it?
First, some important suggestions:
Do not parse input inside the nested loop as it will hit the performance. Do it once outside the nested loops.
Do not close Sacnner for System.in as it also closes System.in and there is no way to open it again without restarting JVM. It means that if it is being used in some other part of your application, your application will crash.
Always follow Java naming conventions e.g. you could name numJ instead of num_j.
Coming back to your problem, there are many ways to solve it and I have listed below just a couple of them:
Use break <<label>> to exit the nested loops:
import java.util.Scanner;
public class Combination {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
String input = sc.next();
System.out.printf("You entered: %d\n", Integer.parseInt(input));
int num = Integer.parseInt(input);
int[] arr = new int[100];
for (int i = 0; i < arr.length; i++) {
arr[i] = i + 1;
}
start: for (int i = 0; i <= 97; i++) {
int numI = arr[i];
for (int j = i + 1; j <= 98; j++) {
int numJ = arr[j];
for (int k = j + 1; k <= 99; k++) {
int numK = arr[k];
if (numI + numJ + numK == num) {
System.out.printf("(%d, %d, %d)", numI, numJ, numK);
break start;
}
}
}
}
}
}
A sample run:
Enter a number : 123
You entered: 123
(1, 22, 100)
Put the logic in a method and return:
import java.util.Scanner;
public class Combination {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
String input = sc.next();
System.out.printf("You entered: %d\n", Integer.parseInt(input));
int num = Integer.parseInt(input);
printFirstCombination(num);
}
static void printFirstCombination(int num) {
int[] arr = new int[100];
for (int i = 0; i < arr.length; i++) {
arr[i] = i + 1;
}
for (int i = 0; i <= 97; i++) {
int numI = arr[i];
for (int j = i + 1; j <= 98; j++) {
int numJ = arr[j];
for (int k = j + 1; k <= 99; k++) {
int numK = arr[k];
if (numI + numJ + numK == num) {
System.out.printf("(%d, %d, %d)", numI, numJ, numK);
return;
}
}
}
}
}
}
You can create a seperate function for that and after you find a combination, print it and return there and then to the main function. In case you didn't find a combination you return 1 which can be handled in the main function,
public class Combination {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
String input = sc.next();
System.out.printf("You entered: %d\n", Integer.parseInt(input));
int res = printCombination(input);
if(res == 1) {
// Do something
}
sc.close();
}
private static int printCombination(String input) {
int[] arr = new int[100];
for (int i = 0; i < arr.length; i++) {
arr[i] = i + 1;
}
for (int i = 0; i <= 97; i++) {
int num_i = arr[i];
for (int j = i + 1; j <= 98; j++) {
int num_j = arr[j];
for (int k = j + 1; k <= 99; k++) {
int num_k = arr[k];
if (num_i + num_j + num_k == Integer.parseInt(input)) {
System.out.printf("(%d, %d, %d)", num_i, num_j, num_k);
return 0;
}
}
}
}
return 1;
}
}
I'm currently learning Java and while I'm able to find the largest number I'm stuck on how to find it's location. Any help would be greatly appreciated!
import java.util.Random;
public class FindingLargestValueInAnArray {
public static void main(String[] args) {
Random number = new Random();
int[] array_1 = new int[10];
int i = 0;
for (i = 0; i < array_1.length; i++) {
int randNum = 1 + number.nextInt(99);
array_1[i] = randNum;
}
System.out.print("Array:");
for (i = 0; i < array_1.length; i++) {
System.out.print(" " + array_1[i]);
}
int largeNumb = 0;
for (i = 0; i < array_1.length; i++) {
if (array_1[i] > largeNumb) {
largeNumb = array_1[i];
}
}
System.out.println("\n\nThe largest value is
"+largeNumb);
}
}
public class FindingLargestValueInAnArray {
public static void main(String[] args) {
Random number = new Random();
int[] array_1 = new int[10];
int i = 0;
for (i = 0; i < array_1.length; i++) {
int randNum = 1 + number.nextInt(99);
array_1[i] = randNum;
}
System.out.print("Array:");
for (i = 0; i < array_1.length; i++) {
System.out.print(" " + array_1[i]);
}
int largeNumb = 0;
int index = 0;
for (i = 0; i < array_1.length; i++) {
if (array_1[i] > largeNumb) {
largeNumb = array_1[i];
index = i;
}
}
System.out.println("The largest value is " + largeNumb + " and it's location is" + index);
}
}
You should get an another variable for the index = 0 and then put it inside the if statement and make it equal to index = i;. After the loop ends the largest value index would be stored here.
you can make an int index=0
in the loop where you check if the number is bigger in the body of the if make infex=i. This way you will always have both the value and the index of the largest numnber.
import java.util.Random;
public class FindingLargestValueInAnArray {
public static void main(String[] args) {
Random number = new Random();
int[] array_1 = new int[10];
int i = 0;
for (i = 0; i < array_1.length; i++) {
int randNum = 1 + number.nextInt(99);
array_1[i] = randNum;
}
System.out.print("Array:");
for (i = 0; i < array_1.length; i++) {
System.out.print(" " + array_1[i]);
}
int largeNumb = array_1[0];
int index = 0;
for (i = 1; i < array_1.length; i++) {
if (array_1[i] > largeNumb) {
largeNumb = array_1[i];
index = i;
}
}
System.out.println("\n\nThe largest value is :" + largeNumb + " and it's index is: " + index);
}
}
just wanting to know how do i set an input range for the scanner object, which then transfers the values and inputs them into a 2d array.
I was able to set a range for random number generator but i do not know how to apply the same concept to my scanner object
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random random = new Random();
int rows = 3;
int columns = 5;
System.out.println("Enter array elements : ");
int arry1[][] = new int[rows][columns];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns-2; j++)
{
arry1[i][j] = sc.nextInt();
}
}
arry1[0][3] = random.nextInt(50-10+1)+10;
arry1[1][3] = random.nextInt(50-10+1)+10;
arry1[2][3] = random.nextInt(50-10+1)+10;
int sum1 = 0;
int sum2 = 0;
int sum3 = 0;
sum1 = arry1[0][0]+arry1[0][1]+arry1[0][2]+arry1[0][3];
sum2 = arry1[1][0]+arry1[1][1]+arry1[1][2]+arry1[1][3];
sum3 = arry1[2][0]+arry1[2][1]+arry1[2][2]+arry1[2][3];
arry1[0][4] = sum1;
arry1[1][4] = sum2;
arry1[2][4] = sum3;
System.out.print("DISPLAYING ARRAY:");
System.out.println();
for (int[] x1 : arry1) {
for (int y1 : x1) {
System.out.print(y1 + " ");
}
System.out.println();
}
}
}
Add checking if the value is within range.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random random = new Random();
int rows = 3;
int columns = 5;
System.out.println("Enter array elements : ");
int arry1[][] = new int[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns - 2; j++) {
int input;
do {
input = sc.nextInt();
} while (input < 0 || input > 3);
arry1[i][j] = input;
}
}
arry1[0][3] = random.nextInt(50 - 10 + 1) + 10;
arry1[1][3] = random.nextInt(50 - 10 + 1) + 10;
arry1[2][3] = random.nextInt(50 - 10 + 1) + 10;
int sum1 = 0;
int sum2 = 0;
int sum3 = 0;
sum1 = arry1[0][0] + arry1[0][1] + arry1[0][2] + arry1[0][3];
sum2 = arry1[1][0] + arry1[1][1] + arry1[1][2] + arry1[1][3];
sum3 = arry1[2][0] + arry1[2][1] + arry1[2][2] + arry1[2][3];
arry1[0][4] = sum1;
arry1[1][4] = sum2;
arry1[2][4] = sum3;
System.out.print("DISPLAYING ARRAY:");
System.out.println();
for (int[] x1 : arry1) {
for (int y1 : x1) {
System.out.print(y1 + " ");
}
System.out.println();
}
}
I can't fix my code. It always says "cannot find symbol" error. I tried to search for some solution but still doesn't work.
import java.util.*;
public class NPP{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int numOfJobs;
float waitingTimeAve = 0, turnaroundTimeAve = 0;
System.out.println("Enter number of jobs : ");
numOfJobs = scan.nextInt();
Job[] job = new Job[numOfJobs]; // Line 15
for(int i = 0; i < numOfJobs; i++ ){
job[i] = new Job(); // Line 18
System.out.println("For job " + (i+1));
System.out.print("Enter arrival time: ");
job[i].setArrivalTime(scan.nextInt());
System.out.print("Enter burst time: ");
job[i].setBurstTime(scan.nextInt());
System.out.print("Enter priority: ");
job[i].setPriority(scan.nextInt());
System.out.println();
}
System.out.println("Type go.");
scan.next();
int jobNum = 0, jobAt = 1, jobBt = 2, jobPrio = 3;
int currentTime;
int [][]timeline = new int[numOfJobs][4];
import java.util.*;
public class NPP{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int numOfJobs;
float waitingTimeAve = 0, turnaroundTimeAve = 0;
System.out.println("Enter number of jobs : ");
numOfJobs = scan.nextInt();
Job[] job = new Job[numOfJobs]; //LINE15
for(int i = 0; i < numOfJobs; i++ ){
job[i] = new Job(); //LINE18
System.out.println("For job " + (i+1));
System.out.print("Enter arrival time: ");
job[i].setArrivalTime(scan.nextInt());
System.out.print("Enter burst time: ");
job[i].setBurstTime(scan.nextInt());
System.out.print("Enter priority: ");
job[i].setPriority(scan.nextInt());
System.out.println();
}
System.out.println("Type go.");
scan.next();
int jobNum = 0, jobAt = 1, jobBt = 2, jobPrio = 3;
int currentTime;
int [][]timeline = new int[numOfJobs][4];
for(int i = 0; i < numOfJobs; i ++){
timeline[i][jobNum] = i;
timeline[i][jobAt] = job[i].getArrivalTime();
timeline[i][jobBt] = job[i].getBurstTime();
timeline[i][jobPrio] = job[i].getPriority();
}
Arrays.sort(timeline, new Comparator<int[]>(){
public int compare(int[] o1, int[] o2) {
return Integer.compare(o1[jobAt], o2[jobAt]);
}
});
currentTime = timeline[0][jobAt];
int [][]ganttchart = new int [100][2];
int g = 0;
for(int i = 0; i < numOfJobs; i++){
int n = 0;
int temp = i;
if(timeline[i][jobAt] > currentTime){
int idle = timeline[i][jobAt] - currentTime;
currentTime += idle;
}
while(timeline[i][jobAt] <= currentTime){
n++;
i++;
if(i == numOfJobs){
break;
}
}
i = temp;
if(n > 1){
for(int j = i; j < n+i; j++){
for(int k = j; k < n+i; k++){
if(timeline[j][jobPrio] > timeline[k][jobPrio]){
int temp1 = timeline[j][jobNum];
int temp2 = timeline[j][jobAt];
int temp3 = timeline[j][jobBt];
int temp4 = timeline[j][jobPrio];
timeline[j][jobNum] = timeline[k][jobNum];
timeline[j][jobAt] = timeline[k][jobAt];
timeline[j][jobBt] = timeline[k][jobBt];
timeline[j][jobPrio] = timeline[k][jobPrio];
timeline[k][jobNum] = temp1;
timeline[k][jobAt] = temp2;
timeline[k][jobBt] = temp3;
timeline[k][jobPrio] = temp4;
}
}
}
}
int temp4 = timeline[i][jobNum];
currentTime += job[temp4].getBurstTime();
job[temp4].setEndTime(currentTime);
ganttchart[g][0] = timeline[i][jobNum];
ganttchart[g][1] = currentTime;
g++;
}
for(int i = 0; i < numOfJobs; i++){
int wt, tt;
tt = job[i].getEndTime() - job[i].getArrivalTime();
job[i].setTurnaroundTime(tt);
turnaroundTimeAve +=tt;
wt = job[i].getTurnaroundTime() - job[i].getBurstTime();
job[i].setWaitingTime(wt);
waitingTimeAve+=wt;
}
turnaroundTimeAve = turnaroundTimeAve/numOfJobs;
waitingTimeAve = waitingTimeAve/numOfJobs;
for(int i = 0; i < numOfJobs; i++){
System.out.println("Job " + (i+1));
System.out.println("End time is " + job[i].getEndTime());
System.out.println("Turnaround time is " + job[i].getTurnaroundTime());
System.out.println("Waiting time is " + job[i].getWatingTime());
System.out.println();
}
System.out.println("Turnaround time average is " + turnaroundTimeAve);
System.out.println("Waiting time average is " + waitingTimeAve);
System.out.println("Gantt Chart: ");
for( int i = 0; i < g; i++){
System.out.println("Job " + (ganttchart[i][0]+1) + " end time of " + ganttchart[i][1]);
}
}
}
I'm trying to write code that checks for repeat numbers in an array. If numbers repeat within the same row, it should generate another random number to replace the number. This is my class/method that is meant to accomplish this goal:
import java.util.*;
public class NoRepeats
{
public static void clean(int ticks[][])
{
for(int i = 0; i < ticks.length; i++)
{
for(int j = 0; j < ticks[0].length; j++)
{
nums[j] = ticks[i][j];
}
for(int k = 0; k < nums.length; k++)
{
for(int count = k + 1; count < nums.length; count++)
{
int othNums = nums[count];
while(true)
{
if(nums[k] == othNums)
{
System.out.print("\n\n Repeat:" + nums[k] + k + " " + othNums + "\tTicket Number: " + (i+1) +"\n\n\n");
nums[k] = 1 + rndm.nextInt(48);
for(int counter = count; counter < nums.length; counter++)
{
int othNums2 = nums[count];
if(nums[k] == othNums2)
{
System.out.print("\n\n Repeat NUM2:" + nums[k] + " " + othNums2 + "\tTicket Number: " + (i+1) +"\n\n\n");
nums[k] = 1 + rndm.nextInt(48);
}
}
for(int l = 0; l < k; l++)
{
int othNums3 = nums[l];
if(nums[k] == othNums3)
{
System.out.print("\n\n RepeatNUM2: " + nums[k] + " " + othNums3 + "\tTicket Number: " + (i+1) +"\n\n\n");
nums[k] = 1 + rndm.nextInt(48);
}
}
}
else
{
break;
}
}
count++;
}
}
for(int q = 0; q < nums.length; q++)
{
ticks[i][q] = nums[q];
}
count = 0;
}
}
public static int nums[] = new int[6];
public static int count = 1;
public static Random rndm = new Random();
public static int numsMatched[] = new int[100];
}
For some reason, there are still matches and I don't know why. Can anyone find what I've done wrong?
You're incrementing count twice and thus skip entries:
for(int count = k + 1; count < nums.length; count++) //<- increment 1
{
int othNums = nums[count];
while(true)
{
//snip
}
count++; //<- increment 2
}
The problem probably originates from the fact that you have a static variable which is also called count and which will be shadowed by the count defined in the loop:
public static int count = 1;
So either remove the second increment or rename one of the variables.