Java代写:java

java代写:本次作业为java代写,涉及数据结构和编译原理方面的知识
8.4
这道题主要是要建立一个类似List的东西 叫Videosammlung(video collection)
1 Implement the class Video with the private attributes String title,
int id and String [] genres, as well as the getter methods String getTitel (),
int getId (), String [] getGenres () and the constructor Video (String title).
The ID of the first movie should be 0, the following are numbered in ascending order.

2. Expand the class Video to include public methods int addGenre (String
genre). This adds one more to the genres, with a maximum video only
can belong to five genres. The method should also prevent the same
Genre is added multiple times. The method returns on successful addition
the genre returns the entire number of genres for this video and -1 if that
Genre could not be added.

3. Implement the Video Collection class, which has a maximum video collection
n represents videos. Here, the size n of the video collection in the constructor
to be handed over.

4. Add a method int addVideo (Video v) to the Video Collection class
where the video v occupies the first free position of the video collection and the
Position of the video is returned. If the video collection is full, so
returns the method -1.

5. Expand the Video Collection class to include the Video Verkaufen (int
position) and Video verkaufen (string title). Both methods delete each
a video from the collection, where position is the same as the position
Add this video was returned, and title the title of the video,
that should be deleted. Is there a title multiple times in the collection, so will
only the first instance deleted. The method returns the sold video or
null if no video was deleted.

6. Insert an object variable private int verbleibende , in which the number
remaining free places is stored. The value of this variable should be at each
Calling the addVideo- and verkaufen- methods can be meaningfully modified.

7. Then add a public int object method to the Videosammlung class
getVerbleibende (), which returns the number of seats remaining,
without checking the individual seats

8. Extend the Videosammlung class by a String [] videosInGenre method
(String genre). The method returns a string array titled All Videos
the passed string genre, back.

8.5
This task is about implementing the (un) known data structure Worstorage,
the (un) known comparable objects (in our case, penguins, which after
their sweetness are ordered) can save particularly inefficient.
A Worstorage consists internally (private object attributes) of an array ps of the size
n = 2k-1 (k大于等于 1) with the references to the stored objects, which are in k levels
and an int array count of size k, in which for each level the number
is recorded on entries.
We number the positions in the array from 1 (array index 0) to n (index n – 1). Of the
direct left-hand followers of an element at position p, if present, is the element
Position 2p, the direct right successor, if any, is the element at position 2p + 1. Successors in general are then defined inductively as follows. Left followers are the direct left successor as well as all his successors. Similarly, the right ones
Successor all successors of the right successor as well as this himself. The first level
consists only of position 1. The number of elements stored there (0 or 1)
is at index 0 in array count. All other levels e contain all the direct ones
Successor to elements of level e – 1.
To quickly access the stored objects, the following invariant applies:
All left successors are always smaller, all right successors larger or equal
Elements. In addition, there must be no gap in the memory: Has an element no direct
left (or right) successor, it may be no left (or right) successor
give. Use the compareTo-1 function of the Penguin class to compare two
Penguins. This is already implemented in the code framework Worstorage.java.
If there are no elements in the array (all entries are null), the above invariant is already fulfilled.
The methods to be implemented must ensure that they are always preserved.
In addition, count must continue to use the correct ones even after the methods have been executed
Contain values.
In detail, the following object methods in the code framework Worstorage.java in
to be implemented in the Worstorage class:

1. public void add(Penguin penguin) : Insert a penguin if it is still not included. If the memory is insufficient, the number of entries should be at the double number plus 1 rise. The number of levels is increased accordingly by 1, that means, count refers to an array larger than 1 after the operation
before.

2. public Boolean find(Penguin penguin) :Returns true if and only if
the argument is included. To find the animal, the invariant should be used
become. It should therefore not be searched in the entire array.

3.public void remove(Penguin penguin) : Removes the argument penguin, if included.
Close any resulting gaps, so that again a consistent state
is reached. It may only be successors(followers) of the item that deleted
was going to change their location. All other elements stay in place.
Also, remember to update count. no more entry (count has the value 0 at the last index), so should the number Memory locations reduced from x to (x-1) / 2 and the number of levels (length of count) can be decreased by 1. But it should always at least one level and one
Give space.

4.public String toString(): Returns the internal penguin array in the following format
back: All entries are successively, starting with index 0, with commas
spent separately. null entries are given by the empty string
represented, penguins by the Knuffigkeit, so the corresponding int value.
Nothing else is allowed to be returned. Example: new Penguin [] {p1, null, p2,
null, zero, p3, null} is returned as “-1,, 5, 3,” if and only if
the penguins p1, p2, p3 have the Knuffigkeit -1, 5 and 3.

The constructor should initially set the size such that it is exactly one level and
Accordingly, there is exactly one storage space for penguins.

Note: You may not use additional arrays or other containers. This
means that you do not insert or delete elements outside the data structure
can buffer.

Example (with numbers instead of penguins):

8.6
Parsing is the conversion of program text into a format which
You can then process in further steps such that at the end of a machine program
comes out (for example, for our plug-in from last sheet). in the
In general, the implementation of parsers involves a rich theory2
– In this task, however, we will work with as simple a recursive as possible
Descent parsers that work exclusively on MiniJava programs
can. Our parser should also be here only for a given MiniJava program
output whether this is syntactically correct.
First of all, provide the following framework for your implementation:

import java.util.Scanner;

public class MiniJavaParser {
public static String readProgramConsole() {
@SuppressWarnings(“resource”)
Scanner sin = new Scanner(System.in);
StringBuilder builder = new StringBuilder();
while (true) {
String nextLine = sin.nextLine();
if (nextLine.equals(“”)) {
nextLine = sin.nextLine();
if (nextLine.equals(“”))
break;
}
if (nextLine.startsWith(“//”))
continue;
builder.append(nextLine);
builder.append(’\n’);
}
return builder.toString();
}

public static String[] lex(String program) {
return null; // Todo
}

public static int parseNumber(String[] program, int from) {
return -1; // Todo
}

public static int parseName(String[] program, int from) {
return -1; // Todo
}
public static int parseType(String[] program, int from) {
return -1; // Todo
}

public static int parseDecl(String[] program, int from) {
return -1; // Todo
}

public static int parseUnop(String[] program, int from) {
return -1; // Todo
}

public static int parseBinop(String[] program, int from) {
return -1; // Todo
}

public static int parseComp(String[] program, int from) {
return -1; // Todo
}

public static int parseExpression(String[] program, int from) {
return -1; // Todo
}

public static int parseBbinop(String[] program, int from) {
return -1; // Todo
}

public static int parseBunop(String[] program, int from) {
return -1; // Todo
}

public static int parseCondition(String[] program, int from) {
return -1; // Todo
}

public static int parseStatement(String[] program, int from) {
return -1; // Todo
}

public static int parseProgram(String[] program) {
return -1; // Todo
}
public static void main(String[] args) {
// Todo
}

}

In the framework, there is a method for each non-terminal of the MiniJava grammar.
Each of these methods expects an array of tokens as parameters and one
Start Index. A token is a logically connected unit of the input program,
e.g. the keyword read. The method is to parse your nonterminal and the index
of the next token behind the nonterminal. If the parsing fails, should
a negative number will be returned.
Before a program can be parsed, it must first be split into tokens. In
our case is a token.

• a parenthesis (both round and curved) or a comma,
• an operator
• a number or
• a name (including keywords such as read, write, or int).
Spaces and line breaks are discarded during the lexicon. The MiniJava
Program

int sum, n, i;
n = read();
while (n < 0) { n = read(); } is therefore placed in the token array [int, sum, ,, n ,, ,, i,;, n, =, read, (,),;, while, (, n, <, 0,), {, n, =, read, (,),;,}] decomposed. Go in your implementation as follows: 1. Implement the method String [] lex (String program), which is a program converted into an array of tokens. 2. Implement the remaining methods of the parser. It may be useful to others Add helper methods. 3. Implement a main program that asks the user to enter a Program asks and then outputs whether this program is parsed correctly could be. Use the method already known from the last sheet String readProgramConsole (). Note: You may padden your token array, so add empty strings to the end, if that helps you distinguish between fewer cases within the parser. Especially can be avoided so often checked for the end of input must become. Note: If you miss the conversion to a format mentioned in the introduction, Please be patient with one of the following exercise sheets. Note: Test your implementation. Take advantage of the following program: int sum, n, i; n = read(); while (n < 0) { n = read(); } sum = 0; i = 0; while (i < n) { { if (i % 3 == 0 || i % 7 == 0) { sum = sum + i; if (i % 3 == 0 || i % 7 == 0) { sum = sum + i; } else sum = 99; } i = i + 1; } } write(sum); This program is a correct MiniJava program. Change the program to to test if your parser rejects wrong programs.

发表评论

电子邮件地址不会被公开。 必填项已用*标注