Question

In: Computer Science

Given an array, write code to scan the array for a particular purpose. Given a class,...

Given an array, write code to scan the array for a particular purpose.

Given a class, write a getter and/or a setter.

Given a class with a list, write code that manages the list.

Solutions

Expert Solution

Since you have not mentioned the language of your preference, I am providing the code in Java.

1.

CODE

public class Main {

public static void main(String[] args) {

int arr[] = {34, 1, 56, 8, 19};

int sum = 0;

for (int i=0; i<arr.length; i++) {

sum += arr[i];

}

System.out.println("Sum of array elements = " + sum);

}

}

2.

class Person {

private String name;

private int age;

private char sec;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public char getSec() {

return sec;

}

public void setSec(char sec) {

this.sec = sec;

}

}

3.

import java.util.ArrayList;

class MyList {

private ArrayList<String> list;

public MyList() {

list = new ArrayList<>();

}

public void add(String x) {

list.add(x);

}

public void delete(String x) {

list.remove(x);

}

public boolean isEmpty() {

return list.isEmpty();

}

public void update(int i, String x) {

list.set(i, x);

}

}


Related Solutions

in java code In the class Hw2, write a method removeDuplicates that given a sorted array,...
in java code In the class Hw2, write a method removeDuplicates that given a sorted array, (1) removes the duplicates so that each distinct element appears exactly once in the sorted order at beginning of the original array, and (2) returns the number of distinct elements in the array. The following is the header of the method: public static int removeDuplicates(int[ ] A) For example, on input A=0, 0, 1, 1, 1, 2, 2, 3, 3, 4, your method should:...
write a code for given an array of integers where wachelement represents the maximum number...
write a code for given an array of integers where wach element represents the maximum number of jumps to reach the end of the array(starting from the first element) if an element O,then no jump can be made from that element if it is not possible to reach the end then output in c
GIVEN THE CODE BELOW Given the following class: /** * Document class. */ public class Document...
GIVEN THE CODE BELOW Given the following class: /** * Document class. */ public class Document { private int words; /** * constructor * pre: none * post: A Document object created. Words initialized to 0. */ public Document() { words = 0; //default words } /** * Changes the number of document words. * pre: none * post: Words has been changed. */ public void setWords(int numWords) { words = numWords; } /** * Calculates the number of pages....
Considering the TCP ACK Scan that we have seen in class, write a signature for an...
Considering the TCP ACK Scan that we have seen in class, write a signature for an Intrusion Detection System (IDS) that is able to catch it.
In this program, you are modifying given code so that the class is object-oriented. 2. Write...
In this program, you are modifying given code so that the class is object-oriented. 2. Write a Java class called CityDistancesOO in a class file called CityDistancesOO.java.    3. Your class will still make use of two text files. a. The first text file contains the names of cities with the first line of the file specifying how many city names are contained within the file.    b. The second text file contains the distances between the cities in the...
Write a code using c# Maximum Sub Array.
Write a code using c# Maximum Sub Array.
Write a code in c++ using dynamic array of structure and dynamic array list. Make a...
Write a code in c++ using dynamic array of structure and dynamic array list. Make a dummy list for a company which stores following information about its customers. Customer ID Customer Name Gender Total items purchased Item category 20% discount in percentage of total purchase amount. Use dynamic array to save at least 20 items by dividing them into 3 different categories. Make a dummy list of items that company sells by dividing them into two categorizes. Items has following...
Sort a string array by frequency given an array of string write a function that will...
Sort a string array by frequency given an array of string write a function that will return an array that is sorted by frequency example {"hello","hola","hello","hello","ciao","hola","hola","hola"} returned array should be {"hola","hello","ciao"}
Write a Java class called Employee (Parts of the code is given below), which has three...
Write a Java class called Employee (Parts of the code is given below), which has three private fields firstName (String), lastName (String) and yearsEmployed (double). Implement accessor/mutator methods for the private fields and toString() method, that returns a String representation, which contains employee name and years she was employed. public class Employee { private String firstName; ... } Write a client program called EmployeeClient that creates an instance of Employee class, sets values for the fields (name: John Doe, yearsEmployed:...
Write code that would allocate the space for an array of 20 integers. It will be...
Write code that would allocate the space for an array of 20 integers. It will be pointed to by a variable named "junk" Write code that puts “file did not open” into an error stream.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT