|
Most platforms have the JVM installed, so it's not necessary to make an executable. You can make a standalone executable if you want, but you will loose Java's portability.
Releated stack overflow question.
Instead of Swing you should the newer GUI library, JavaFX. As far as games go, LWJGL is used for making games in Java. Minecraft was originally written in Java.
|
|
|
|
|
thanks
i've completed half of the book on java programming.... and i find this language great. And then i'm planning to learn JavaFX and then probably OpenGL with java for android developement.
|
|
|
|
|
These are the reasons that I personally came up with:
Java has a very rich API, and an incredible supporting open source ecosystem. There are tools upon tools for just about everything you would like to do.
Java is an Object Oriented language. It internally embraces best practices of object oriented design.
The IDEs available for Java will blow your mind.
Java is running just about everywhere you can imagine. It’s usually where most large applications end up due to its scalability, stability, and maintainability.
All Android Apps are written in Java.
Java is a verbose language, which at first can seem daunting. However, after learning the basics you’ll find that you can easily grab onto more advanced concepts because the code is very explicit.
Hope it will help you!
|
|
|
|
|
Kimberly Weldon wrote: All Android Apps are written in Java. Not true, many are now written in C, C++, C# etc.
|
|
|
|
|
who will prefer C++ over java if both languages are gonna give the same result?! 
|
|
|
|
|
Probably all those people who have not learned Java and do not want to start now.
|
|
|
|
|
i've completed 60% of the book by now.... its syntax it quite similar to C#
and netbeans is blowing my mind
..i recently found out that it was written in java...
but the newer versions of visual studio are way better than it.
|
|
|
|
|
|
Hi guys I gave up on this code ! trying figure out how if condition became true ! since
i never will be greater than
j
public static void main(String[] args){
int a[]={4,2,7};
for(int i=0; i< a.length ;i++) {
for(int j=i; j< a.length ;j++) {
if (a[i]>a[j]){
System.out.print("condition is match ");
}
}
}
}
i start with 0
j start with 0
j became 1
j became 2
i became 1
j start with 1
j became 2
i became 2
j start with 2
I got confuse ,
modified 20-Jun-16 13:57pm.
|
|
|
|
|
You're not comparing i to j ; you're comparing a[i] to a[j] .
Look at what happens when i = 0 and j = 1 :
a[i] == 4
a[j] == 2
a[i] > a[j]
=== 4 > 2
=== true
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thank you Richard for your replay in fact, this is my question how on the second loop j start with 1 ? should start with 0 since j always j=i ; and i start with 0 , then j will be 0 so a[i] will be 4 and a[j] will be 4 ?
for(int i=0; i< a.length ;i++) {
for(int j=i; j< a.length ;j++) {
modified 20-Jun-16 14:16pm.
|
|
|
|
|
j doesn't start with 1 ; it starts with i , and then increments for each pass of the inner loop.
i = 0
j = 0 a[i] = 4; a[j] = 4
j = 1 a[i] = 4; a[j] = 2
j = 2 a[i] = 4; a[j] = 7
i = 1
j = 1 a[i] = 2; a[j] = 2
j = 2 a[i] = 2; a[j] = 7
i = 2
j = 2 a[i] = 7; a[j] = 7
You really need to learn to use a debugger. Rather than trying to guess what your code is doing, step through it line by line and examine the values of the variables.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I came over this post again , it surprise for me to not see my last post ,
any way, Dear Richard thank you so much for clearing the process steps of loop , I really appropriate your time. and i'm sorry to replay back late i just shack to not see my thank you post I may closed the browser before public the post
![Rose | [Rose]](https://www.codeproject.com/script/Forums/Images/rose.gif)
|
|
|
|
|
There're 9 numbers in a 3 x 3 plane. We could only clockwise or counterclockwise rotate the four tiles around one of the four points, I II III and IV. R1, R2, R3, R4 and r1, r2, r3, r4 are corresponding to the 8 kinds of rotation, where we mark clockwise rotations as r and counterclockwise rotations as R. For example, starting from configuration (S), by the rotation sequence r1R4r2R3 we reach the configuration (E). Now, starting from configuration (S), find the shortest way to reach configuration (T).
If I want to program to solve the problem,do you have any good idea?
[Cilck here for detail picture]
|
|
|
|
|
Your homework is set to test what you know, not how good you are at getting strangers to do your work for you.
Try it yourself. It should be based on concepts that you have recently covered in your course, so you'll probably find it's actually not as difficult as you think.
If you don't know where to start, then talk to your teacher.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
that't not homework,my friend ask me the question, I think it's funny, so I want to solve it with an Android app,traversal need too much steps,so I need an idea about math models
|
|
|
|
|
zarkerBlack wrote: so I need an idea about math models Then you should use Google to do some research. This forum is for Java questions.
|
|
|
|
|
I already have done the work like this ,the real problem is about machine learning,how could the machine konw the way to the result,I have three ideas,one is let machine try all the ways,another is find the law,it like the deduction of mathematic,the last one is that,let machine think like man,it could do the work like a real person ,maybe i need to design a classifier like SVM to training the machie。
Do you have any advice about last two?
|
|
|
|
|
As I explained in my previous message, this has nothing to do with Java. You need to do some research on machine learning.
|
|
|
|
|
I have class:
public class book implements Serializable {
private static final long serialVersionUID = 1L;
private String title = "N N";
private String author = "N N";
private int price = 0;
public book()
{
}
public book(String title, String author, int price)
{
setTitle(title);
setAuthor(author);
setPrice(price);
}
public void setTitle(String title)
{
this.title = title;
}
public void setAuthor(String author)
{
this.author = author;
}
public void setPrice(int price)
{
this.price = price;
}
public int getPrice()
{
return price;
}
public String getTitle()
{
return title;
}
public String getAuthor()
{
return author;
}
public void skrivUt()
{
System.out.println("");
System.out.println("Titel: " + title);
System.out.println("Författare: " + author);
System.out.println("Pris: " + price);
}
}
And then a list to which I save the objects:
book aBook = new book(title, anAuthor , thePrice);
aList = new ArrayList<book>();
aList.add(aBook);
I save the list:
FileOutputStream fil = new FileOutputStream("C:\\SavedObjects\\Objektfil.dat");
ObjectOutputStream oostr = new ObjectOutputStream(fil);
oostr.writeObject(aList);
oostr.close();
And gets it back:
FileInputStream fil = new FileInputStream("C:\\SavedObjects\\Objektfil.dat");
ObjectInputStream oistr = new ObjectInputStream(fil);
aList = (ArrayList<book>) oistr.readObject();
oistr.close();
Goes through the list in a textArea:
for (book b : aList) {
ta1.append("Titel " + b.getTitle() + "\n");
ta1.append("Författare: " + b.getAuthor() + "\n");
ta1.append("Pris: " + Integer.toString(b.getPrice()) + "\n");
ta1.append("\n");
}
This works fine. The problem is that I only get back (or so it seems) the first object in the list.
|
|
|
|
|
I just ran a slightly modified version of your code and it returned all the items. I did find that the object reader did not return the list as an ArrayList<Book> (maybe my mistake) so had to modify the foreach to accept a List of objects and cast each one to a Book.
[edit]
A slight modification shows that your code should work fine, and return the full list.
[/edit]
modified 14-Jun-16 10:43am.
|
|
|
|
|
Thanks for your reply.
Could you show me some code?
I tested to add some objects by writing in the textfields and pressing the button (btn).
If I do this for some books and then hit the second button (btn2) it only shows the first book in the list.
If I add a book and then hits the second button it show that book. If I add a second book and hits the second button again, it shows both books.
|
|
|
|
|
That is different from your original question. The code that works is the code I copied from your question, and the only changes I made are:
ArrayList<Book> bList = (ArrayList<Book>) oistr.readObject();
oistr.close();
for (Book bb : bList) {
bb.skrivUt();
}
which worked, and listed all the items I added to the original list.
You are now asking something concerned with pressing buttons, which you have not shown in your code.
|
|
|
|
|
 Ok, I'll try again.
This is my total code in a class that inherits from JFrame instantiated from main(). Slightly changed because I come from Sweden.
So I enter the info in the textfield for title, author and price.
If I enter the info for one book and then press "show books" it works. I enter another book and press show books again it still works. The textarea shows all books.
If I enter a book and doesn't press "show books", enter a few others and then hit "show books" it only shows the last entered book.
If I have entered a few books using the first method (enter book, press "show books", enter another book, press show books again)and then quits using the Close-button one books is saved, not more.
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
public class MainFrame extends JFrame implements ActionListener, MyContacts {
private static final long serialVersionUID = 1L;
public JTextField txtFieldTitle;
private JTextField txtFieldAuthor;
private JTextField txtFieldPrice;
private JTextField txtFieldLanguage;
private JTextArea ta1;
private JButton btn;
private JButton btn2;
private JButton btnClose;
private JButton btnRemove;
private JRadioButton proRadio;
private JRadioButton amateurRadio;
private ButtonGroup playersBtnGroup;
private ArrayList<book> aList;
private JLabel txtTextArea;
private JLabel txtTitle;
private JLabel txtAuthor;
private JLabel txtPrice;
private Border redBorder = BorderFactory.createLineBorder(Color.red);
public MainFrame(){
super("Library");
setLayout(null);
setSize(1200, 1000);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
getContentPane().setBackground(Color.red);
loadBook();
btn = new JButton("Add book");
btn.setBounds(120,70,200,20);
add(btn);
btn.addActionListener(this);
btn2 = new JButton("Show books");
btn2.setBounds(220,470,200,20);
add(btn2);
btn2.addActionListener(this);
btnClose = new JButton("Close");
btnClose.setBounds(150,800,200,50);
add(btnClose);
btnClose.addActionListener(this); händelser
txtFieldTitle = new JTextField();
txtFieldTitle.setBounds(570,70,200,20);
add(txtFieldTitle);
txtFieldAuthor = new JTextField();
txtFieldAuthor.setBounds(570,110,200,20);
add(txtFieldAuthor);
txtFieldPrice = new JTextField();
txtFieldPrice.setBounds(570,150,200,20);
add(txtFieldPrice);
ta1 = new JTextArea();
ta1.setBounds(720,470,400,420);
ta1.setBorder(redBorder);
add(ta1);
txtTextArea = new JLabel("Böcker");
txtTextArea.setBounds(720,430,200,20);
add(txtTextArea);
txtTitle = new JLabel("Titel");
txtTitle.setBounds(500,70,200,20);
add(txtTitle);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == btn)
{
String title = txtFieldTitle.getText();
String anAuthor = txtFieldAuthor.getText();
String anPrice = txtFieldPrice.getText();
String aLanguage = txtFieldLanguage.getText();
int thePrice = Integer.parseInt(anPrice);
book aBok = new book(title, anAuthor , thePrice);
aList = new ArrayList<book>();
aList.add(aBok);
}
if(e.getSource() == btn2)
{
for (book b : aList) {
ta1.append("Titel " + b.getTitle() + "\n");
ta1.append("Författare: " + b.getAuthor() + "\n");
ta1.append("Pris: " + Integer.toString(b.getPrice()) + "\n");
ta1.append("\n");
}
}
if(e.getSource() == btnClose)
{
saveBook(aList);
System.exit(0);
}
}
public void saveBook(ArrayList<book> aList)
{
try
{
FileOutputStream fil = new FileOutputStream("C:\\SavedObjects\\Objektfil.dat");
ObjectOutputStream oostr = new ObjectOutputStream(fil);
oostr.writeObject(aList);
oostr.close();
}
catch (IOException e)
{
System.out.println(e);
System.out.println("Nu blev det visst fel i spara Bok");
}
}
@SuppressWarnings("unchecked")
public void loadBook()
{
try
{
FileInputStream fil = new FileInputStream("C:\\SavedObjects\\Objektfil.dat");
ObjectInputStream oistr = new ObjectInputStream(fil);
aList = (ArrayList<book>) oistr.readObject();
oistr.close();
}
catch (IOException e)
{
System.out.println(e);
System.out.println("Something went wrong in IO");
}
catch (ClassNotFoundException e)
{
System.out.println(e);
System.out.println("Could not find class...");
}
}
}
|
|
|
|
|
if(e.getSource() == btn)
{
String title = txtFieldTitle.getText();
book aBok = new book(title, anAuthor , thePrice);
aList = new ArrayList<book>();
aList.add(aBok);
}
Every time you add a book you create a new ArrayList<book>() , so you are losing the previous books. You need to create your list at the beginning of the program and add to that as you enter more books.
|
|
|
|
|