Click here to Skip to main content
15,889,281 members
Home / Discussions / Java
   

Java

 
AnswerRe: Java Graphics and Desktop Pin
427748024-Sep-09 18:51
427748024-Sep-09 18:51 
GeneralRe: Java Graphics and Desktop Pin
sharkbc24-Sep-09 19:48
sharkbc24-Sep-09 19:48 
GeneralRe: Java Graphics and Desktop Pin
sharkbc24-Sep-09 20:27
sharkbc24-Sep-09 20:27 
GeneralRe: Java Graphics and Desktop Pin
427748025-Sep-09 0:34
427748025-Sep-09 0:34 
GeneralRe: Java Graphics and Desktop [modified] Pin
sharkbc25-Sep-09 15:55
sharkbc25-Sep-09 15:55 
QuestionJ2ME Pin
Matt Cavanagh24-Sep-09 9:51
Matt Cavanagh24-Sep-09 9:51 
AnswerRe: J2ME Pin
427748024-Sep-09 18:50
427748024-Sep-09 18:50 
Questionplz help in java question Pin
arifihsan24-Sep-09 0:35
arifihsan24-Sep-09 0:35 
Exercise 1
Write a program Backwards.java that reads a line of text from the keyboard and then prints it in reverse order on the screen. An execution might look like this:
Type a line of text: Hello! My name is Jonas Lundberg.
Backwards: .grebdnuL sanoJ si eman yM !olleH


Exercise 2
Write a program LargestK.java that for an arbitrary positive integer N (read from the keyboard) computes the largest K such that 0+2+4+6+8+...+K < N.

Exercise 3
Write a program CountDigits.java that for an arbitrary positive integer N (read from the keyboard) prints the number of zeros, odd digits, and even digits. An execution might look like this:
Provide a positive integer: 6789500
Zeros: 2
Odd: 3
Even: 2

Notice, we treat zero as neither odd nor even.

Exercise 4
Write a program HighLow.java that implements the simple guessing game High and Low. The program selects a random number between 1 and 100. The player than tries to guess its value. After each, the program gives a hint, "higher" or "lower". An execution might look like this:
Guess 1: 67
Hint: higher
Guess 2: 82
Hint: lower
Guess 3: 77
Correct after only 3 guesses - Brilliant!

The program is terminated after 10 guesses with a suitable comment.

Exercise 5
Write a program Diamond.java that for an arbitrary positive and odd integer (read from the keyboard) prints is "diamond". An execution might look like this:
Provide an odd integer: 7
*
***
*****
*******
*****
***
*

Notice, the integer N determines the thickness of the "diamond" over its waist. The program should terminate with an appropriate error message if the provided integer isn't odd.

Exercise 6
Write a program CountChars.java that counts different types of characters in a text that is read from a file. The program should count how many characters found in the following categories:
Upper case letters
Lower case letters
"Whitespace" (i.e., space, tab, and return)
Other characters.
The search path to the text file you are reading can be coded directly into the program. (We will test your program using our own text file.) Here is an example of a text file. It is an american article about the spanish golfer Sergio Garcia taken from the golf magazine Golf Digest. However, we recommend that you start with something smaller.

After lecture Java 2
Exercise 7
Write a class Arrays.java with the following static methods.
Method int sum(int[] arr) that adds all elements in the array arr and returns the sum.
Method String toString(int[] arr) that returns a string which, if printed, provides a nice looking print out of the array content. Used as:
int n = {3,4,5,6,7};
String str = Arrays.toString(n);
System.out.println("n = " + str);

Method int[] addN(int[] arr, int n) that creates, and returns, a new array where we have added the number n to alla elements in the array arr. The array arr should be left unchanged.
Method int[] reverse(int[] arr) that creates, and returns, a new array with all the elements in array arr but in reverse order. The array arr should be left unchanged.
Method boolean hasN(int[] arr, int n) that returns true if the array arr contains the element n, otherwise false.
Method void replaceAll(int[] arr, int old, int nw) that replaces all occurences of the elemnt old with nw in arr.
Method int[] sort(int[] arr) that returns a new sorted array (least element first) with the same set of elements as in arr. The array arr should be left unchanged.
Method boolean hasSubsequence(int[] arr, int[] sub) that returns true if the subsequence sub is a part of the array arr, otherwise false. For example, in the case hasSubsequence({1,2,3,4,5}, {3,4,5}) the result is true since {3,4,5} is the final part of {1,2,3,4,5}.
Write also a program ArraysMain.java that demonstrates how all these methods work.

Exercise 8
Create a program Histogram.java that reads any number of integers from a file and then prints a simple histogram bar-chart for all integers between 1 and 100. Note: not all integers in the file need to be in the interval [1-100]. An example of an execution:
Reading integers from the file: C:\Temp\integers.dat
Number of integers in the interval [1,100]: 46
Others: 16

Histogram
1 - 10 | ******
11 - 20 | ****
21 - 30 | **
31 - 40 | ***
41 - 50 | *******
51 - 60 | ****
61 - 70 | ***
71 - 80 | *********
81 - 90 | *****
91 - 100 | ***

All exceptions that might be raised during an execution should be handled within the program.

Exercise 9
Write a program (CountJava.java) that prints a numbered list of all Java files (.java) in a given directory (and all its sub-directories). The program should also print the number of lines in each Java file. A program run may look like
Root director: C:\teaching\da1021\
1 Hello.java lines = 14
2 InOut.java lines = 19
3 Formatting.java lines = 22
4 .....

All exceptions that might be raised during an execution should be handled within the program.

Exercise 10
Create a class Pnr.java, representing a Swedish personal identity number on the form YYMMDD-NNNN. Information about the structure of Swedish personal identity numbers can be found at Wikipedia (Personal identity number (Sweden)). The class should contain the following members:
A constructor, creating and initializing an identity number.
Methods getFirstPart and getSecondPart, returning the first part (YYMMDD) and second part (NNNN) of the identity number, respectively.
isCorrect returning true if the number is a correct Swedish personal identity number.
isFemaleNumber, isMaleNumber returning true if the identity number corresponds to a male or female, respectively.
isEqualTo comparing two Pnr instances, checking if they correspond to the same identity number.
toString returning a string representation of the identity number.
Feel free to add more methods, if you think anything is missing. Suitable types for arguments and return values are up to you to decide.

Exercise 11
Write a class Deck that represents an ordinary deck of cards with 52 different cards. Each card has a color (4 possible) and a value (13 possible). The Deck class should have methods for shuffeling the deck, hand out a single card, and count number of remaining cards. Notice, it should only be possible to shuffle the deck when it has 52 cards.

Writa also a test program DeckMain that creates a Deck, hands out a few cards, and displays what cards that was handed out, and the number of cards remaining in the deck.

Exercise 12
News papers are exchanging news using news agencies (e.g., Reuter). A news paper register themself at a news agency and sends their news to that agency. The news agency receives news and sends them to all registred news papers.

Create classes that simulates this scenario.
AnswerRe: plz help in java question Pin
Richard MacCutchan24-Sep-09 1:21
mveRichard MacCutchan24-Sep-09 1:21 
AnswerRe: plz help in java question Pin
427748024-Sep-09 3:37
427748024-Sep-09 3:37 
QuestionLUCKY DRAW system Pin
syarizan23-Sep-09 23:03
syarizan23-Sep-09 23:03 
AnswerRe: LUCKY DRAW system Pin
Richard MacCutchan24-Sep-09 1:22
mveRichard MacCutchan24-Sep-09 1:22 
AnswerRe: LUCKY DRAW system Pin
427748024-Sep-09 2:58
427748024-Sep-09 2:58 
QuestionProblems Implementing Functions into Main.java Pin
wkid8722-Sep-09 17:42
wkid8722-Sep-09 17:42 
AnswerRe: Problems Implementing Functions into Main.java Pin
David Skelly22-Sep-09 22:21
David Skelly22-Sep-09 22:21 
AnswerRe: Problems Implementing Functions into Main.java Pin
Richard MacCutchan22-Sep-09 23:05
mveRichard MacCutchan22-Sep-09 23:05 
GeneralRe: Problems Implementing Functions into Main.java Pin
wkid8723-Sep-09 3:50
wkid8723-Sep-09 3:50 
GeneralRe: Problems Implementing Functions into Main.java Pin
Richard MacCutchan23-Sep-09 5:43
mveRichard MacCutchan23-Sep-09 5:43 
GeneralRe: Problems Implementing Functions into Main.java Pin
David Skelly23-Sep-09 6:24
David Skelly23-Sep-09 6:24 
GeneralRe: Problems Implementing Functions into Main.java [modified] Pin
wkid8723-Sep-09 16:59
wkid8723-Sep-09 16:59 
GeneralRe: Problems Implementing Functions into Main.java Pin
David Skelly24-Sep-09 3:35
David Skelly24-Sep-09 3:35 
AnswerRe: Problems Implementing Functions into Main.java Pin
427748023-Sep-09 18:22
427748023-Sep-09 18:22 
QuestionN Queens Problem help Pin
shiggedyshwa21-Sep-09 19:20
shiggedyshwa21-Sep-09 19:20 
AnswerRe: N Queens Problem help Pin
427748021-Sep-09 20:27
427748021-Sep-09 20:27 
GeneralRe: N Queens Problem help Pin
shiggedyshwa21-Sep-09 21:15
shiggedyshwa21-Sep-09 21:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.