Question

In: Computer Science

1. public class MyString { private char[] data; MyString(String string) { data = string.toCharArray(); } public...

1.
public class MyString {
    private char[] data;

    MyString(String string) { data = string.toCharArray(); }
    public int compareTo(MyString other) { /* code from HW1 here */ }
    public char charAt(int i) { return data[i]; }
    public int length() { return data.length; }
    public int indexOf(char c) { /* code from HW1 here */ }
    public boolean equals(MyString other) { /* code from HW1 here */ }

    /*
    * Change this MyString by removing all occurrences of
    * the argument character c. For example, if MyString s
    * represents the text "radar", then s.remove('a') will
    * change s so that it now represents the text "rdr".
    */
    public void remove(char c) {
        /* Type in the box below the code that should go here. */
    }
}

public class ArrayIntSet {

private int[] data;

private int size;

public ArrayIntSet(int capacity) {

data = new int[capacity];

size = 0;

}

2.

public int size() { return size; }

public boolean contains(int i) { /* Code from HW3 here */ }

public boolean addElement(int element) { /* Code from HW3 here */ }

private int index(int element) { /* Code from HW3 here */ }

public boolean removeElement(int element) { /* Code from HW3 here */ }

public boolean equals(ArrayIntSet other) { /* Code from HW3 here */ }

public void union(ArrayIntSet other) { /* Code from HW3 here */ }

public void intersect(ArrayIntSet other) { /* Code from HW3 here */ }

/*

* Returns a new ArrayIntSet that contains

* all the even numbers from this set.

*/

public ArrayIntSet evenSubset() {

/* Write code for this method in the box below */

}

}

Solutions

Expert Solution

1.public void remove(char c) {

int j, count = 0, n =string.length();//to get the length of string or we can call 'length()'because it already declare function

    char []t = string.toCharArray();//to get string to array or we can call 'data' because it already decalre in above so same 'data' array will be used as 't'

    for (int i = j = 0; i < n; i++)

    {

        if (t[i] != c)

        t[j++] = t[i];

        else

            count++;

    }

     

    while(count > 0)

    {

        t[j++] = '\0';

        count--;

    }

     

    System.out.println(t);

}

2.public int[] evenSubset(){

int[] temp;

temp = new int [size];

int j = 0;

for(int i=0;i<data.length;i++){ //here data used for array set

if(data[i]%2 == 0) {

temp[j++] = data[i];

}

}

return temp;// temp is new array which contain even number

}


Related Solutions

public class StringNode { private String item; private StringNode next; } public class StringLL { private...
public class StringNode { private String item; private StringNode next; } public class StringLL { private StringNode head; private int size; public StringLL(){ head = null; size = 0; } public void add(String s){ add(size,s); } public boolean add(int index, String s){ ... } public String remove(int index){ ... } } In the above code add(int index, String s) creates a StringNode and adds it to the linked list at position index, and remove(int index) removes the StringNode at position...
public class StringTools {    public static int count(String a, char c) {          ...
public class StringTools {    public static int count(String a, char c) {           }
public class GroceryShopping {    //declared variable    private String vegetableName;    private String fruitName;   ...
public class GroceryShopping {    //declared variable    private String vegetableName;    private String fruitName;    private double vegetablePrice;    private double fruitPrice;    private double vegetableOrdered;    private double fruitOrdered;       //declared constants    public static final double SERVICE_RATE =0.035;    public static final double DELIVERY_FEE=5;       public GroceryShopping( String vegetableName, String fruitName, double vegetablePrice, double fruitPrice)    {        this.vegetableName = vegetableName;        this.fruitName = fruitName;        this.vegetablePrice = vegetablePrice;        this.fruitPrice...
public class Classroom { // fields private String roomNumber; private String buildingName; private int capacity; /**...
public class Classroom { // fields private String roomNumber; private String buildingName; private int capacity; /** * Constructor for objects of class Classroom */ public Classroom() { this.capacity = 0; }    /** * Constructor for objects of class Classroom * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Classroom(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room...
java programing Q: Given the following class: public class Student { private String firstName; private String...
java programing Q: Given the following class: public class Student { private String firstName; private String lastName; private int age; private University university; public Student(String firstName, String lastName, int age, University university) { this.firstName = fisrtName; this.lastName = lastName; this.age = age; this.university = university; } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public University getUniversity(){ return university; } public String toString() { return "\nFirst name:" + firstName +...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity;...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity; public Room() { this.capacity = 0; } /** * Constructor for objects of class Room * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Room(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room number. * * @param rN a new room number...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity;...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity; public Room() { this.capacity = 0; } /** * Constructor for objects of class Room * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Room(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room number. * * @param rN a new room number...
Assume you have created the following data definition class: public class Book {    private String title;...
Assume you have created the following data definition class: public class Book {    private String title;    private double cost;       public String getTitle() { return this.title; }    public double getCost() { return this.cost; }       public void setTitle(String title) {       this.title = title;    } // Mutator to return true/false instead of using exception handling public boolean setCost(double cost) {       if (cost >= 0 && cost < 100) {          this.cost = cost;          return true;       }       else {          return false;       }...
Assume you have created the following data definition class: public abstract class Organization { private String...
Assume you have created the following data definition class: public abstract class Organization { private String name;    private int numEmployees;    public static final double TAX_RATE = 0.01;    public String getName() { return this.name; }    public int getNumEmployees() { return this.numEmployees; }         public abstract double calculateTax() {       return this.numEmployees * TAX_RATE;    } } In your own words, briefly (1-2 sentences) explain why the data definition class will not compile. Then, write modified code that...
import java.util.Scanner; public class test {    public static void main(String args[]){        char letter;...
import java.util.Scanner; public class test {    public static void main(String args[]){        char letter;        int number = 0;        Scanner in = new Scanner(System.in);        System.out.print("Enter a letter: ");        letter = in.next().charAt(0);        if(letter == 'A' || letter == 'B' || letter == 'C') number = 2;        if(letter == 'D' || letter == 'E' || letter == 'F') number = 3;        if(letter == 'G' || letter ==...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT