|
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the array");
int n = sc.nextInt();
int arr[]=new int[n];
System.out.println("Enter the elements of the array");
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
DivArray(arr,0,arr.length-1);
for(int z:arr)
{
System.out.print(z+" ");
}
}
static void DivArray(int arr[],int low,int high)
{
if(low<high)
{
int mid=(low+high)/2;
DivArray(arr,low,mid);
DivArray(arr,mid+1,high);
MergeSort(arr,low,mid,high);
}
}
static void MergeSort(int arr[],int low,int mid,int high)
{
int i = low;
int k = low;
int j = mid+1;
int dummy[]=new int[arr.length];
while(i<=mid && j<=high)
{
if(arr[i]<arr[j])
{
dummy[k]=arr[i];
k++;
i++;
}else{
dummy[k]=arr[j];
k++;
j++;
}
}
while(i<=mid)
{
dummy[k]=arr[i];
k++;
i++;
}
while(j<=high)
{
dummy[k]=arr[j];
k++;
j++;
}
for(int q=0;q<arr.length;q++)
{
arr[q]=dummy[q];
}
}
}
OUTPUT:
Enter the size of the array
8
Enter the elements of the array
9 8 7 6 5 4 3 2
0 0 0 0 0 0 0 0
modified 3-Dec-21 4:28am.
|
|
|
|
|
Move the code the displays the sorted array out of the MergeSort method, and into your Main method, after the call to DivArray .
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I am trying to extract date from a String (Line), to do a Date conversion, but my String concatenation (#1) gives incorrect result.
Adding a blank ("") at the start (#2) gives the correct result.
Why is this? Where does the "105" come from?
System.out.println("Line= " + Line);
char ch1 = Line.charAt(0), ch2 = Line.charAt(1);
char ch4 = Line.charAt(3), ch5 = Line.charAt(4);
char ch7 = Line.charAt(6), ch8 = Line.charAt(7),
ch9 = Line.charAt(8), ch10 = Line.charAt(9) ;
System.out.println("Text date= " + ch1 + ch2 + '/' + ch4 + ch5 + '/' +
ch7+ ch8+ch9+ch10);
String sDate1= ch1 + ch2 + "/" + ch4 + ch5 + '/' +
ch7+ ch8+ch9+ch10;
System.out.println("Not correct: sDate1= " + sDate1);
String sDate2= "" + ch1 + ch2 + "/" + ch4 + ch5 + '/' +
ch7+ ch8+ch9+ch10;
System.out.println("Correct: sDate2= " + sDate2);
Output:
Line= 09/12/2021
Text date= 09/12/2021
Not correct: sDate1= 105/12/2021
Correct: sDate2= 09/12/2021
|
|
|
|
|
It "thinks" you're doing "addition", then string concatenation in the first instance (0x30 + 0x39 == 105 == char(0) + char(9)); and all string concats in the second; due to the leading quote.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
Er, what exactly are you trying to do?
|
|
|
|
|
Im trying to extract the date from the string, then do a date conversion, but Im not able to extract the date correctly.
UPDATE:
Ok, I resolved this myself using the java substring function instead of trying to extract one character at a time:
String sTest = "09/21/2021 and other junk on the line";
System.out.println("sTest= " + sTest);
String sDate = sTest.substring(0, 10);
System.out.println("sDate= " + sDate);
Date date1=new SimpleDateFormat("MM/dd/yyyy").parse(sDate);
System.out.println(sDate +"\t"+date1);
now gives the expected result:
sTest= 09/21/2021 and other junk on the line
sDate= 09/21/2021
09/21/2021 Tue Sep 21 00:00:00 EDT 2021
modified 28-Nov-21 19:11pm.
|
|
|
|
|
Can someone help with this task?
Reuse the included class CA in order to provide graphical interface: instead of printing X's and blanks on the screen, black and white cells are drawn on the canvas depending on the CA's state. Your SwingCA class should accept a single argument representing a Wolfram's rule (handle exceptions!) and generate 600 generations each of size 400. For example, if you run java SwingCA 30 the image you get should resemble:
https://media.cheggcdn.com/media/59e/59e3b9d6-7ede-413d-a953-78cbb74e925b/phpYbnEbr[^]
REUSE THIS CLASS:
-----------------------------------------
public class CA {
// Data Members
private boolean[] cell;
private int size;
private int rule;
private boolean[] ttable;
// Constructor
public CA(int size, int rule) {
this.rule = rule;
this.size = size;
// Initialize Truth Table
ttable = new boolean[8];
for (int i = 0; i < ttable.length; i++) {
ttable[i] = (rule & 0x1) == 1? true: false;
rule >>= 1;
}
// Create Cells
cell = new boolean[size];
// Set Middle Cell To 1
cell[size / 2] = true;
}
// Compute And Return Next State
public boolean[] getState() {
// Array To Hold New State
boolean[] newCell = new boolean[size];
for (int i = 1; i < size - 1; i++) {
// Get Left Current And Right Values
int left = cell[i - 1]? 1 : 0;
int cur = cell[i] ? 1 : 0;
int right = cell[i + 1]? 1 : 0;
// Convert It To Decimal And Use It As Index For Rule Array
int ruleIdx = 4 * left + 2 * cur + 1 * right;
// Set New Cell Value To Rule TT Value
newCell[i] = ttable[ruleIdx];
}
// Update Cell Array
cell = newCell;
// Return New State
return cell;
}
}
|
|
|
|
|
Suave65 wrote: Can someone help with this task? Help in what way?
|
|
|
|
|
Can you send a BufferedImage thought a socket by changing it somehow because it is not Serializable , and then on client side using WritableImage to show it inside a ImageView ? I am not trying to send an image file but a screenshot made using the following code which I can in the end use it inside ImageView :
Robot robot = new Robot();
BufferedImage imageB = robot.createScreenCapture(screenRectangle);
WritableImage imageW = SwingFXUtils.toFXImage(imageB, null);
ImageView.setImage(imageW);
|
|
|
|
|
Valentinor wrote: Can you send a BufferedImage thought a socket
No.
That class has a specific purpose and what you are doing has nothing to do with that.
The class is used to create (and manipulate) an image. The image, not the class is what you will be sending.
And as a suggestion if you have never used sockets before then I suggest that you experiment with something besides an image for the first time. Like a simple string.
|
|
|
|
|
jschell wrote: And as a suggestion if you have never used sockets before then I suggest that you experiment with something besides an image for the first time. Like a simple string.
What is with that suggestion and the relation with what I asked?
jschell wrote: That class has a specific purpose and what you are doing has nothing to do with that.
I did said the following, I wasn't trying to send it straight, because I knew you can't... by changing it somehow because it is not Serializable
Your comment wasn't useful in the least, it was actually the opposite, it didn't contained anything useful, only a comment to try and look smart and make the other person feel stupid. Nowhere did I said I didn't worked with sockets before, yet you added that last part... You should have left it at "No." and it would have been better, and yet still wrong because you can do something there and change it into bytes and you can still send it, not that is fast, but you can do it. And why didn't you quoted the whole think I said and stopped at that? It's like those manipulations where you get only that you need, not the whole truth.
Yeah this is a harsh reply but in the future a more constructive answer is better if not none at all is even better, we want to learn from one another not to just make ourself look smart and not truly help other, or help them with a riddle that it is going to take them even farther from the solution.
|
|
|
|
|
Valentinor wrote: Your comment wasn't useful in the least, it was actually the opposite,
You specifically said the following
"Can you send a BufferedImage thought a socket by changing it somehow because it is not Serializable,"
Questions like this have been asked for decades and answered by me when the person doesn't actually understand what a socket is doing. Note that I am not saying that someone hasn't used a socket but rather than they do not know what it is doing.
If you had said "how do I send an image" then that would have been different. But you specifically mentioned that class and that it is Serializable. So I answered that specific question.
Valentinor wrote: and you can still send it, not that is fast,
I didn't say anything at all about performance.
Valentinor wrote: Yeah this is a harsh reply but in the future a more constructive answer is better
You asked a specific question by making a specific statement. I quoted exactly that and addressed specifically that. Perhaps you can ask a more general question in the future. I will note that the following in google seems to address your general question however.
example java send image by socket
Valentinor wrote: or help them with a riddle that it is going to take them even farther from the solution.
The forum is intended to provide answers to people that ask technical questions. It is not a forum on teaching people how to ask questions on forums.
|
|
|
|
|
Both BufferedImage and WriteableImage utilize (internal) pixel arrays. You "unload" the BufferedImage to a pixel array, transmit it as a stream, then load it into the WriteableImage.
There are no explicit "serialize / deserialize" methods; you do it yourself.
The "interface" is pixel arrays (in one form or another); that's what you have to remember.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
BufferedImage already "unpacked" an image for display/usage usage: the raster of pixels, the color model and such. The only simple serialization is to write the image to some buffer/file, and send those (generally much smaller and faster) bytes.
To stress the point: serializing for every image type, indexed color or not, packed RGB and so on, is some effort, requiring some unit tests checking internals of BufferedImage.
|
|
|
|
|
For given numbers N and K, write a function to find 'significant number'. Let's look at an example to understand the steps to find 'significant number'.
Given N = 46333 and K = 3,
1. Check if N is 'reversible' or not. ['Reversible' denotes a number sequence that reads the same backward as forward, e.g., 12321]
If Yes, return 1.
If No, go to step 2.
2. Convert digits of N to elements of array A.
e.g. For N = 46333
Convert to array A = [4, 6, 3, 3, 3]
3. Replace each element of array A with its 'modified value'.
Modified value of A[ i ] = A[ i ] * 5;
Modified value of 4 is = 4*5 = 20
Modified value for 6 is = 6*5 = 30 and so on.
After replacing elements of array A with their 'modified values', array A will be [20, 30, 15, 15, 15]
4. For each element in the array generated in previous step, find the 'child primes' that are less than the array element. Then, generate the sum of these child primes.
A child prime is a number in the form (2 * p) + 1 where p is prime.
[Prime numbers are 2, 3, 5, 7, 11 ...]
For A[0] = 20,
for prime number 2, (2 * 2) + 1 = 5 < 20
for prime number 3, (2 * 3) + 1 = 7 < 20
for prime number 5, (2 * 5) + 1 = 11 < 20
for prime number 7, (2 * 7) + 1 = 15 < 20
for prime number 11, (2 * 11) + 1 = 23 > 20. Hence, will not consider 23.
So, child primes less than 20 are [5 7 11 15]. Sum of child primes for array element A[0] is = 38
Similarly for A[1] = 30, child primes that are less than 30 are [5 7 11 15 23 27]
Sum of child prime for array element A[1] is = 88 and so on.
Array is now = [38 88 23 23 23]
5. Count pairs of the array generated in previous step whose sum is divisible by value K=3.
Here pairs which are divisible by 3 are (38,88), (88,23), (88,23) and (88,23).
Hence, return 4. This is the 'significant number' to be returned.
Input
46333
3
where,
First line represents value of N
Second line represents value of K
Expected Output
4
|
|
|
|
|
|
In most of scripting/interpreting programming languages such as python or JavaScript, there is/are a/some method(s) to execute a string in runtime just like a code.
For example: eval("int i=5;");
those could have run the eval method inside the interpreting shell and even inside the input parameter of eval method recursively without a problem.
In jshell I found, inside the java host program after creating a jshell object we can call the eval, but what if I want to insert the eval inside the string and then run it in the jshell object.
For example:
JShell jsh=JShell.create();
jsh.eval("int j=4;");//current way of using eval in jshell hosted by another java or jshell program.
I need to do something like this:
JShell jsh=JShell.create();
jsh.eval("""
eval("int j=4;");
""");
or alternatively
JShell jsh=JShell.create();
jsh.eval("""
currentJShell.eval("int j=4;");
""");
or again alternatively
JShell jsh=JShell.create();
jsh.eval("""
currentJShell.eval("int j=4;");
""");
after some research, I found something like "/open" doesn't solve the problem for multiple reasons:
1- it's a part of interactive jshell utility/UI (provided (probably) by oracle(not sure))
not exactly jshell object
2- even if it is a part of jshell, still inside the guest jshell program, I can't call it
in a for loop or ... I have to type it with the keyboard and then hit enter.
for(int c=0;c<34;c++){/open <filename+c>}// it's not possible.
But in beanshell looks like ok just like python and JavaScript.
I have the same problem with csharp script.
Thanks for your time.
Sorry if this question is asked in the wrong spot. I am not very familiar with these types of q/a website.
|
|
|
|
|
Majid Karimi wrote: Sorry if this question is asked in the wrong spot
Your question is really about how the jshell works and not really about java.
Majid Karimi wrote: JShell jsh=JShell.create();
Did you try creating a new shell in the eval. Pseudo code like the following
JShell jsh1=JShell.create();
jsh.eval("""
JShell jsh2=JShell.create();
jsh2.eval("int j=4;");
""");
Also note that presumably you are doing this to experiment. You would not want to create application code like this. That is due to complexity and maintenance costs.
|
|
|
|
|
I've been dealing with a problem in my application that has me forced to change the HibernateQuery in a way that casts the existing DateTime column I have within my database into a date for further processing.
I've looked around a little on here already and found a supposed solution using sqlGroupProjection which ended up looking like this for me.
Projection dp = Projections.sqlGroupProjection("CAST("+sp.getIname()+" AS DATE",
"CAST("+sp.getIname()+" AS DATE",
new String[] {sp.getIname()},
new Type[] {StandardBasicTypes.DATE});
This does work to some the extent that it now generates this in the SQL statement
CAST(ierstelltAm AS DATE)
Which would be correct if it wasn't for the fact that something is now missing. Usually when Hibernate generates these statements they're given unique identifiers, one of them being y[i]_ and the other somehow relating to the table/entity used in the syntax of entity[i]_.
Since these are all inbuilt hibernate functions I had expected the above method to generate something along these lines
CAST(auftrag8_.IErstelltAm AS DATE) as y1_
based on the fact that the code previously generated using the criteria system did look like this.
auftrag8_.IErstelltAm as y1_
The question is now how do I correctly cast something to another data type using hibernate criterie, if it's not the sqlGroupProjection method mentioned in other places -- or is this a syntactical/logical error on my part?
|
|
|
|
|
How to make a scrolling background for a JPanel in java? I tried searching google but couldn't find a helpful one.
|
|
|
|
|
Do you mean manual scrolling, or something that scrolls by itself? And how would that affect any other controls that are in the panel?
|
|
|
|
|
I have a jpanel with an image as a background . how can i make that scroll like in a game ?
|
|
|
|
|
|
sir i don't want scroll panes . i want my jpanel background image to scroll horizontaly automatticaly.i am making a game.
|
|
|
|
|
|