I need to find the largest prime number of a given array when adding two numbers in an array,so I decided to add all possible sums first and displayed it. Now I want to take those output elements to a new array.Please help me solve this problem.
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int noOfElem = scanner.nextInt();
int[] array = new int[noOfElem];
int[][] newArray = new int[5][4];
int i=0;
while(scanner.hasNextInt()){
array[i] = scanner.nextInt();
i++;
if(i == noOfElem){
break;
}
}
for (int a = 0; a < array.length; a++)
{
for (int b = a+1; b < array.length; b++) {
int m = array[a] + array[b];
newArray[a][b] =
}
}
}
}
Not quite sure what the problem is here, just do
newArray[a][b] = m;
This stores all sums of all 'a's and 'b's such that newArray[a][b] is the sum of array[a] + array[b]
I have a question about stop array input limit then the result to show the output.
Below is my coding have already set new float[3][3]:
import java.util.Scanner;
public class Clone2Darray {
public static float[][] clone(float[][] a) throws Exception {
float b[][] = new float[a.length][a[0].length];
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
b[i][j] = a[i][j];
}
}
return b;
}
public static void main(String args[]) {
Scanner sc = new Scanner (System.in);
System.out.println ("Type nine float numbers two-dimensional array of similar type and size with line breaks, end by -1:");
float[][] a = new float[3][3];
for (int i=0; i<3; i++){
for (int j=0; j<3; j++){
String line = sc.nextLine();
if ("-1".equals(line)){
break;
}
a[i][j]=Float.parseFloat(line);
}
}
System.out.println("\n The result is:");
try {
float b[][] = clone(a);
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
System.out.print(b[i][j] + " ");
}
System.out.println();
}
} catch (Exception e) {
System.out.println("Error!!!");
}
}
}
The limit output is show me like below:
run:
Type float numbers in the two-dimensional array of similar type and size
with line breaks, end by -1:
5.33
9.33
63.33
6.36
3.55
7.25
2.33
3.66
The result is:
6.33 5.33 9.33
63.33 6.36 3.55
7.25 2.33 3.66
BUILD SUCCESSFUL (total time: 31 seconds)
My problem is want to stop limit float[3][3] and can unlimited key in the input until type -1 to stop the input. May I know how to remove the limit float[3][3] in the array? Hope anyone can guide me to solve my problem. Thanks.
At the point when you allocate memory for the two-dimensional array you have to tell the sizes of its elements, because memory will be allocated for that array and the amount of memory to be allocated must be known.
You can bypass this, by using some more dynamic types, like List and its popuplar implementation, ArrayList, even in a nested form. That's a nice thing to do, but then you will not have a "real" array.
The below code allows you to create the dynamic arrays.
import java.util.Scanner;
public class Clone2DArray {
public static float[][] clone(float[][] a) throws Exception {
float b[][] = new float[a.length][a[0].length];
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
b[i][j] = a[i][j];
}
}
return b;
}
public static void main(String args[]) {
Scanner sc = new Scanner (System.in);
System.out.println("enter row size");
int row = Integer.parseInt(sc.nextLine());
System.out.println("enter column size");
int column = Integer.parseInt( sc.nextLine());
System.out.println ("Type float numbers two-dimensional array of similar type and size with line breaks:");
float[][] a = new float[row][column];
for (int i=0; i<row; i++){
for (int j=0; j<column; j++){
String line = sc.nextLine();
if ("-1".equals(line)){
break;
}
a[i][j]=Float.parseFloat(line);
}
}
System.out.println("\n The result is:");
try {
float b[][] = clone(a);
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
System.out.print(b[i][j] + " ");
}
System.out.println();
}
} catch (Exception e) {
System.out.println("Error!!!");
}
}
}
Hi I am trying to realize an algorithm and realise the function: when one of the molecule numbers become 0, the whole while loop will end and will automatically proceed to the next chart-drawing step with JFreeChart.
Right now, after using "return", eclipse show no error and process the main method completely. However, i cannot see my chart showing out. Wondering if any steps goes wrong.
I want to show the molecule number of different species at different times. Each specie will represent a new line.
SoI create a new chart
static JPanel chartPanel;
and in the constructor I set something like(just follow online tutorial)
super("Line Chart of molecule numbers at different times");
add(chartPanel, BorderLayout.CENTER);
setSize(640, 480);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
in the main method I want to show my number entered and calculated in a line chart (didn't create another class as afraid of null pointer exception)
public static void main(String args[]) throws Exception {
//input the number of species
System.out.println("Enter the number of species:");
int n = sc.nextInt();
//input the number of reactions
System.out.println("Enter the number of reactions:");
int m = sc.nextInt();
//
int[][]matrixPre = new int[m][n];
enterMatrixDataPre(sc, matrixPre, m, n);
printMatrixPre(matrixPre, m, n);
//convert the 2d int to 2d double
double [][] matrixPre2 = new double[m][n];
for(int i = 0; i <m; i++)
{
for(int j = 0; j < n; j++)
matrixPre2[i][j] = (double) matrixPre[i][j];
}
RealMatrix PreMatrix = new Array2DRowRealMatrix(matrixPre2);
// remember to add space key when doing the typing
int[][]matrixPost = new int[m][n];
enterMatrixDataPost(sc, matrixPost, m, n);
printMatrixPost(matrixPost, m, n);
//convert the 2d int to 2d double
double [][] matrixPost2 = new double[m][n];
for(int i = 0; i <m; i++)
{
for(int j = 0; j < n; j++)
matrixPost2[i][j] = (double) matrixPost[i][j];
}
RealMatrix PostMatrix = new Array2DRowRealMatrix(matrixPost2);
//
RealMatrix matrixSubtract = PreMatrix.subtract(PostMatrix);
System.out.println("So the transpose matrix after subtraction is:\t"+matrixSubtract.transpose());
//input the default maxium time of the whole reaction
System.out.println("Enter the maxium time of the whole reaction:");
double Tmax =sc.nextDouble();
//input the name of all the species
System.out.println("Enter the name of all species");
String[] strs=new String[n];
for(int i = 0; i< n; i++) {
strs[i]=sc.nextLine();
}
//input the rate constant of all the reactions(the coefficient), must be a double
System.out.println("Enter the rate constant of each reaction:");
Double[] rate=new Double[m];
for(int r = 0; r< m; r++) {
rate[r]=sc.nextDouble();
}
//
Vector<Double> timeList = new Vector<Double>(0);
Vector<int[]> allStateList = new Vector<int[]>(0);
timeList.add(newTime);
//input the initial states of numbers of molecules
System.out.println("Enter the initial molecule numbers of all species:");
int[]state = new int[n];
for (int i = 0; i< n; i++)
{
state[i]=sc.nextInt();
}
allStateList.add(state);
while(newTime<Tmax) {
for(int loopIndex =0; loopIndex<allStateList.size(); loopIndex++) {
// calculate the hazard for each reaction and the general hazard
double[] h = new double[m];
H = 0;
try {
for(int i =0; i<m; i++) {
for(int j =0; j<n; j++) {
h[i]=rate[i]*CombinatoricsUtils.binomialCoefficientDouble(allStateList.get(loopIndex)[j],(int)(PreMatrix.getRowVector(i).toArray()[j]));
H +=h[i];
}
}
}
catch(NumberIsTooLargeException exceptionn) {
System.out.println("One of the species has been exhausted and there is no next firing");
return;
}
System.out.println("So the general hazard is:"+H);
// select a random reaction time
Random random = new Random();
double tau = (1*Math.log(1/random.nextDouble()))/H;
System.out.println("So the random reaction time is"+tau);
//put the newTime
newTime = timeList.get(loopIndex)+tau;
System.out.println("So the new reaction time is:" + newTime);
timeList.add(newTime);
//select a random reaction j
Random random2 = new Random();
int index =0;
for (int i=0; i<m; i++) {
if(h[i]>random2.nextDouble()*H)
index =i;
}
System.out.println("So the next simulated event is:"+index);
//Update the state
double[] vectorDoubleArray = matrixSubtract.transpose().getColumnVector(index).toArray();
int[] intArray = new int[n];
for (int i=0; i<n; i++) {
intArray[i] = (int) vectorDoubleArray[i];
}
int[] newState = new int[n];
int newS =0;
for (int p =0; p<n; p++){
newS= intArray[p]+allStateList.get(loopIndex)[p];
newState[p]=newS;
}
System.out.println("Right now the molecule number of all species are:"+Arrays.toString(newState));
allStateList.add(newState);
}
//close the scanner
sc.close();
}
}
with all the preparation done, I want to print the number of two vector list, timeList and allStateList using jfreechart
String chartTitle = "Line chart of molecule numbers";
String categoryAxisLabel = "time";
String valueAxisLabel = "molecule numbers";
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
int[] eachSpecieState = new int[allStateList.size()];
for (int i =0; i<n; i++) {
for (int j=0; j<allStateList.size(); j++) {
eachSpecieState[i]=allStateList.get(j)[i];
}
}
for (int i =0; i<m;i++) {
String series[]= new String[m];
series[i]=""+strs[i];
for(int k =0; k<n;k++) {
for (int j=0; j<allStateList.size(); j++) {
eachSpecieState[k]=allStateList.get(j)[k];
dataset.addValue(eachSpecieState[k],series[i+1],""+timeList.get(j));
}
}
}
JFreeChart chart = ChartFactory.createLineChart(chartTitle,
categoryAxisLabel, valueAxisLabel, dataset);
chartPanel = new ChartPanel(chart);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new StochasticProcess().setVisible(true);
}
});
however no chart shows. As I am not that good in swing language, can anybody help me point out if any parts go wrong?(the last plot and chart part of code) Thanks~
One of the simplest solutions would be to just add the break condition in a simple for Loop
for(int i =0; i < m && allStateList.get(loopIndex)[j]!=0; i++)
{
}
I've to admit that, without a specific knowledge, it's not easy for me to understand and debug the whole code; but, as a general idea, I saw that, a certain point, you wrote:
***while(allStateList.get(loopIndex)[j]==0) {
break;
}***
In my opinion, as general rule, if you want to check a condition, you must use an IF statement, e.g.: something like if(condition==true) { break; }
I think it will be useful for you.
So to solve the problem, I created a list of XYSeries and add a new name to each XYSeries inside the loop. After adding values to each XYSeries, use another loop to add them into the XYCollection.The question was asked a week ago and lots of parts have been modified to get final result.I will post the new version of whole code below to set as an example.
import java.util.*;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.lang.Math;
import org.apache.commons.math3.exception.NumberIsTooLargeException;
import org.apache.commons.math3.linear.Array2DRowRealMatrix;
import org.apache.commons.math3.linear.RealMatrix;
import org.apache.commons.math3.util.CombinatoricsUtils;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.block.BlockBorder;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
The main method part is here:
public class StochasticSimulationProcess extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
static int matrixPre[][];
static int matrixPost[][];
static int m;// the number of reactions
static int n;// the number of species
static double[] h;// the hazard for each reaction
static double[] rate;
static int[] state;
static double newTime;
static int max;
static double Tmax;// the overall reaction time
Vector<Double> timeList;
static double H;
Vector<int[]> allStateList;
static String[] strs;
XYSeriesCollection dataset;
public StochasticSimulationProcess(String s) {
super(s);
newTime = 0;
}
static Scanner sc = new Scanner(System.in);
public static void main(String args[]) throws Exception {
// input the number of species
System.out.println("Enter the number of species:");
int n = sc.nextInt();
// input the number of reactions
System.out.println("Enter the number of reactions:");
int m = sc.nextInt();
//
int[][] matrixPre = new int[m][n];
enterMatrixDataPre(sc, matrixPre, m, n);
printMatrixPre(matrixPre, m, n);
// convert the 2d int to 2d double
double[][] matrixPre2 = new double[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
matrixPre2[i][j] = (double) matrixPre[i][j];
}
RealMatrix PreMatrix = new Array2DRowRealMatrix(matrixPre2);
// remember to add space key when doing the typing
int[][] matrixPost = new int[m][n];
enterMatrixDataPost(sc, matrixPost, m, n);
printMatrixPost(matrixPost, m, n);
// convert the 2d int to 2d double
double[][] matrixPost2 = new double[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
matrixPost2[i][j] = (double) matrixPost[i][j];
}
RealMatrix PostMatrix = new Array2DRowRealMatrix(matrixPost2);
//
RealMatrix matrixSubtract = PreMatrix.subtract(PostMatrix);
System.out.println("So the transpose matrix after subtraction is:\t" + matrixSubtract.transpose());
// input the default maxium time of the whole reaction
System.out.println("Enter the maxium time of the whole reaction:");
double Tmax = sc.nextDouble();
// input the name of all the species
System.out.println("Enter the name of all species");
String[] strs = new String[n];
for (int i = 0; i < n; i++) {
strs[i] = sc.nextLine();
}
// input the rate constant of all the reactions(the coefficient), must be a
// double
System.out.println("Enter the rate constant of each reaction:");
Double[] rate = new Double[m];
for (int r = 0; r < m; r++) {
rate[r] = sc.nextDouble();
}
//
Vector<Double> timeList = new Vector<Double>(0);
Vector<int[]> allStateList = new Vector<int[]>(0);
timeList.add(newTime);
// input the initial states of numbers of molecules
System.out.println("Enter the initial molecule numbers of all species:");
int[] state = new int[n];
for (int i = 0; i < n; i++) {
state[i] = sc.nextInt();
}
allStateList.add(state);
timeLoop:
while (newTime < Tmax) {
for (int loopIndex = 0; loopIndex < allStateList.size(); loopIndex++) {
// calculate the hazard for each reaction and the general hazard
double[] h = new double[m];
H = 0;
try {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
h[i] = rate[i] * CombinatoricsUtils.binomialCoefficientDouble(
allStateList.get(loopIndex)[j], (int) (PreMatrix.getRowVector(i).toArray()[j]));
H += h[i];
}
}
}
catch (NumberIsTooLargeException exceptionn) {
System.out.println("One of the species has been exhausted and there is no next firing");
break timeLoop;
}
System.out.println("So the general hazard is:" + H);
// select a random reaction time
Random random = new Random();
double tau = (1 * Math.log(1 / random.nextDouble())) / H;
System.out.println("So the random reaction time is" + tau);
// put the newTime
newTime = timeList.get(loopIndex) + tau;
System.out.println("So the new reaction time is:" + newTime);
timeList.add(newTime);
// select a random reaction j
Random random2 = new Random();
int index = 0;
double hi =0;
for (int i = 0; i < m; i++) {
hi +=h[i];
if (hi >= random2.nextDouble() * H) {
index = i;
break;
}
}
System.out.println("So the next simulated event is:" + index);
// Update the state
double[] vectorDoubleArray = matrixSubtract.transpose().getColumnVector(index).toArray();
int[] intArray = new int[n];
for (int i = 0; i < n; i++) {
intArray[i] = (int) vectorDoubleArray[i];
}
int[] newState = new int[n];
int newS = 0;
for (int p = 0; p < n; p++) {
newS = intArray[p] + allStateList.get(loopIndex)[p];
newState[p] = newS;
}
System.out.println("Right now the molecule number of all species are:" + Arrays.toString(newState));
allStateList.add(newState);
}
// close the scanner
sc.close();
}
XYSeriesCollection dataset = new XYSeriesCollection();
XYSeries[] species = new XYSeries[n];
for (int j =0; j<n; j++) {
species[j]=new XYSeries(""+strs[j]+Integer.toString(j));
}
for (int j =0; j<n; j++) {
for (int k=0; k< allStateList.size();k++) {
species[j].add(timeList.get(k).doubleValue(), allStateList.get(k)[j]);
}
dataset.addSeries(species[j]);
}
// plot out the graph
System.out.println("TEST");
JFreeChart chart = ChartFactory.createXYLineChart(
"SSA Modeling",
"time",
"Number of Molecules",
dataset,
PlotOrientation.VERTICAL,
true,
true,
false
);
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
chartPanel.setBackground(Color.white);
XYPlot plot = chart.getXYPlot();
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
for (int j =0; j<n;j++){
renderer.setSeriesPaint(j, Color.RED);
renderer.setSeriesStroke(j, new BasicStroke(2.0f));
}
plot.setRenderer(renderer);
plot.setBackgroundPaint(Color.white);
plot.setRangeGridlinesVisible(false);
plot.setDomainGridlinesVisible(false);
chart.getLegend().setFrame(BlockBorder.NONE);
chart.setTitle(new TextTitle("Stochastic Simulation Algorithm",
new Font("Serif", Font.BOLD, 18)
)
);
SwingUtilities.invokeLater(() -> {
StochasticSimulationProcess ex = new StochasticSimulationProcess("SSA");
ex.setContentPane(chartPanel);
ex.pack();
ex.setLocationRelativeTo(null);
ex.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ex.setVisible(true);
});
}
public static void enterMatrixDataPre(Scanner sc, int[][] matrixPre, int m, int n) {
System.out.println("Enter Matrix pre data");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
matrixPre[i][j] = sc.nextInt();
}
}
}
public static void printMatrixPre(int[][] matrixPre, int m, int n) {
System.out.println("Matrix pre is:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(matrixPre[i][j] + "\t");
}
System.out.println();
}
}
public static void enterMatrixDataPost(Scanner sc, int[][] matrixPost, int m, int n) {
System.out.println("Enter Matrix post data");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
matrixPost[i][j] = sc.nextInt();
}
}
}
public static void printMatrixPost(int[][] matrixPost, int m, int n) {
System.out.println("Matrix post is:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(matrixPost[i][j] + "\t");
}
System.out.println();
}
}
}
to change colours randomly, as the number of XYSeries that represent species are undefined, so I choose the random set hgb method as they can be combined randomly as long as the value is between 0-255, so I change the line of code as :
renderer.setSeriesPaint(j, Color.getHSBColor(0 + (float)(Math.random() * ((255 - 0) + 1)), 0 + (float)(Math.random() * ((255 - 0) + 1)), 0 + (float)(Math.random() * ((255 - 0) + 1))));
This question already has answers here:
Java algorithm to make a straight pyramid [closed]
(4 answers)
How to print this pyramid pattern?
(5 answers)
Logic behind printing pyramid shape using nested for loops in Java
(1 answer)
What is a debugger and how can it help me diagnose problems?
(2 answers)
Closed 4 years ago.
I tried to print the below pattern in java
***1***
**2*2**
*3*3*3*
4*4*4*4
like this way
public class Sample {
public static void main(String[] args) {
int m, n;
Scanner s = new Scanner(System.in);
System.out.println("Enter the no of rows");
m = s.nextInt();
System.out.println("Enter the no of columns");
n = s.nextInt();
s.close();
//Printing the number of rows
for (int i = 1; i <= m; i++) {
//printing number of columns
for (int j = n; j > 0; j--) {
//Printing values in the pattern
for (int k = 0; k < j; k++) {
if (k == j / 2)
System.out.print(i);
}
System.out.print("*");
}
System.out.print("\n");
}
}
}
I am facing problem at the logic of finding the positions to print the values in each row. Its asked at my previous interview.
Try to figure out a formula when the asterisk(*) is replaced with number.
Hint1: the formula depends on the distance of the given symbol from the middle of the row
Hint2: the formula depends on the remainder modulo 2 of the position in the current row(and the number of the row too)
The formula is simple enough if you note the two dependencies I mention above.
we can write logic based on the output. Right now you are passing the value row = 4 and column =7. For that you have provided output. Based on that I have written this program, which output matching with that, we can modify/tune this program also:
import java.util.Scanner;
public class Sample {
public static void main(String[] args) {
int m, n;
Scanner s = new Scanner(System.in);
System.out.println("Enter the no of rows");
m = s.nextInt();
System.out.println("Enter the no of columns");
n = s.nextInt();
s.close();
String[][] str = new String[m][n];
int frontPos = 0;
int backPos = n-1;
for (int i=0; i<m; i++){
int l = Math.round((n)/(i+2));
if(i==(m-1)){
frontPos = 0;
backPos = n-1;
} else {
frontPos = l;
backPos = n-1-l;
}
//System.out.println("Difference =="+frontPos);
boolean contFlag = false;
do{
//System.out.println("frontPos=="+frontPos+"|backPos=="+backPos);
if(frontPos == backPos){
str[i][frontPos] = new Integer(i+1).toString();
} else if(frontPos < backPos){
str[i][frontPos] = new Integer(i+1).toString();
str[i][backPos] = new Integer(i+1).toString();
}
if((backPos-frontPos) > l){
contFlag = true;
frontPos = frontPos + (l+1);
backPos = backPos -(l+1);
} else {
contFlag = false;
}
} while(contFlag);
//System.out.print("\n");
}
for(int a=0; a<m; a++){
for(int b=0; b<n; b++){
if(str[a][b]==null){
System.out.print("*");
} else {
System.out.print(str[a][b]);
}
}
System.out.print("\n");
}
}
}
I have this code and I do not know why the selection sort is not sorting all the way Does anyone know where to fix the program. The selection sort code I believe is right i just dont know what is wrong. The code is functioning
import java.util.Scanner;
public class selectionSort
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int temp;
int i,j,first;
System.out.println("How many numbers do you want to enter?");
int ammount = scanner.nextInt();
int[]array = new int[ammount];
for (i = 0 ; i < array.length; i++ )
{
System.out.println("Enter the numbers now.");
array[i] = scanner.nextInt();
}
System.out.println("\nThe array is:");
for(i = 0; i < array.length; i++){
System.out.print(array[i] + " ");
}
for (i=array.length - 1; i>0;i--)
{
first=0;
for(j=1;j<=1;j++)
{
if(array[j]<array[first])
first = j;
}
temp = array[first];
array[first] = array[i];
array[i]=temp;
}
System.out.println("\nThe sorted array is:");
for( i = 0; i < array.length; i++){
System.out.print(array[i] + " ");
}
}
}
You appear to have a typo. This line:
for(j=1;j<=1;j++)
should probably be:
for(j=1;j<=i;j++)
(The loop termination test should be j<=i, not j<=1.)