Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Remote.java
- -----------
- import java.util.Arrays;
- public class Remote {
- private String[] channels;
- public Remote(int size) {
- channels = new String[size];
- for (int i = 0; i < channels.length; i++) {
- channels[i] = "";
- }
- }
- public boolean addChannel(String name, int index) {
- if (index < channels.length && channels[index].length() == 0) {
- channels[index] = name;
- return true;
- }
- return false;
- }
- public int find(String name) {
- // -1 if not found
- for (int i = 0; i < channels.length; i++) {
- if (channels[i].equals(name)) {
- return (i + 1);
- }
- }
- return -1;
- }
- public void remove(String name) {
- int index = find(name);
- if (index != -1) {
- remove(index);
- }
- }
- public void remove(int index) {
- channels[index - 1] = "";
- }
- public void sortByName() {
- Arrays.sort(channels);
- }
- public void move(int index1, int index2) {
- String tmp = channels[index1];
- channels[index1] = channels[index2];
- channels[index2] = tmp;
- }
- @Override
- public String toString() {
- String list = "";
- for (int i = 0; i < channels.length; i += 1) {
- list += (i + 1) + " : " + channels[i] + "\n";
- }
- return list;
- }
- }
- Tester.java
- -----------
- public class Tester {
- public static void main(String[] args) {
- Remote myRemote = new Remote(10);
- myRemote.addChannel("MBC 4", 6);
- myRemote.addChannel("zee", 1);
- System.out.println(myRemote);
- myRemote.sortByName();
- System.out.println(myRemote);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement