I am trying to write code.It takes 3 input value 10 4 3.As you can see kdx value is 0.when it call function hello, value of qdx is changing to 11, But it should be 0.And return value is also 11.Do you have any idea why it is happening?
import java.util.Scanner;
import java.io.FileNotFoundException;
public class test1 {
private static final int FORWARD = 1;
private static final int BACKWARDS = -1;
private static Scanner scn = new Scanner(System.in);
public static void main(String args[]) throws FileNotFoundException {
int N = scn.nextInt();
int k = scn.nextInt();
int m = scn.nextInt();
while (N != 0) {
boolean[] offQueue = new boolean[N];
int offCount = 0;
int kdx = 0;
int mdx = N - 1;
kdx = hello(k, offQueue, kdx, FORWARD);
System.out.println(kdx);
}
}
private static int hello(int q, boolean[] offQueue, int qdx, int direction) {
return qdx;//Problem is here
}
}
I cannot reproduce your problem. Are you sure that you are using your last version when you run it? You can as well try to debug your application with Eclipse.
Related
I'm trying to solve BaekJoon 12865 problem by Java.
But it evaluates Execution timeout.
There isn't any error in eclipse compiler.
Someone know about problem in my codes.
Please tell me what did i do wrong.
This problem is find Maximum value in K weight by N objects that has V(value) and W(weight).
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
class Object implements Comparable<Object>{
private int W; //object's weight
private int V; //object's value
private int VbyW;
public Object(int w, int v) {
this.W = w;
this.V = v;
this.VbyW = v/w; // value by weight
}
#Override
public int compareTo(Object object)
{
if (object.VbyW > VbyW)
{
return 1;
}else if(object.VbyW < VbyW){
return -1;
}else
return 0;
}
}
int N; // number of object
int K; // Maximum of weight that I can have
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
K = sc.nextInt();
//first I got number of objects and Maximum weight
int W[] = new int[N];
int V[] = new int[N];
Object[] obj = new Object[N];
ArrayList<Object> objs = new ArrayList<>();
for (int i=0;i<N;i++)
{
W[i] = sc.nextInt();
V[i] = sc.nextInt();
obj[i] = new Object(W[i],V[i]);
objs.add(obj[i]);
} // I'll get all object's value and weight
Collections.sort(objs, Collections.reverseOrder());
int totalV=0;
for(int j=0;j<N;j++)
{
while(K>objs.get(j).W)
{
totalV +=objs.get(j).V;
}
} // I'll find Maximum value in K weight by objects
System.out.println(totalV);
sc.close();
}
}
problem site : https://www.acmicpc.net/problem/12865
I have a task to write a program that takes in a profit score and multiplies it by 2, takes hard work score and multiplies it by 5, adds the two together, divides by 7 and then multiplies by 5000 to give a "bonus".
This would be very simple but the specification states i must use at least 9 methods not including main, use functions and getter/setter methods. The rest i can do but i don't know how to use ADT in a program like this.
This is what i've got so far...it does what its supposed to but its missing ADT.
import java.util.Scanner;
class bonus {
public static void main(String[] args) {
int i1 = inputProfit();
int w1 = inputWork();
int p1 = performanceScore(i1, w1);
int f1 = finalbonus(p1);
output(f1);
System.exit(0);
}//end main method
public static int inputProfit() {
Scanner scanner = new Scanner(System.in);
System.out.println("Profit score?");
int profitScore = scanner.nextInt();
return profitScore;
}
public static int inputWork() {
Scanner scanner1 = new Scanner(System.in);
System.out.println("Hard work score?");
int workScore = scanner1.nextInt();
return workScore;
}
public static int profit(int profitScore) {
profitScore = profitScore*2;
return profitScore;
}
public static int work(int workScore) {
workScore = workScore*5;
return workScore;
}
public static int performanceScore(int profitScore, int workScore) {
int p = profit(profitScore);
int w = work(workScore);
int performance = ((p + w)/7);
return performance;
}
public static int finalbonus(int performance) {
int payOut = performance * 5000 ;
return payOut;
}
public static void output(int payOut) {
System.out.println("Your bonus is " + payOut + " pounds");
}
}//end class bonus
ADDn - The first n (0 ≤ n ≤ 4) bits replicate and are concatenated to the first n bits. The last n bits are deleted
e.g. ADD3 ABCDEFGH becomes ABCABCDE
METHODS -
public class Methods
{
public String ADD(String x, int y)
{
if(y > 0)
{
String output = x.substring(0,y);
String output2 = x.substring(y, x.length() - y);
return output + output + output2;
}else {
return x;
}
}
RUNNER -
import java.io.*;
import java.util.*;
public class Runner
{
public static void main(String []args)throws FileNotFoundException
{
Scanner in = new Scanner(new File("data.txt"));
Methods md = new Methods();
String cell = in.nextLine();
String methods = in.nextLine();
for (int x = 0; x < 5; x++)
{
if(methods.equals(("ADD2"))
{
int i = Integer.parseInt(methods);
System.out.println( );
}
DATAFILE-
ADD2 ABBCDFGG
I need it to print ABABBCDF
class Problem {
public static void main(String[] args) {
for (int num = 1000; num <= 2000; num++) {
if (String.valueof(num).contains("3")) {
System.out.println(num);
}
}
}
}
I'm having a problem with my code. We're working on binary search, and I can't seem to get the right output whenever I input a number. We were given a list of 60 numbers already in order(external file), and whatever number we input, the program should search and return the position. If the number is not on the list, it should return a -1.
My code:
import java.io.*;
import java.util.*;
public class Prog489
{
public static void main(String[] args) throws IOException
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter a number to search for: ");
int search = scan.nextInt();
Scanner kbReader = new Scanner(new File("C:\\Users\\Guest\\Documents\\java programs\\Prog489\\Prog489.in"));
int[] num = new int[60];
int i = 0;
System.out.println(binarySearch(num, search));
while(kbReader.hasNextInt())
{
num[i++] = kbReader.nextInt();
}
}
private static int binarySearch(int[] num, int search)
{
int lb = 0;
int ub = num.length - 1;
while(lb<=ub)
{
int mid = (lb+ub)/2;
if(num[mid] == search)
{
return mid;
}
else if(search>num[mid])
{
lb=mid+1;
}
else
{
ub = mid-1;
}
}
return -1;
}
}
So the part with the return should only return -1 if the number is not on the list. But whenever I do enter a number on the list (such as 60), it still returns a -1. Everything compiles, so I'm not really sure what I'm missing, or if it's something really obvious that I'm forgetting. Could someone please help me identify the error? Any guidance/feedback is greatly appreciated.
Move the call to print the output of binarySearch to after you populate the array:
int[] num = new int[60];
int i = 0;
while(kbReader.hasNextInt())
{
num[i++] = kbReader.nextInt();
}
System.out.println(binarySearch(num, search));
This is my program that makes some calculations to a numbers into the array named "initialMarks". However I would like to fill the initialMarks array from another class using scanner.Could you help me to figure it out how to do that? Is it possible to outprint the result array "result" in a third class?
public class testingN
{
public static void main(String[] args)
{
int [] initialMarks = new int [4];
int [] result = new int [6];
result[0] = computedMarks(initialMarks[0], initialMarks[1])[0];
result[1] = computedMarks(initialMarks[2], initialMarks[3])[1];
for(int i=0; i< result.length; i++)
System.out.println(result[i]);
}
public static int [] computedMarks(int mark1, int mark2)
{
int [] i= new int [6];
for (int j = 0; j < i.length; j++)
{
if ((mark1 < 35 && mark2 > 35) || (mark1 > 35 && mark2 < 35))
{
i[j] = 35;
}
else
{
i[j] = (mark1 * mark2);
}
}
return i;
}
}
Your other class can have a method that returns a stream, and you can feed that into your Scanner's constructor.
In your other class's String getInitialMarks() method:
// Generate a string with all the "marks" as "stringWithMarks", separated by "\n" characters
InputStream is = new ByteArrayInputStream( stringWithMarks.getBytes( "UTF-8" ) );
return is;
Then in your first class, otherClass:
Scanner scanner = new Scanner(otherClass.getInitialMarks());
And proceed to read in the marks as if it were user input.
public class testingN
{
static int [] result = new int [6];
static int [] initialMarks = new int [4];
public static void main(String[] args)
{
Declaring result as class member (global variable) and being static should help your cause
Example: using from another class
public class AnotherClass {
public static void main(String[] args) {
// example
testingN.result[0] = 4;
System.out.println(testingN.result[0]);
}
}
Edit: Run the code below Here. You'll see it works just fine.
class testingN
{
static int [] result = new int [6];
static int [] initialMarks = new int [4];
}
public class AnotherClass {
public static void main(String[] args) {
// example
testingN.result[0] = 4;
System.out.println(testingN.result[0]);
}
}