Why it doesn't return any value? - java

import java.util.Scanner;
public class MarkCalculator {
public static int[] exam_grade = new int[6];
public static int[] coursework_grade = new int[6];
public static int[] coursework_weight = new int[2];
public static int[] module_points = new int[6];
public static String[] module_grade = new String[20];
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
for (int i=0; i<3; i++){
System.out.printf(i+". Modelue"+" Enter grade of exam:");
exam_grade[i]=input.nextInt();
System.out.printf(i+". Modelue"+" Enter grade of coursework:");
coursework_grade[i]=input.nextInt();
}
for(int i = 0 ;i < 3; i++){
System.out.println(exam_grade[i]+" "+coursework_grade[i]);
}
computeMark(module_points, coursework_grade, exam_grade);
for(int i = 0 ;i < 3; i++){
System.out.println(module_points[i]);
}
input.close();
}
public static int[] computeMark (int coursework_grade[], int exam_grade[], int module_points[]){
coursework_weight[0]= 50;
coursework_weight[1]= 50;
for(int i=0;i<3;i++){
if (coursework_grade[i] < 35 || exam_grade[i] < 35){
module_points[i]=((coursework_grade[i]*coursework_weight[0] + (exam_grade[i]*(100-coursework_weight[1])))/100);
}
}
return module_points;
}
}
I wonder why it doesn't return any value. The function module_points worked few days ago and now I just can't find any error there. The output is all the time 0 only. Anyone can help pls? Thank you.I bet it's just something simple but really stuck at this point. What i need to do is: based on input (coursework_grade and exam_grade) count the module_points (formula is given), store these values in the array and return this array to the main method where is this array showed. Thanks for help guys.

You are calling the function computeMark() without storing the returned result.
Change:
computeMark(module_points, coursework_grade, exam_grade);
To: [edited]
module_points = computeMark(coursework_grade, exam_grade, module_points);

I guess you got 0 with divide by 100. Declare module_points[] as double[] to get the fraction value.

see the part:
if (coursework_grade[i] < 35 || exam_grade[i] < 35)
{
module_points[i]= ......
}
This means all your inputs must be >= 35 Otherwise no association of module_point array elements will occur.

Related

How to use create 2 Array with User input with Std.readAllInts()

This is how it should work, i Put in put for the 1 st array like: 2 3 1 2 3 4 5 6 then 2 and 3 are row and colum and the rest are the values. Problem is the 1st array work, but when i reach EOF ( ctrl+z) then there is out of bound exception. Which mean i cant input value for the 2nd Array like the 1st one. I know there is anotherway where that i can declare array size first then value. But how could i fix this f i still want to usr StdIn.readAllInts() ?
public class MatrixMult {
public static void main(String[] args){
System.out.println("First Matrix Config");
int[] einGabeMatrix1= StdIn.readAllInts();
int zeileM1 = einGabeMatrix1[0];
int spalteM1 = einGabeMatrix1[1];
int[][] ersteMatrix= new int [zeileM1][spalteM1];
int k=2;
int sum;
for(int i=0;i<zeileM1-1;i++){
for(int j=0;j<spalteM1-1;j++){
ersteMatrix[i][j]=einGabeMatrix1[k];
k++;
}
}
System.out.println("Second Matrix Config");
int[] einGabeMatrix2 = StdIn.readAllInts();
int zeileM2 = einGabeMatrix2[0];
int spalteM2 = einGabeMatrix2[1];
int h=2;
int[][] zweiteMatrix= new int [zeileM2][spalteM2];
for(int m=0;m<zeileM2-1;m++){
for(int n=0;n<spalteM2-1;n++){
zweiteMatrix[m][n]=einGabeMatrix2[h];
h++;
}
}
int[][] ergebnisMatrix= new int [zeileM1][spalteM2];
for (int t = 0; t < zeileM1; t++) {
for (int c = 0; c < spalteM2; c++) {
sum = 0;
for (int d = 0; d < spalteM1; d++) {
sum = sum + ersteMatrix[t][d] * zweiteMatrix[d][c];
}
ergebnisMatrix[t][c] = sum;
}
}
for(int i=0;i<zeileM1;i++){
for(int j=0;j<spalteM1;j++){
System.out.println(ergebnisMatrix[i][j]);
}
}
}
}
// This is StdIn.readAllInts(), standard method by java.
public static int[] readAllInts() {
String[] fields = readAllStrings();
int[] vals = new int[fields.length];
for (int i = 0; i < fields.length; i++)
vals[i] = Integer.parseInt(fields[i]);
return vals;
}
It looks like the issue is coming from the fact that StdIn.readAllInts() reads until the EOF. There will be no values left to read by the time your code gets to the second call.
I would suggest instead using the StdIn.readInt() call to read each integer one at a time and then you can use it within your loop to read the exact number of values your need.
Here is an example of how you could get the first 2 integers to find your matrix size:
int zeileM1 = StdIn.readInt();
int spalteM1 = StdIn.readInt();
You will also need to apply this method in your for loops to read the data into the matrix.

