enter image description here
so i have this problem to solve it's working without any errors but the number of a and f isn't right they give me a false answer. the photo show the problem and we can't calculate f and a in the method genarategrades
static int genarategrades(int n) {
int a = 0;
for (int i = 0; i < n; i++) {
a = (int) (Math.random() * (100 - 50)) + 50;
System.out.print(a + " ");
}
return a;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n;
do {
System.out.print("Enter a positive value for n: ");
n = input.nextInt();
} while (n <= 0);
System.out.print("Genarate Grades: ");
int grade = genarategrades(n);
int A = 0;
int F = 0;
for (int i = 0; i < grade; i++) {
if (grade < 60)
F++;
else if (grade > 90)
A++;
}
System.out.println();
System.out.println("NB of A students: " + A);
System.out.println("NB of F students: " + F);
System.out.println("Percentage A: " + A + "/" + n + " = " + (double) A / n);
System.out.println("Percentage F: " + F + "/" + n + " = " + (double) F / n);
}
}
Call the genarateGrades() inside the loop if you don't want to use an array, like this:
static int genarateGrades(int n) {
int a = 0;
a = (int) (Math.random() * (100 - 50)) + 50;
System.out.print(a + " ");
return a;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n;
do {
System.out.print("Enter a positive value for n: ");
n = input.nextInt();
} while (n <= 0);
System.out.print("Generated Grades: ");
int A = 0;
int F = 0;
for (int i = 0; i < n; i++) {
int grade = genarateGrades(n);
if (grade < 60)
F++;
else if (grade > 90)
A++;
}
input.close();
System.out.println();
System.out.println("NB of A students: " + A);
System.out.println("NB of F students: " + F);
System.out.println("Percentage A: " + A + "/" + n + " = " + (double) A / n);
System.out.println("Percentage F: " + F + "/" + n + " = " + (double) F / n);
}
, output
Enter a positive value for n: 5
Generated Grades: 59 75 80 98 79
NB of A students: 1
NB of F students: 1
Percentage A: 1/5 = 0.2
Percentage F: 1/5 = 0.2
Related
I have a program that finds the mode of an array. Here is the code:
System.out.print("How many numbers do you want to find the mode of? ");
double[] inputs = new double[scanner.nextInt()];
for(int i = 0; i < inputs.length; i++) {
System.out.println("Input number " + i);
inputs[i] = (double) scanner.nextDouble();
}
Arrays.sort(inputs);
int count2 = 0;
int count1 = 0;
double popular1 = 0;
double popular2 = 0;
//finds most popular value in the array
for(int i = 0; i < inputs.length; i++) {
popular1 = inputs[i];
count1 = 1;
for(int j = i + 1; j < inputs.length; j++) {
if(popular1 == inputs[j]) {
count1++;
}
}
if(count1 > count2) {
popular2 = popular1;
count2 = count1;
} else if(count1 == count2) {
popular2 = Math.min(popular2, popular1); //when there are two winners, the lowest one is the mode
}
}
finalans = "The mode of those " + inputs.length + " numbers is " + popular2 + ", with a staggering " + count1 + " appearances.";
return finalans;
However when ran, I'm not getting the desired output. 1st, it doesn't count correctly, 2nd, it doesn't always detect which number is the mean. Just in case it is necessary, the full program is below.
import java.util.Arrays;
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean again = true;
while(again == true) {
System.out.println(runMath());
System.out.println("Do you want to run again? true or false");
again = scanner.nextBoolean();
}
scanner.close();
}
public static String runMath() {
double solution1;
double solution2;
String finalans;
Scanner scanner = new Scanner(System.in);
System.out.println("Operation ID? 1 = Add, 2 = Sub, 3 = Mul, 4 = Div, 5 = Ave");
int ID = scanner.nextInt();
solution1 = 0;
solution2 = 0;
switch (ID) {
case 1: {System.out.println("How many numbers do you want to add?");
double[] inputs = new double[scanner.nextInt()];
for(int i = 0; i < inputs.length; i++) {
System.out.println("Input number " + i);
inputs[i] = (double) scanner.nextDouble();
}
for(int i = 0; i < inputs.length; i++) {
solution1 += inputs[i];
}
finalans = "The sum of those " + inputs.length + " numbers is " + solution1;
return finalans;}
case 2: {System.out.println("How many numbers do you want to subtract?");
System.out.println("All numbers are subtracted from the first number, in inputted order.");
double[] inputs = new double[scanner.nextInt()];
for(int i = 0; i < inputs.length; i++) {
System.out.println("Input number " + i);
inputs[i] = (double) scanner.nextDouble();
}
solution1 = inputs[0];
for(int i = 1; i < inputs.length; i++) {
solution1 -= inputs[i];
}
finalans = "The difference of those " + inputs.length + " numbers is " + solution1;
return finalans;}
case 3: {System.out.println("How many numbers do you want to multiply?");
double inputs[] = new double[scanner.nextInt()];
for(int i = 0; i < inputs.length; i++) {
System.out.println("Input number " + i);
inputs[i] = (double) scanner.nextDouble();
}
solution1 = inputs[0];
for(int i = 1; i < inputs.length; i++) {
solution1 *= inputs[i];
}
finalans = "The product of those " + inputs.length + " numbers is " + solution1;
return finalans;}
case 4: {System.out.println("How many numbers do you want to divide?");
System.out.println("All numbers are subtracted from the first number, in inputted order.");
double[] inputs = new double[scanner.nextInt()];
for(int i = 0; i < inputs.length; i++) {
System.out.println("Input number " + i);
inputs[i] = (double) scanner.nextDouble();
}
solution1 = inputs[0];
for(int i = 1; i < inputs.length; i++) {
solution1 /= inputs[i];
}
finalans = "The quotient of those " + inputs.length + " numbers is " + solution1;
return finalans;}
case 5: {System.out.println("How many numbers do you want to average?");
double[] inputs = new double[scanner.nextInt()];
for(int i = 0; i < inputs.length; i++) {
System.out.println("Input number " + i);
inputs[i] = (double) scanner.nextDouble();
}
for(int i = 0; i < inputs.length; i++) {
solution1 += inputs[i];
}
solution1 /= inputs.length;
finalans = "The mean of those " + inputs.length + " numbers is " + solution1;
return finalans;}
case 6: {System.out.println("How many numbers do you want to find the median of?");
double[] inputs = new double[scanner.nextInt()];
for(int i = 0; i < inputs.length; i++) {
System.out.println("Input number " + i);
inputs[i] = (double) scanner.nextDouble();
}
Arrays.sort(inputs);
if(inputs.length % 2 == 0) {
solution1 = (inputs[inputs.length / 2] + inputs[inputs.length / 2 - 1]) / 2;
} else solution1 = inputs[inputs.length / 2];
finalans = "The median of those " + inputs.length + " numbers is " + solution1;
return finalans;}
case 7: {System.out.println("How many numbers do you want to find the mode of? ");
double[] inputs = new double[scanner.nextInt()];
for(int i = 0; i < inputs.length; i++) {
System.out.println("Input number " + i);
inputs[i] = (double) scanner.nextDouble();
}
Arrays.sort(inputs);
int count2 = 0;
int count1 = 0;
double popular1 = 0;
double popular2 = 0;
//finds most popular value in the array
for(int i = 0; i < inputs.length; i++) {
popular1 = inputs[i];
count1 = 1;
for(int j = i + 1; j < inputs.length; j++) {
if(popular1 == inputs[j]) {
count1++;
}
}
if(count1 > count2) {
popular2 = popular1;
count2 = count1;
} else if(count1 == count2) {
popular2 = Math.min(popular2, popular1); //when there are two winners, the lowest one is the mode
}
}
finalans = "The mode of those " + inputs.length + " numbers is " + popular2 + ", with a staggering " + count1 + " appearances.";
return finalans;}
default: return "ur mom";
}
}
}
Thank you to any geniuses that might solve this!
One thing I've noticed in your code that you use count1 as a temporary container for your current loop. So you should use count2 in your finalans instead of count1.
finalans = "The mode of those " + inputs.length + " numbers is " + popular2 + ", with a staggering " + count2 + " appearances.";
I saw this code on the internet and decided to try it myself, but I've been wondering, how do you print out the addition of the "fibonacci"?
package fibonacci;
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
int k, n, a = 1, b = 1;
k = 0;
System.out.println("input number: ");
n = sc.nextInt();
System.out.print("0 1 1 ");
while (k <= n) {
k = a + b;
if (k >= n) break;
System.out.print(k + " " );
a = b;
b = k;
}
System.out.println("Sum of 0 + 1 = 1");
System.out.println("Sum of 1 +" + a + " = " + b);
}
}
How can you generate an output like this:
0 1 1 2 3 5 8
0 + 1 = 1
1 + 1 = 2
1 + 2 = 3
2 + 3 = 5
3 + 5 = 8
this should answer your question:
package com.example.demo;
import java.util.Scanner;
public class Fibonaccci {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
int k, n, a = 0, b = 1;
k = 0;
System.out.println("input number: ");
n = sc.nextInt();
System.out.print("0 1 ");
StringBuffer acumResults= new StringBuffer("\n");
while (k <= n) {
k = a + b;
acumResults.append(a+" + "+b+" = "+k+"\n");
System.out.print(k + " " );
if (k >= n) break;
a = b;
b = k;
}
System.out.println(acumResults);
}
}
Start a string with the initial calculation (0 + 1 = 1) and then append to it in each iteration of the loop the current calculation i.e.
System.out.print("0 1 1 ");
String addition = "0 + 1 = 1\n";
while (k <= n) {
k = a + b;
addition += a+ " + " +b + " = " + k + "\n";
if (k >= n) break;
System.out.print(k + " " );
a = b;
b = k;
}
System.out.println();
System.out.println(addition);
To produce exactly your output I would code the following:
package fibonacci;
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
public class Fibonacci {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
int k, n, a = 1, b = 1;
k = 0;
List<Integer> numbers = new ArrayList<Integer>();
System.out.println("input number: ");
n = sc.nextInt();
System.out.print("0 1 1 ");
numbers.add(1);
numbers.add(1);
while (k <= n) {
k = a + b;
if (k >= n) break;
System.out.print(k + " " );
a = b;
b = k;
numbers.add(k);
}
// used for loop since I don't know your Java version
System.out.println();
int oldSum = 0;
for (int i= 0; i < numbers.size(); ++i) {
int element = numbers.get(k);
System.out.println oldSum + " + " + element + " = " + (oldSum + element);
oldSum += element;
}
}
}
I would like improve this code to OOP in Java. How can I for example return value from variable avg or Can I put ArrayList in method parameters?
Thank you in advance
class ArrayTester {
private double sum;
public void getAverageNotes() {
List < Integer > theBigList = new ArrayList < Integer > ();
Random theGenerator = new Random();
for (int n = 0; n < 4; n++) {
theBigList.add(theGenerator.nextInt(6) + 1);
}
if (theBigList.size() > 2) {
int max = Collections.max(theBigList);
int min = Collections.min(theBigList);
theBigList.remove(Integer.valueOf(max));
theBigList.remove(Integer.valueOf(min));
System.out.println(theBigList);
for (int n = 0; n < theBigList.size(); n++) {
System.out.println("New note " + n + " of the Informatics is: " + theBigList.get(n));
sum = sum + theBigList.get(n);
}
System.out.println("Collection size is: " + theBigList.size() + "\nExtreme values are: " + min + " and " + max);
double avg = Math.round(sum) / (double) theBigList.size();
System.out.println("Average: " + String.format("%.2f", avg));
} else {
System.out.println("to small");
}
You can return the average by returning the avg once it has been calculated. If there are less than 2 items, it will return -1 as a result.
public double getAverageNotes() {
List<Integer> theBigList = new ArrayList<Integer>();
Random theGenerator = new Random();
for (int n = 0; n < 4; n++) {
theBigList.add(theGenerator.nextInt(6) + 1);
}
if (theBigList.size() > 2) {
double avg = 0;
int max = Collections.max(theBigList);
int min = Collections.min(theBigList);
theBigList.remove(Integer.valueOf(max));
theBigList.remove(Integer.valueOf(min));
System.out.println(theBigList);
int sum = 0;
for (int n = 0; n < theBigList.size(); n++) {
System.out.println("New note " + n + " of the Informatics is: " + theBigList.get(n));
sum = sum + theBigList.get(n);
}
System.out.println("Collection size is: " + theBigList.size() + "\nExtreme values are: " + min + " and " + max);
avg = Math.round(sum) / (double) theBigList.size();
System.out.println("Average: " + String.format("%.2f", avg));
return avg;
} else {
System.out.println("to small");
return -1;
}
}
Alternatively, if you wanted to put the ArrayList as a parameter, you can change the method signature:
public double getAverageNotes(List<Integer> list){
List<Integer> theBigList = list;
...
and define the array list to be passed in:
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
Random theGenerator = new Random();
for (int n = 0; n < 4; n++) {
list.add(theGenerator.nextInt(6) + 1);
}
System.out.println(getAverageNotes(list));
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Please enter the max number:");
int max = input.nextInt();
int[]arr1 = new int[max+1];
int[]arr2 = new int[max+1];
int[]arr3 = new int[max+1];
int i = 1;
// For-loop to calculate
for (i = 1;i <= max;i++)
arr1[i] = arr1[i-1] + i;
i = 1;
// While-loop to calculate
while (i <= max) {
arr2[i] = arr2[i-1] + i;
i++;
}
i = 1;
// Do-While-loop to calculate
do
arr3[i] = arr3[i-1] + i;
while (++i <= max);
for (i = 0; i <= max; i++)
System.out.println("Arr1 " + arr1[i] + " Arr2 " + arr2[i] + " Arr3 " + arr3[i]);
System.out.println("Sum of All is " + arr1[max]);
}
I have this for doing sums but I am stuck when it comes to getting it to square
You seem to have 3 identical array objects?
Anyway, it's pretty straightforward to print the square of all numbers from 1 to max:
for (int i = 1; i <= max; i++) {
System.out.println(i + ": " + i * i);
}
There are also some fun ways to sum up the numbers from 1 to max, such as:
System.out.println(IntStream.range(1, max + 1).sum());
How do you find the minimum and maximum to see if they match? But the thing I'm finding the minimum and maximum for integers that are not an array. I need to find the minimum and maximum of the averages and compare them. So far I have this:
int i;
int totalall = 0;
int total1 = 0;
int total2 = 0;
int total3 = 0;
int total4 = 0;
int min1 = Integer.MAX_VALUE, minIndex = 0;
int max1 = Integer.MIN_VALUE, maxIndex = 0;
int average1 = 0;
int average2 = 0;
int average3 = 0;
int average4 = 0;
System.out.print("Please enter the sample size: ");
int max = input.nextInt();
int[]arr0 = new int[max + 1];
int[]arr1 = new int[max + 1];
int[]arr2 = new int[max + 1];
int[]arr3 = new int[max + 1];
System.out.println("Enter numbers for Trial 0 ");
for (i = 1;i <= max;i++){
System.out.print("Enter sample #" + (i-1) + ":");
arr0[i-1]= input.nextInt();
total1 = total1 + arr0[i-1];
}
System.out.println("Enter numbers for Trial 1 ");
for (i = 1; i <= max; i++){
System.out.print("Enter sample #" + (i-1) + ":");
arr1[i-1] = input.nextInt();
total2 = total2 + arr1[i-1];
}
System.out.println("Enter numbers for Trial 2 ");
for (i = 1; i <= max; i++){
System.out.print("Enter sample #" + (i-1) + ":");
arr2[i-1] = input.nextInt();
total3 = total3 + arr2[i-1];
}
System.out.println("Enter numbers for Trial 3 ");
for (i = 1; i <= max; i++){
System.out.print("Enter sample #" + (i-1) + ":");
arr3[i-1] = input.nextInt();
total4 = total4 + arr3[i-1];
}
totalall += total1 + total2 + total3 + total4;
average1 = total1 / (i-1);
average2 = total2 / (i-1);
average3 = total3 / (i-1);
average4 = total4 / (i-1);
System.out.println("\tSample #\tTrial 0\tTrial 1\tTrial 2\tTrial 3");
System.out.println("\t\t" + arr3[max] + "\t" + arr0[i-2] + "\t" + arr1[i-2] + "\t" + arr2[i-2] + "\t" + arr3[i-2]);
System.out.print("Average: \t\t");
System.out.println(average1 + "\t" + average2 + "\t" + average3 + "\t" + average4);
for (i=1; i < average; i++);
}
}
Perhaps this:
var averages = new List<int>(){average1,average2,average3,average4};
var maxAvg = averages.Max();
var minAvg = averages.Min();
do you need this?
import java.util.Scanner;
public class test {
public static void main(String[] args) {
int max = 0;
int min = 0;
int total = 0;
int avg = 0;
float count = 0;
String input = "";
Scanner scanner = new Scanner(System.in);
while (!(input = scanner.nextLine()).equals("end")) {
if(max < Integer.parseInt(input))
max = Integer.parseInt(input);
else if(min > Integer.parseInt(input))
min = Integer.parseInt(input);
if(count == 0)
max = min = Integer.parseInt(input);
total += Integer.parseInt(input);
count++;
}
System.out.println("max: " + max);
System.out.println("min: " + min);
System.out.println("total: " + total);
System.out.println("avg: " + total / count);
}
}
To find the minimum of a list of variables (which are not an array) you can let java syntax help you:
The NumberUtils class comes from Apache Commons-Lang library
public class ArrayTest {
public static int min(int ... value)
{
return org.apache.commons.lang.math.NumberUtils.min(value);
}
public static int max(int ... value)
{
return org.apache.commons.lang.math.NumberUtils.max(value);
}
public static void main(String[] args) {
int average1=4, average2=20, average3=40;
System.out.println("minimum:"+min(average1,average2,average3));
System.out.println("maximum:"+max(average1,average2,average3));
}
}