Results 1 to 20 of 33
Thread: Char's questions
- 12-03-2009, 10:07 PM #1
- 12-03-2009, 10:20 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
In the directory where you downloaded the JDK you should find a src.zip file which you can unzip.
- 12-04-2009, 09:09 AM #3
okay heres another one..im trying to do somekind of error handling system for my little program
well its for my school.
and i want to run a program with arguments like this:
i want it to print out error message when i do not have 2 arguments given.Java Code:java program -s 13
and also if the first argument isnt -s ... for example it is -t
and then stop program from workin..
i know i have to stop program with System class..i tried something this:
but it does not work correctly..or atleast i do not apply it correntlyJava Code:System.err.println("First argument is wrong.");
need any help i can get.
- 12-04-2009, 09:29 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
The normal technique is to do something like:
Now, the description of useage format can be found all over the place. You'll see it if you just do "java" from the command line. That's all there is to it, really.Java Code:if (number of args isn't 2, or the first arg isn't "-s") { print out a description of useage. exit program } main program bits go here...
- 12-04-2009, 10:37 AM #5
yeah tahts pretty much what im doin..i was just askin syntax..
but i think i got it workin now..maybe needs some optimization though
thanks for your help :)
Java Code:class kaks_argumenti { public static void main(String[] args){ if (args.length==2){ double a = Double.parseDouble(args[1]); if (args[0].equals("-r")){ System.out.println(Math.acos(a)+" radiaani."); }else if(args[0].equals("-d")){ System.out.println((int)(Math.toDegrees(Math.acos(a)))+" kraadi."); }else{ System.out.println("Etteantud lipud on tundmatud."); System.exit(0); } }else{ System.out.println("Argumentide arv on vale."); System.exit(0); } } }Last edited by Char; 12-06-2009 at 06:50 PM.
- 12-04-2009, 10:52 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
You also want to deal with the situation where they put more or less than 2 arguments to the program...and else, in other words.
- 12-06-2009, 06:48 PM #7
- 12-07-2009, 08:48 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Whoops...missed that!
:)
- 12-20-2009, 04:13 PM #9
anyways im reallly really new to java, and doin everything in one class i think can't be too hard, but object oriented is crazy at the moment for me
i was programming all the basic stuff in python before..but i only did some methods and for,if statements..nothing hard
so can you guys give me a task with multible classes what is good for beginner, and i mean..like really beginner
- 12-20-2009, 04:45 PM #10
Member
- Join Date
- Feb 2009
- Posts
- 57
- Rep Power
- 0
you can type into search engines like "java tutorials"
Last edited by anthrax; 12-20-2009 at 04:45 PM. Reason: x
-
By your statements above and elsewhere in the forum, you don't seem new to programming, just Java. Why not take a favorite program of yours written in whatever language you know, and re-write it from the ground up in Java. For e.g., any programmer worth their salt has attempted one of these: zooming the Mandelbrot set, displaying the game of life or creating a Sudoku solver.
Have fun!
- 12-20-2009, 08:34 PM #12
i think this question was already here, but..how i format double
in python it was float
how is it here?XML Code:print("Circles area is: %0.2f"%(area))
tried something this..but not workinJava Code:System.out.printf("Circles area is: %0.2d",circle.area(4));
also i want 2 spots behind comaLast edited by Char; 12-20-2009 at 08:39 PM.
-
%.2f can work for floating point numbers, but for me, there are too many unknowns here. What is a the circle variable, what does area(4) return. How are things not working? What errors are being shown? Have you set the locale? etc...
- 12-20-2009, 08:52 PM #14
well thanks it worked like that %0.2f
really thank you..you are the most active member in this forum and i see you helping people in all topics...
biggg ups for youuu :)
Java Code:public class Ring extends Kujund { private int raadius; public Ring(int raadius){ this.raadius=raadius; } public int getRaadius() { return raadius; } public void setRaadius(int raadius) { this.raadius = raadius; } public double ümbermõõt(int raadius){ return 2*raadius*Math.PI; } public double pindala(int raadius){ return Math.pow(raadius, 2)*Math.PI; } }btw there were my classes..ring is a circle class..and it has area and girth of it etcJava Code:public class Test { public static void main(String[] args){ Ring ring=new Ring(4); Ruut ruut=new Ruut(5); System.out.printf("Ringi pindala on: %.2f",ring.pindala(ring.getRaadius())); } }Last edited by Char; 12-20-2009 at 08:54 PM.
-
This here bothers me:
The last two methods ignore the radius field held by the object here:Java Code:public class Ring extends Kujund { private int raadius; public Ring(int raadius){ this.raadius=raadius; } //.. public double ümbermõõt(int raadius){ return 2*raadius*Math.PI; } public double pindala(int raadius){ return Math.pow(raadius, 2)*Math.PI; } }
and allow you to pass as a parameter a new radius, that doesn't set the class's raadius field. Better I think would be have these two methods take no parameters, like so:Java Code:private int raadius;
This would allow the methods to use the raadius field held by the object.Java Code:public double ümbermõõt() { return 2*raadius*Math.PI; } public double pindala() { return Math.pow(raadius, 2)*Math.PI; }
- 12-20-2009, 09:08 PM #16
humm wait..i didnt exactly get what you mean?
why shouldnt i use that variable there when im using it inside method?
im a little confused.because english aint my mother tongue
-
Perhaps this is best explained with an example. Say we had a Student class that holds the student's name:
Java Code:public class Student { private String name; public Student(String name) { this.name = name; } public String getName() { return name; } public void displayInformation(String name) { System.out.println("Hi, my name is " + name); } public void displayInformation() { System.out.println("Hi, my name is " + name); } }
There are two displayInformation methods here, one that takes no parameter:
Java Code:public void displayInfo() { System.out.println("Hi, my name is " + name); }
and one that takes a String parameter:
Java Code:public void displayInformation(String name) { System.out.println("Hi, my name is " + name); }
If you create a test program that uses this Student class like so:
You'll see that this is the outputJava Code:public class TestStudent { public static void main(String[] args) { // create a new student whose name is set to "George Bush" Student student = new Student("George Bush"); student.displayInformation(); student.displayInformation("Barak Obama"); } }
Java Code:Hi, my name is George Bush Hi, my name is Barak Obama
The output from the parameterless method on top is correct since we are seeing the true name of the student. The output from the method that passes a parameter is not correct since its name parameter is "shadowing" the Student's name field; it's covering it over and giving us false information.
Make sense?Last edited by Fubarable; 12-20-2009 at 09:26 PM.
- 12-20-2009, 09:32 PM #18
yeah its kinda clear now.
- 01-06-2010, 06:16 PM #19
im trying to use robot class...but why it insists that i must use try and catch
i have no idea what they are..can someone explain
well my code is this..simple but even now i have error..
Java Code:import java.awt.Color; import java.awt.Robot; public class Test { public static void main(String[] args){ Robot robot1=new Robot(); robot1.mouseMove(300, 200); } }
- 01-07-2010, 08:49 AM #20
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Similar Threads
-
questions for 1yr exp
By rahaman.athiq in forum Java ServletReplies: 2Last Post: 11-26-2008, 01:13 AM -
I have Questions -_-
By ChazZeromus in forum New To JavaReplies: 5Last Post: 09-13-2008, 08:08 PM -
Questions About JSP?
By mtz1406 in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 08-19-2008, 07:56 PM -
Just a Few Questions
By pringle in forum New To JavaReplies: 21Last Post: 01-09-2008, 06:21 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks