المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : اسئلة في الجافا عاجلة



sondos
10-11-2005, 09:06 AM
السلام عليكم

اصدقائي حاولت احل هالسؤال الصعب لكن ما قدرت فقلت احط لكم السؤال ومحاولتي في حله
وعطوني تنبيهاتكم ومشورتكم لان عليه 10 درجات
السؤال هو

ii) This question requires you to write and test a program that, using the
StreamTokenizer class (Budd, Section 14.3), will read a file containing
numbers, and print them to a second file with a ‘+’ printed before each
number, and an ‘=’ sign at the end followed by the sum of all the numbers.
For example, if the input file contained ‘5 23 11’ then the output file should
contain ‘+ 5.0 + 23.0 + 11.0 = 39.0’.
In doing this question, you should carry out the following steps.
1 Create a new folder called Streams in JavaProjects\TMA01.
2 Start up your IDE and create a new project called AddingNumbers.jpr in
the folder JavaProjects\TMA01\Streams.
3 Add a new class called Adder to this project, which includes both a main
method and a constructor (see Practical Activity 2.2 in the IDE
Handbook for details on how to do this).
4 Within the constructor for Adder, write a try statement that will create:
• a BufferedReader object named input, which is capable of reading
characters from a text file named MyInputFile.txt stored in a directory
of your own choice such as C:\JavaProjects\TMA01\Streams. (See
Budd, Chapter 14, Section 14.7). Note that in a Java string, whenever
you want to include the character '\' you should use '\\'.
• a BufferedWriter object named output, which is associated with a file
called MyOutputFile.txt, to which the processed input from
MyInputFile.txt can be written. For convenience, we suggest
MyOutputFile.txt be located in the same directory as MyInputfile.txt.
5 Create the text file named MyInputFile.txt (using an editor such as
Notepad) in the directory you used in step 4 above. It should contain
three or four numbers with spaces between the numbers (e.g. 5 23 11).
Note that you do not have to create MyOutputFile.txt before running the
program: it will be automatically created when the program is executed.
6 Define a method called run that processes the numbers in MyFile.txt
according to the requirements described above. You should use the
facilities of the class StreamTokenizer to do this. Budd, Chapter 14,
Section 14.3 illustrates the use of the StreamTokenizer class, but you may
also find the description given in the box below helpful.
7 Write an appropriate main method for Adder.
8 Run your program.
Include in your Solution Document:
(a) a copy of the code of your class Adder; [7 marks]
(b) a window showing the contents of your text file MyFile.txt; [1 mark]
(c) a window showing the contents of your output file, MyOutputFile.txt. [2 marks]


The class StreamTokenizer
A StreamTokenizer object takes either an InputStream or a Reader (such as input in the first
bulleted point of step 4 above) and splits it into a sequence of tokens (either bytes in the
case of an InputStream or characters in the case of a Reader). The next token in the stream
can be obtained by using the method nextToken (). The StreamTokenizer class defines four
constants (all of type int and non-zero):
TT_EOF, which represents the special token that marks the end of a file;
TT_EOL, which represents the token that marks the end of a line;
TT_NUMBER, which represents a number;
TT_WORD, which represents a word.
After you have read a token using nextToken, the int data field ttype contains one of the
above constants. If the last token read is a number (ttype == TT_NUMBER), its value will
be in the floating point (double) variable nval; if the last token read is a word (ttype ==
TT_WORD), the word will be in the String variable sval.
To access the class StreamTokenizer, you need to import it from java.io.*
The class StreamTokenizer also allows you to define how a word or number is defined, but
for this exercise simply accept the default definitions which, in essence, say that words or
numbers are delimited by spaces and normal punctuation marks



وهذي محاولتي لحل السؤال


import java.io,*;
public class Adder
try{
BufferedReader input=new BufferedReader(MyInputFile);
BufferedWriter output=new BufferedWriter(MyOutPutFile)
}
catch(FileNoFoundExceptuin e){
run()=new StreamTokenizer(System.input)

run.TT-Number
System.Print("+"+"number"+"="+"sum)")





ممكن تصححولي هذا السؤال ايضا
This question requires you to define a class that will represent a list of
objects, taken in order, with a specified maximum number of elements. For
example, such a data type may be used to represent a waiting list of names
for admission to a school. Prospective students are normally added at the end
of the list, but if they have a sibling already at the school then they can be
inserted at an earlier point. Once the list reaches its maximum size, and
becomes full, no further students can be added until the person at the top has
been admitted to the school, and removed from the waiting list, thus freeing
up a space.
Assume that the class implementing such a list is called MyList and has the
following public constructor and methods (note that to the user of the list,
the first element of the list should be in position 1 and not 0):
• a constructor, MyList (int m), which creates a new empty list that can
have a maximum of m elements.
• isFull (), which returns the boolean true if the list has the maximum
number of elements in it, and false otherwise.
• first (), which returns the element at the top of the list. (There is no need
to worry about the situation when there are no elements in the list, since
we will assume that the user of this method must check that the list is
not empty when this method is called.)
• removeFirst (), which removes the element at the top of the list. (As
before you may assume that the list is not empty, so there is no need for
your code to check whether or not the list is empty.)
• addElement (Object o), which adds the Object o to the end of the list. (In
this case, it is assumed that the method will only be called if the list is
not full, so there is no need for your code to check whether or not the list
is full.)
• addElement (Object o, int n), which inserts the Object o at position n in
the list. (It will be assumed that this method must not be called if n is
less than 1 or greater than the number of elements in the list. That is, n
must not fall outside the range of the current number of elements in the
list. So, you do not have to write code to cover this situation.)
• getElement (int n), which returns the element of class Object in position
n if there are n or more elements in the list. (As with addElement, you
may assume that n is at least 1 and not greater than the current number
of elements in the list.)
• numInList () , which returns the int value of the number of elements in
the list.
(a) Create a new folder in JavaProjects\TMA01 called List, and create a new
project TestMyList.jpr in this new folder. Add a new class, called MyList,
to your project, and define a full implementation of this class using
composition based on the class Vector (the operations of which are
shown in Budd, Table 19.2). Note that you will need to include the
appropriate import statement to be able to use Vector. Your class should
contain two data fields:
• one called theData of type Vector for storing the elements of the list;
• the other called maxElements, of type int for storing the maximum
size of the list.
[NB DO NOT attempt to execute the project at this stage. It is incomplete
and requires an additional class, which you will add in part (c) below,
before it can be run and tested.]
Copy the text of your implementation into your Solution Document. [8 marks]
(b) Why would you use composition as opposed to inheritance to create the
MyList class? [3 marks]
(c) The file TestMyList.java is one of the files that would have been
downloaded onto your machine as part of the zip archive file containing
TMA01. Copy TestMyList.java into JavaProjects\TMA01\List\src (the
folder src would have been created when you developed the class
MyList).
TestMyList.java contains a class that will test your MyList class
developed in part (a). Add it to your TestMyList project and execute the
program. Include a copy of the output window in your Solution
Document. [2 marks
وهذه هي الاجابة
public class MyList {
private Vector data;
public MyList(int m){
theData= new Vector(m);
maxElements= int m;
}
private theData.isFull(){
if((theData.capicity)= maxElements)
theData.removeFirst(object o);
else
theData.addElement.(object o)
}
private theData.first(object o){
theData.FirstElement(o)
}
public theData. removeFirst(object o){
theData.removeElementAt(int 1)
}
private theData.addElement(object o,1{(
theData.addElement(o,1(
}
private theData.getElement(int n{(
while (n<=m)
if (theData.elmentAt(n))=(thData.addElment(o))
theData.insertElmentAt(o,(n+1)) }
private theData.numInList(){
theData.size=m

}
شكرا لكل المشاركين بالتوضيح او الشرح او الراي