How to use previous parameter from array in for loop

I have been try to code this, to increment the user input by 33 if the input is "31" or more up to the input or "90", and I have hit a wall where I want to use the user input from the array but I cannot. Could anyone help? Thank you.
if (choice.equals("R")) {
System.out.println("You have selected to draw a Rectangle!");
System.out.println("Please enter the Height and Width of the rectangle that is within 30cm - 90cm: ");
int[] array = new int[2];
int[] array2 = new int[2];
Scanner scan = new Scanner(System.in);
String line1 = scan.nextLine();
String[] numbers1 = line1.split(" ");
for(int i=0;i<numbers1.length;i++){
array[i] = Integer.parseInt(numbers1[i]);
}
I am trying to make a method to be able to easily call upon it later on, but that's the problem as I cannot complete my calculation.
public static void timeTurn (int a, int b) {
for(int i = 1000; i < 3001; i+= 33) {
if(numbers1.equals(>=30)) {
}
}
}
int[] array2 = new int[2]; is useless here.
By the way, please do not name a variable "array" or "array2". It's hard to understand for others and for yourself in the future.
if you defined the "array"s length to 2, then this for loop for(int i=0;i<numbers1.length;i++){...} is meaningless cause you already defined that you can only have two integers in this array.
I do not know the meaning of "convert cm to milliseconds" since one is distance and the other is time...
I can not understand (int a, int b) what "a", "b" means.
by my understanding the second code should be:
public static void timeTurn (int[] length) {
int minMillsecond = 1000;
int maxMillsecond = 3001;
int gap = 33;
for(int len : length){
if (30 <= len <= 90){
len += minMillsecond;
while(len < 3000){ len+=gap;}
print len;
}
}
}

simple Java 2D Array with input

I'm new to Java and got some work at school.
Now I don't know how to continue and I hope you can help me.
That's my code till now
package bp;
import java.util.Scanner;
public class BenzinP {
private int row = 4;
private int col = 4;
private int[][] matrix;
public BenzinP(int trow, int tcol) {
this.row = trow;
this.col = tcol;
}
public BenzinP(int trow, int tcol, int[][] m) {
this.row = trow;
this.col = tcol;
this.matrix = m;
}
public int[][] fill(){
int[][] data = new int[row][col];
Scanner in = new Scanner(System.in);
for(int row = 0; row< matrix.length; row++){
for(int col = 0 ;col< matrix.length; col++){
System.out.println("Date");
data[row][0] = in.nextInt();
System.out.println("Price");
data[row][1] = in.nextInt();
}
System.out.println();
}
for(int row = 0; row< matrix.length; row++){
for(int col = 0 ;col< matrix[row].length; col++){
System.out.print(data[row][col]);
}
System.out.println();
}
return data;
}
public static void main(String[] args){
int[][] ma = new int[3][2];
BenzinP q2 = new BenzinP(3, 2,ma);
q2.fill();
}
}
The task is:
Create a Java program which can save the prices for gas for different days and can be outputted in different formats (sort for highes price or date).
You should use a 2D Array which can safe data for at least 30 days.
Also it should show the average, min. and max price.
Also the program should be outsourced in methodes etc.
I hope you can help and keep it simple.
I will make some assumptions in order to be able to answer this:
if BenzinP is a class, that should not contain the main method. Only the main class in your project should contain the main, since that is what the JVM expects to be the "point of entry" in your code.
public static void main(String[] args)
Since this is an exercise, dont worry so far about this.
Then, the BenzinP class has 2 constructors that are not really needed.
I suggest the following:
BenzinP(){
this.matrix=new int[this.row][this.col];
}
Create the rest of the methods you want, such as query the highest value, lowest, etc.. and start your main
public static void main(String[] args){
BenzinP q2 = new BenzinP();
q2.fill();
q2.getHighest();
// q2.getLowest();
// q2.getEtc();
}
As a suggestion, in order to search the highest and lowest value, iterate your array and compare that value with an initialized one, if its bigger/smaller, update the value, at the end of the array, return the initialized value:
public int getHighest(){
int max = -2147483648; // the lowest possible integer.
for(int i=0 i<this.row;i++){
if(matrix[i][1]>max){
max = matrix[i][1];
}
}
return max;
}

ArrayList giving problems in java. positive integers solution to x+y+z+w = 13

So i am creating a method that basically gives all possible positive integer solutions to the problem x+y+z+w = 13. Really I have designed a program that can get all possible positive integer solutions to any number using any number of variables. I have managed to obtain the solution using this method:
public class Choose {
public static ArrayList<int[]> values;
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] loops = new int[3];
int q = 0;
values = new ArrayList<int[]>();
int[] array = new int[4];
System.out.println(choose(12,3));
NestedLoops(3,10,0,loops,13,array, 0);
for(int i = 0; i < values.size(); i++){
printArray(values.get(i));
}
}
public static void NestedLoops(int n, int k, int j,
int[] loops, int q, int[] array, int g){
if(j==n){
for(int i = 0; i< n; i++){
q-=loops[i];
}
if(q>0){
for(int i = 0; i < n; i++){
array[i] = loops[i];
}
array[n] = q;
values.add(array);
}
return;
}
for(int count = 1; count <= k; count++){
loops[j] = count;
NestedLoops(n,k,j+1,loops, 13, array, g);
}
}
}
My problem is that when i go to print the ArrayList, all i get is the last value repeated again and again. When i try to just print out the values instead of storing them in the ArrayList it works totally fine. This makes me think that the problem is with the values.add(array); line but i don't know how to fix it or what i am doing wrong. Thanks for any help offered.
Try using:
values.add(array.clone());
Every add of the same array just points to that array object. As you keep changing the same object, the final state is what is being shown for all stored elements. The print works as it just dumps the state of the array at that particular instant.

How to generate random numbers up to 100 in an array size of 10

I have to create an array size of 10 and generate random numbers from 0 to 100 including 0 and excluding 100. When I write the code it keeps giving me an error of:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 59
at BillyLancasterHw6.printArray(BillyLancasterHw6.java:23)
at BillyLancasterHw6.main(BillyLancasterHw6.java:13)
Here is the code I am using.
public class BillyLancasterHw6 {
public static void main(String[] args){
//int N = 10;
double[] list = new double[10];
for(int i = 0; i < list.length; i++) {
double randomNumber = (Math.random() * 100);
list[i] = randomNumber;
}
printArray(list);
//sort(list);
//System.out.println();
//printArray(list);
}
public static void printArray(double[] list) {
for(double u: list) {
System.out.printf("%2.2f%s", list[(int) u], " ");
}
}
}
I am not understanding why I cannot generate random numbers up to 100 in an array of size 10. Meaning that 10 numbers are randomly generated between 0 and 100.
Any suggestions would be great. If you can reference where in the documentation I can find the answers as well. I am new to programming and am having trouble with this.
Your enhanced for loop has already done the job of extracting the random number for you out of the list; just print it. There's no need to go back to the list.
System.out.printf("%2.2f%s", u, " ");
I'm a bit rusty on my java but:
for(double u: list) {
System.out.printf("%2.2f%s", list[(int) u], " ");
}
looks suspicious. What about this:
for(double u: list) {
System.out.printf("%2.2f%s", u, " ");
}
Maybe I'm not getting your question but see if the code below work's. I don't understand why your using double
public class BillyLancasterHw6 {
public static void main(String[] args)
{
int[] randomNumber = new int[10];
for(int i = 0; i < randomNumber.length; i++)
{
randomNumber[i] = (int)(Math.random() * 100);
System.out.print(randomNumber[i]+" , ");
}
System.out.println();
}
}

Categories