class Compound {
public static void main(String args[]) {
int P = Integer.parseInt(args[0]);
int R = Integer.parseInt(args[1]);
int T = Integer.parseInt(args[2]);
int Ci, i, A = 1, Pa = P;
for (i = 1; i <= T; i++) {
Ci = P * R / 100;
P = P + Ci;
A = P + Ci;
}
Ci = A - Pa;
System.out.println(Ci + " is the Ci\nAmount=" + A);
return 1;
}
}
It throws the following exception:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Compound.main(JavaApplication1.java:3)
C:\Users\Dell\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 2 seconds)
this is what happened Please tell the reason for the error and a suitable code to avoid it. Above was a simple program for Compound Interest
Your program is crashing because you're calling it without 3 arguments and just assuming they are there.
You need to ensure that you actually have all three arguments before processing them. Put the code you have in main now in a function and call it when you have 3 args.
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.println("Enter P");
int P = in.nextInt();
System.out.println("Enter R");
int R = in.nextInt();
System.out.println("Enter T");
int T = in.nextInt();
getCi(P, R, T);
}
public static void getCi(int P, int R, int T) {
int Ci, i, A=1, Pa=P;
for(i=1;i<=T;i++){
Ci=P*R/100;
P=P+Ci;
A=P+Ci;
}
Ci=A-Pa;
System.out.println(Ci+" is the Ci\nAmount="+A);
}
Related
I am currently coding using JAVA language, an array and a queue. I am having a problem with using an array that is a "Process" type. I am reading an input file which have the following:
3 1 5 30 3 1 5 30 4 0 5 30 3
Here is the stacktrace. When you see my console, you can see that each element (=Process type process object) takes in all of its properties (a, b, c, d) properly, when I am adding each of them into allProcesses array.
However, when I print out allProcesses array USING the printProcesses() method, the "a" property of each Process objects are all 0 --> allProcess[p].a = 0 whereas it should be 1.
Upon the requests, here are FCFS(), and Process class.
//Sorts "allProcesses" in an ascending order of "a".
public static void FCFS (Process[] allProcesses, int numProcesses) {
ArrayList<Process> FCFSsortedAllProcesses = new ArrayList<Process>();
// Iterating through the created list from the position
for (int p = 0; p < allProcesses.length; p++) {
for (int j = p + 1; j < allProcesses.length; j++) {
if (allProcesses[p].a > allProcesses[j].a) {
Process temp = allProcesses[p];
allProcesses[p] = allProcesses[j];
allProcesses[j] = temp;
}
}
}
}
public class Process {
/* Linked list Node*/
int id;
static int a;
int b;
int c;
int io;
int readyCycle;
int CPUburstRemaining;
int CPUcycle;
int IOburstRemaining;
int IOcycle;
String state;
String relationship;
int priority;
//int runningTime;
int finishingTime;
int turnaroundTime;
int ioTime;
int waitingTime;
Process next;
//Node prev;
// Constructor to create a new node
// Next is by default initialized as null
public Process(int a, int b, int c, int io){
this.id = id;
this.a = a;
this.b = b;
this.c = c;
this.io = io;
this.readyCycle = readyCycle;
this.CPUburstRemaining = 0;
this.CPUcycle = 0;
this.IOburstRemaining = 0;
this.IOcycle = 0;
this.state = "unstarted";
this.relationship = null;
this.priority = 0;
this.finishingTime = 0;
this.turnaroundTime = 0;
this.ioTime = 0;
this.waitingTime = 0;
}
}
I tried to find the error using my debugger, but all I figured out is that the Process object doesn't take its "a" property value that was originally given. Here is the debugger window that shows the properties of the first "curElement" in printProcesses(). The "a" property is just missing. The same goes for the other two curElements as well.
Please advise me on how to fix this issue, and let me know if there is any other information I should provide to make it easier for you. Thanks in advance.
public static void main(String args[]) {
try {
String fileAddress = args[0];
File fileInput = new File(fileAddress); //Read
Scanner scan = new Scanner(fileInput);
int numProcesses = scan.nextInt();
Queue<Process> processes = new LinkedList<Process>();
Process[] allProcesses = new Process[numProcesses];
//Adding each process to processes queue
for (int m = 0; m < numProcesses; m++) {
int a = scan.nextInt();
int b = scan.nextInt();
int c = scan.nextInt();
int io = scan.nextInt();
Process thisProcess = new Process(a, b, c, io);
thisProcess.id = m;
processes.add(thisProcess);
allProcesses[m] = thisProcess;
System.out.println(m + " thisProcess.a = " + thisProcess.a);
System.out.println(m + " allProcesses[m].a = " + allProcesses[m].a);
}
System.out.printf("\noriginal\n");
printProcesses(allProcesses, numProcesses); //original
FCFS(allProcesses, numProcesses);
System.out.println();
System.out.printf("sorted\n");
printProcesses(allProcesses, numProcesses); //sorted
}
catch (Exception e){
e.printStackTrace();
System.out.printf(" Error: File not foundd. \n");
}
}
public static void printProcesses (Process[] allProcesses, int numProcesses) {
System.out.printf("The original input was: ");
for (int p = 0; p < allProcesses.length; p++) {
Process curElement = allProcesses[p];
System.out.printf("%d %d %d %d ", curElement.a, curElement.b, curElement.c, curElement.io);
}
System.out.print("\n\n");
}
You have static int a;. So it will have the last update which in your case is zero. And the reason you are not getting a in debugger is most likely your settings. There should be show static variables in your debug menu for eclipse.
Java Newbie Here,
So i am attempting to write a program that can set the number of hello worlds, and the number of exclamation points that follow it, entering these value using the command line arguments. i have in a manner done it but the output format is wrong.
Desired result
"Hello World !!!!
"Hello World !!!!"
Attempt 1
"Hello World !
!
!
!" (this continues down)
what I am Getting, attempt 2
"Hello World !!!!Hello World!!!!Hello World!!!!"
my code for Attempt 1
public class NHelloWorldWE {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
//
int n = Integer.parseInt(args[0]);
int e = Integer.parseInt(args[1]);
for (int a = 1; a <= n; a = a + 1) {
System.out.print("Hello World");
for (int b = 1; b <= e; b = b + 1) {
System.out.print("!");
System.out.println(" ");
}
}
}
}
My Code for Attempt 2
public class NHelloWorldWE {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
//
int n = Integer.parseInt(args[0]);
int e = Integer.parseInt(args[1]);
for (int a = 1; a <= n; a = a + 1) {
System.out.print("Hello World");
for (int b = 1; b <= e; b = b + 1) {
System.out.print("!");
}
}
}
}
You need to use a new line print:
System.out.println("Hello World");
Like this:
public static void main(String[] args) {
//
int n = Integer.parseInt(args[0]);
int e = Integer.parseInt(args[1]);
for (int a = 1; a <= n; a = a + 1) {
System.out.print("Hello World");
for (int b = 1; b <= e; b = b + 1) {
System.out.print("!");
}
System.out.println();
}
}
This one will give you a similar result, but with 1 less exclamation mark in each iteration (I believe this is what you are trying to do)
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
int e = Integer.parseInt(args[1]);
for (int a = 1; a <= n; a++) {
System.out.print("Hello World");
for (int b = 1; b <= e; b++) {
System.out.print("!");
}
e--;
System.out.println();
}
}
Here is a version of your solution:
public class NHelloWorldWE {
public static void main(String... args) {
int n = Integer.parseInt(args[0]);
int e = Integer.parseInt(args[1]);
for (int a = 0; a < n; a++) {
System.out.print("Hello World");
for (int b = 0; b < e; b++) {
System.out.print("!");
}
System.out.println();
}
}
}
And here is one using streams:
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class NHelloWorldWE {
public static void main(String... args) {
int n = Integer.parseInt(args[0]);
int e = Integer.parseInt(args[1]);
final String hello = "Hello World" + Stream.generate(() -> "!")
.limit(e)
.collect(Collectors.joining());
Stream.generate(() -> hello)
.limit(n)
.forEach(System.out::println);
}
}
I want the output from the code that 5=0101 if 1 occur double and add and if 0 occur only double from the algo
Step 1: randomly select an integer s
Step 2: compute the bit length Ls of s
Step 3: compute L=n/Ls, Lr = n%Ls, Sr= s>> (Ls-Lr)
Step 4: if (Lr=0) then compute M=sP Else compute M=sP and Mr=SrP
Step 5: Q= Mr
Step 6: for i=0 to L
6.1 Q=2LsQ
6.2 Q=Q+M
Step 7: return Q
but this program doesn't return any output.
whats wrong with the code, please rectify the error.
Thanks in advance
package main;
import java.awt.Point;
import java.util.Random;
import java.util.Scanner;
public class a {
private static final double M = 0;
static Object Q;
public static double a() {
//public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println("enter the no.:");
int k = reader.nextInt();
String str = java.lang.integer.toBinaryString(k);
int n = str.length();
Point P = new Point();
Random r = new Random();
int s = r.nextInt(50) + 1;
int Ls = Integer.toBinaryString(s).length();
int L = n/Ls;
int Lr = n%Ls;
int Sr = s>>(Ls-Lr);
int Mr = 0;
double Q = Mr;
for (int i = 0; i<L; i++) {
Q = (Math.pow(2, Ls)*Q);
//int M;
Q = Q + M;
}
return Q;
}
public void Q() {
// TODO Auto-generated method stub
}
}
Main method
package main;
import main.a;
public class newmeth {
//private static final int Q = 0;
//private static final String SrP = null;
//private static final int M = 0;
//public int Q() {
public static void main(String [] args) {
System.out.println("enter the no.:");
a myjava = new a();
myjava.Q();
Object Q = null;
a(Q);
}
public static int ya(Object Q) {
//System.out.println("enter the no.:");
return 0;
}
}
If I will simplify your code and remove all unused variables - I will get something like that. and as you can see - you are not execute a() method from main()
package main;
import java.util.Random;
import java.util.Scanner;
import static java.lang.Integer.toBinaryString;
public class newmeth {
private static final double M = 0;
public static void main(String[] args) {
System.out.println("enter the no.:");
}
private static double a() {
Scanner reader = new Scanner(System.in);
System.out.println("enter the no.:");
int k = reader.nextInt();
int n = toBinaryString(k).length();
int s = new Random().nextInt(50) + 1;
int Ls = toBinaryString(s).length();
int L = n / Ls;
double Q = 0;
for (int i = 0; i < L; i++)
Q = (Math.pow(2, Ls) * Q) + M;
return Q;
}
}
And minimal useful changes is change main method like below
public static void main(String[] args) {
System.out.print(a());
}
And also please, check this out, and start calculate M based on your step 4 from your step by step guide
double Q = 0
into loop Q = <...> * Q what always return 0
Q = Q + M Q and M equal 0 so result still 0
What would be the next step in order to ask the user which of the numbers in this series of 30 he wants to see and prompts for an integer input - a number between 1 and 30 (inclusive)?
So if the user wants to see the fifth (5th) number of the Fibonacci series the user would input the integer 5 in response to the prompt.
public class MyFibonacci {
public static void main(String a[]) {
int febCount = 30;
int[] feb = new int[febCount];
feb[0] = 0;
feb[1] = 1;
for(int i=2; i < febCount; i++) {
feb[i] = feb[i-1] + feb[i-2];
}
for(int i=0; i< febCount; i++) {
System.out.print(feb[i] + " ");
}
}
}
Use a Scanner, or some InputStreamReader to read the input from the console. Scanner would be used in this way:
Scanner scanner = new Scanner(System.in);
if(scanner.hasNextInt())
int someInt = scanner.nextInt();
Another option would be to use some reader like this:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
and parse the required data from the output of the reader. Using Scanner will be simpler in most cases though.
public class Fibonacci_series // upto less than a given number
{
public static void main(long n)
{
long a=0, b=1, c=0;
while (c<n)
{
a=b;
b=c;
System.out.print(c+", ");
c=a+b;
}
System.out.print("....");
}
}
Here you go :
public class TestProgram {
public static void main(String a[]) {
Scanner reader = new Scanner(System.in);
printFibonacciSeries();
System.out.println("");
System.out.println("Fibonacci Series, Enter the number in the series of 30 you want to see");
int num = reader.nextInt();
int febCount = 30;
int[] feb = new int[febCount];
feb[0] = 0;
feb[1] = 1;
for (int i = 2; i < febCount; i++) {
feb[i] = feb[i - 1] + feb[i - 2];
if (i == num) {
System.out.print(feb[i] + " ");
}
}
}
public static void printFibonacciSeries() {
int febCount = 31;
int[] feb = new int[febCount];
feb[0] = 0;
feb[1] = 1;
for (int i = 2; i < febCount; i++) {
feb[i] = feb[i - 1] + feb[i - 2];
System.out.print(feb[i] + " ");
}
}
}
You could use a command line argument and read it from the main method args:
public class MyFibonacci {
public static void main(String a[]) throws NumberFormatException {
int febCount = Integer.parseInt(a[0]);
then call it with
java MyFibonacci 30
Or pass it in the 'VM options' input in an IntelliJ run configuration
import java.util.*;
class Series {
public static void main(String args[]) {
int a, d, n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter first term of A.P");
a = sc.nextInt();
System.out.println("Enter difference");
d = sc.nextInt();
System.out.println("Enter the number of terms");
n = sc.nextInt();
Series s1 = new Series();
s1.lastTerm();
}
public int lastTerm(int s) {
int a, d, n;
s = a + (n - 1) * d;
return s;
}
}
The error I'm getting is:
Series.java:15: error: method lastTerm in class Series cannot be applied to given types;
s1.lastTerm();
^
required: int
found: no arguments
reason: actual and formal argument lists differ in length
How can I fix this?
In your code:
Series s1 = new Series();
s1.lastTerm();
You're calling the lastTerm() function. You declared your function to pass an int but in your s1.lastTerm() you're not passing any integer value to it. That's why the compiler is complaining.
This should be how you solve your problem:
public int lastTerm(int a, int d, int n)
{
return a+(n-1)*d;
}
and call it as follows:
s1.lastTerm(a, d, n);
Pass all required parameters to the method
public int lastTerm(int firstTerm, int difference, int termCount) {
return firstTerm + (termCount - 1) * difference;
}