Question

In: Computer Science

Define for better understanding & give example. do not copy paste google answers of definitions. 1....

Define for better understanding & give example. do not copy paste google answers of definitions.

1. What is the difference between a set and a list?

2. What is inheritance ?

3. Difference between a class and an object? How do classes relate to objecst?

4. How would you create an SQL table which makes use of customers information table and vehicle ownership over time? 5. How would you create a function that checks if a string is a palindrome?

Solutions

Expert Solution

1. Difference Between a Set and a List.

Set List
1.Definition Set is basically collection of elements, which is unordered list of elements, as order doesn't matters in a set. List is an ordered sequence of elements.
2.Duplicate Elements Set also doesn't allows duplicate elements. For eg If you write {1,2,3,4,4,5} It is equivalent to {1,2,3,4,5}. Duplicate elements will be counted for once only. List allows duplicate elements completely,It is allowed to store multiple duplicate elements in List.
3.Null Elements Set just allow one null element as there is no duplicate permitted while in Map you can have null values and at most one null key. The list allows null elements and you can have many null objects in a List because it also allowed duplicates.
4. Implementation & Uses Set interface are implemented in HashSet, LinkedHashSet, and TreeSet List is implemented in ArrayList, LinkedList, and Vector class.
5. Order of elements

Set doesn't maintains any order of elements

{1,2,3,4,5} is equivalent to {2,3,4,5,1}

List maintains order of elements as per they are inserted, hence we can easily access/insert/delete element easily at specified index.
6. When to use If the requirement is to have only unique values then Set is your best bet as any implementation of Set maintains unique values only. If there is a need to maintain the insertion order irrespective of the duplicity then List is a best option

Examples: I have shown implementation in JAVA.

Set: In this example i have taken a set ABC which have 5 values, Then I have shown implementation of this set Interface in Hash Set first and then in tree set, where hash set creates hashing and Treeset is ised to sort the elements.

import java.util.*;
import java.lang.*;
import java.io.*;

class Set
{
   public static void main (String[] args) throws java.lang.Exception
   {
       int ABC[] = {1,2,3,4,5};
Set<Integer> Hash = new HashSet<Integer>();

for(int i = 0; i<4; i++)
{
hset.add(ABC[i]);
}
System.out.println(Hash);

TreeSet<Integer> Tree = new TreeSet<Integer>(Hash);
System.out.println("The sorted list is:");
System.out.println(Tree);
   }
}


List: It is very simple to implement, I have shown Array list implementation below

import java.util.List;
import java.util.ArrayList;
import java.util.LinkedList;
public class List1 
{
   public static void main(String[] args)
 {
      List<String> ABC = new ArrayList<String>();
      ABC.add("Delhi");
      ABC.add("Mumbai");
      ABC.add("Bangalore");
      System.out.println("List Elements: ");
      System.out.print(ABC);
   }
}

2. Inheritance

First let's discuss types of classes:

Child Class:
The class that extends the features of another class is known as child class, sub class or derived class.

Parent Class:
The class whose properties and functionalities are used(inherited) by another class is known as parent class, super class or Base class.

In Object oriented concepts, Inheritance is a Mechanism in which one object can acquire all the properties and behaviors of a parent object, by using extends keyword

Syntax of Inheritance:

class Subclass-name extends Superclass-name  

{  

   //methods and fields  

}  

Thsi extends keyword shows that you are making a new child class that derives features from an existing Parent class. The meaning of "extends" is to increase the functionality, The most basic use of inheritance is to increase the code reusability and extend features in a class.

The biggest advantage of Inheritance is that only- the code which is already written in base (Parent) class that need not be rewritten in the child class, You can simply extend the child class from parent class it will itself gain the features.

There are various real life Inheritance examples like

  • Automobiles and Pulled Vehicles are subclasses of Vehicles.
  • Vehicles are the base class or superclass of Automobiles and pulled Vehicles.
  • Car and Bus are sub-classes or derived classes of Automobiles.
  • Automobiles are the base class or superclass of Car and Bus.

3.) Difference between class and object

No. Object Class
1) It is an instance of a class. We create instances of a class and we call them as objects of that class Class is a blueprint or template from which objects are created.
2) It is like a real world entity such as pen, pencil etc. Class is a group of similar objects, Many similar instances form a class.
3) Object is a physical entity. Class is a logical entity.
4) Object is created through new keyword mainly e.g.
Customer s1=new Customer);
Class is declared using class keyword e.g.
class Flower{}
5) Object is created many times as per requirement, we can create as many objects we need of a particular class. Class is declared only once, Then we create multiple objects from it.
6) There are many ways to create object in java such as new keyword, newInstance() method, clone() method, etc.

There is only one way to define class in java using class keyword.

class classname

{

}

4.) SQL TABLE for Customer Information

Since we have to maintain customer information table, we need some basic attributes like Customer Name, Customer Number can be considered as the primary key and rest we can have additional information like Age, Sex, Mobile No. Address etc.

And next to keep a record of ownership time we have to record date of purchase so that we can find the onwership time by calculating date difference from today's date.

CREATE TABLE Customer(Cust_No integer PRIMARY KEY, Cust_Name text , Cust_Age integer, Cust_Sex text , Purchase_Date DATE);

We have taken Customer Number as Integer which is Primary Key, Cust Name which is a text field, Customer Age which is integer and Cust Sex which is also text, and lastly Purchase_Date which is of DATE Data type. You can also use varchar or char instead of text.

Syantax for Date difference function:

DATEDIFF(interval, date1, date2)

5.) Palindrome - I have given a C function here.

#include <stdio.h>
#include <string.h>

int main()
{

char string[1000]; //Define a char array so as to take string input

printf("Enter the string: ");
gets(string);
  

if(checkpalindrome(string))
    printf("String is palindrome"); //If it gets 1 from function then palindrome
else
printf("String is not palindrome"); ////If it gets 0 from function then Not palindrome
}

int checkpalindrome(char *string) //This function checks if it is palindrome or not, It takes character pointer of the string array
{
int i,a=0,n;   
n=strlen(string); //strlen function returns length of the string
   for(i=0;i<n/2;i++)
{
   if(string[i]==string[n-i-1]) //We check from reverse and compare each character if it matches we increment a
   a++;
    }
    if(a==i) //If after incrementing a it becomes equals to i that means each and every element is same from reverse
return 1; //If it returns 1 that means yes it is palindrome
else
return 0; //If returns 0 that means not palindrome
}

Output:

Note: I have mentioned comments in code and examples wherever necessary, Copy this code, save it with .c extension and run, It will work fine,

Hope it Helps..!!

I have tried to answer all 5 questions as nicely i could.


Related Solutions

Provide definitions for each of the folowing dont copy paste from google concurrency group movement NEMA...
Provide definitions for each of the folowing dont copy paste from google concurrency group movement NEMA phase numbering overlap phase ring ring barrier diagram
Define and give unique example for the following term: (Please do not copy from textbook it...
Define and give unique example for the following term: (Please do not copy from textbook it needs to be in your own words) Opportunity Cost Positive and normative statments Production possibilities frontier Productive efficiency
Explain the Need and importance of understanding International Political Economy. (Please do not copy and paste...
Explain the Need and importance of understanding International Political Economy. (Please do not copy and paste from the internet. Would be grateful if you could also include a couple of in-text citations from a journal or a book)
Define and give examples for better understanding 1. What are all the data types of java...
Define and give examples for better understanding 1. What are all the data types of java script? 2. What are all the SQL joins? 3. What is polymorphism in your own words? 4. What is a constructor?
This a public health question, please be detail in the reponse and dont copy/ paste google...
This a public health question, please be detail in the reponse and dont copy/ paste google responses What are the purposes of an evaluation effort? 2. Who benefits from an evaluation? Who is it for? 3. How might the evaluation differ based on stakeholder needs? How might the evaluation differ based on the proposed “use”? 4. How might the format of an evaluation affect community members? 5. What are the most important outcomes to measure in an evaluation?
explain compounding and discounting ..please no copy and paste, I can google that myself.
explain compounding and discounting ..please no copy and paste, I can google that myself.
Explain Bringing Human Resources Back into Strategic Planning Copy and Paste. Do not use previous answers.
Explain Bringing Human Resources Back into Strategic Planning Copy and Paste. Do not use previous answers.
explain in brief. no google copy and paste is required Digital detectors of X-rays in radiology...
explain in brief. no google copy and paste is required Digital detectors of X-rays in radiology Image quality in radiology Radiographic equipment (construction, main parts of equipment) and applications in clinical practice
R studio questions Write up your answers and paste the R code Copy and paste all...
R studio questions Write up your answers and paste the R code Copy and paste all plots generated. First create a sample drawn from a normal random variable. R has many distributions for which you can get probabilities and draw random numbers. We are going to use the normal. Go to help in R and type in rnorm. You will see a write up for functions associated with the normal distribution. dnorm is the density; pnorm is the probability distribution...
Please don't copy and paste answers from several sources. Write in your own words! Do you...
Please don't copy and paste answers from several sources. Write in your own words! Do you feel teams help or hurt creativity? Give specific examples. How should you handle a freeloader (someone not willing to do their share of the work) on a team where you are a member? Be specific. For an organization where you have worked, list three ways the organization helped you do your job. (This can be any type of organization if you have never worked.)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT