Tutorialspoint

Programming Examples

Java Signature Method

public class MethodSignature {
   public int add(int a, int b){
      int c = a+b;
      return c;
   }
   public static void main(String args[]){
      MethodSignature obj = new MethodSignature();
      int result = obj.add(56, 34);
      System.out.println(result);
   }
}

Mean

c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <pthread.h>

int req_threads;
int float_size = 12;
float floats[] = { 1.5, 5.2, 7.8, 1.0, 1.7, 2.4, 4.5, 3.8, 2.7, 2.2, 8.7, 3.7 };
float means[4] = {0,0,0,0};
float total_mean;
long i,j,k;

void *getMean(void* rank)
{
	long my_rank = (long) rank;
	for (int i = my_rank * (float_size / 4); i < (my_rank + 1) * (float_size / 4); i++) {
		printf("%f\n", floats[i]);
		means[my_rank] += floats[i];
	}
	printf("Mean of Thread ID: %i, = %f\n", my_rank, means[my_rank] / 3 );
	means[my_rank] = means[my_rank] / 3;
}

int main() {
	req_threads = ceil(float_size / 3);
	pthread_t threads[req_threads];
	for (i = 0; i < req_threads; i++) {
		pthread_create(&threads[i], NULL, getMean, (void *) i);
	}
	
	for (i=0;i<req_threads;i++) {
		pthread_join(threads[i], NULL);
	}
	
	for (i=0;i<req_threads;i++) {
		total_mean += means[i];
	}
	total_mean = total_mean / 4;

	printf("Total mean = %f\n", total_mean);

	return 0;
}

Java Overloading Method

public class OverloadingExample {
   public void display(){
      System.out.println("Display method");
   }
   public void display(int a){
      System.out.println("Display method "+a);
   }
   public static void main(String args[]){
      OverloadingExample obj = new OverloadingExample();
      obj.display();
      obj.display(20);
   }
}

Java Overriding Method

class SuperClass{
   public static void sample(){
      System.out.println("Method of the super class");
   }
}
public class RuntimePolymorphism extends SuperClass {
   public static void sample(){
      System.out.println("Method of the sub class");
   }
   public static void main(String args[]){
      SuperClass obj1 = new RuntimePolymorphism();
      RuntimePolymorphism obj2 = new RuntimePolymorphism();
      
      obj1.sample();
      obj2.sample();
   }
}

tabela praštevil

# Dela tabelo prastevil tako, da kandidate preverja
# s prejsnjimi prastevili v tabeli, Vinc okt.2018
import time
a=time.clock()  
prast=[2] #seznam prastevil, prvo prast[0]=2, nato jih dodaja
for n in range(3,1010):
    p=1    #zastavica je 1 ce n je prastevilo, in 0 sicer
    i=0
    while (prast[i]*prast[i]<=n):
        ostanek=n%prast[i]
        if ostanek==0:
            p=0
            break
        i=i+1
    if (p==1):
        prast.append(n)
b=time.clock()
print (prast)
print("Racunal sem ",b-a,"s.")

4.5

c

#include <stdio.h>

void main()
{
    int row,x=1,p,i,j;
    printf("Input number of rows: ");
    scanf("%d",&row);
    for(i=0;i<row;i++)
    {
        for(p=1;p<=row-i;p++)
        printf("  ");
        for(j=0;j<=i;j++)
        {
            if (j==0||i==0)
                x=1;
            else
               x=x*(i-j+1)/j;
               if(i==0)
               {
                    printf("\n\t" " " " ");
               }
                   
            printf("% 4d",x);
        }
        printf("\n");
    }
}

Execute Python Online

tup1 = ( 100, 36.5, 17.8);
tup2 = ("xyz", "abc", "pqr");

tup3 = tup1 + tup2;
print " tup3";

TpDartyFinalise

public class DartyMain{

     public static void main(String []args){
        System.out.println("Programme Darty");
        
        // Creation des objets
        // Creation d'un objetDirecteur
        Directeur dir = new Directeur("Duchemin");
        
        // Creation de 5 objets Vendeur
        Vendeur v1 = new Vendeur("Martin",1, 1000.0f);
        Vendeur v2 = new Vendeur("Dupuis",2, 1500.0f);
        Vendeur v3 = new Vendeur("Duchnoc",3, 900.0f);
        Vendeur v4 = new Vendeur("Durand",4, 1600.0f);
        Vendeur v5 = new Vendeur("Dupont",5, 2000.0f);
        
        // Creation des objets Machine
        Ordinateur o = new Ordinateur("HP", 1500.0f);
        Imprimante imp = new Imprimante("Epson",149.0f);
        Aspirateur asp = new Aspirateur("Dyson", 500.0f);
        MachineALaver mal = new MachineALaver("Vedette",400.0f);
        
        // Liste des actions
        v1.vendreMachine(o);
        v1.vendreMachine(imp);
        v2.vendreMachine(asp);
        v3.vendreMachine(mal);
        
        float total;
        total = dir.demanderSommeTotaleVente(v1);
        System.out.println("Total vente "+total);
        total = dir.demanderSommeTotaleVente(v3);
        System.out.println("Total vente "+total);
        total = dir.demanderSommeTotaleVente(v4);
        System.out.println("Total vente "+total);
        
        
        v2.testerMachine(mal);
     }
}

// Definition de la classe Personne (mere)
class Personne {
    
    // Attributs
    private String nom;
    
    public Personne(String name) {
        nom = name;
    }
}

class Directeur extends Personne {

    // Constructeur
    public Directeur(String nom) {
        
        // Appeller explicitement le constructeur 
        // de la mere
        super(nom);
    }
    
    public float demanderSommeTotaleVente(Vendeur v) {
        
        // Demander a l'objet v de lui fournir
        // la somme de ses ventes
        return v.donnerSommeTotaleVente();
    }
    
    public void augmenterSalaireVendeur(Vendeur v) {
        
        float salaire;
        float NouveauSalaire;
        
        // Demander salaire du vendeur
        salaire = v.donnerSalaire();
        
        //salaire = salaire*(1+10/100);
        NouveauSalaire = salaire*(1+10/100);
        
        // Donner le nouveau salaire au vendeur
        //v.recevoirNouveauSalaire(salaire);
        v.recevoirNouveauSalaire(NouveauSalaire);
    }
}

// Definition de la classe Vendeur
class Vendeur extends Personne {
    
    // Attributs
    private int numVendeur;
    private float sommeTotaleVentes;
    private float salaire;
    
    // Constructeur
    public Vendeur(String nom, int numVend, float sal) {
        super(nom);
        
        numVendeur = numVend;
        salaire = sal;
        sommeTotaleVentes = 0;
    }
    
    // Methodes
    public float donnerSalaire() {
        return salaire;
    }
    
    public float donnerSommeTotaleVente() {
        return sommeTotaleVentes;
    }
    
    public void recevoirNouveauSalaire(float newSalaire) {
        salaire = newSalaire;
    }
    
    public void vendreMachine(Machine m) {
        
        float prixMachine;
        
        prixMachine = m.donnerPrix();
        
        sommeTotaleVentes = sommeTotaleVentes + prixMachine;
        
        System.out.println("j'ai vendu un(e) "+m.donnerNomMachine() + " d'une valeur de "+prixMachine + " Euros");
    }
    
    public void testerMachine(Machine m) {
        m.fonctionner();
    }
}

// Definition de la classe Machine (mere)
class Machine {
    
    // Attributs
    private String nom;
    private float prix;
    
    // Methodes
    
    // Constructeur
    public Machine(String name, float price) {
        nom = name;
        prix = price;
    }
    
    public float donnerPrix() {
        return prix;
    }
    
    public String donnerNomMachine() {
        return nom;
    }
    
    public void fonctionner() {
        System.out.println("je fonctionne ....");
    }
}

// Definition de la classe Ordinateur
class Ordinateur extends Machine {
    
    
    // Constructeur
    public Ordinateur(String nom, float prix) {
        
        // Appel explicite du constructeur de Machine
        super(nom, prix);
    }
    
    public void fonctionner() {
        System.out.println("le système d’exploitation est charge ..OK.. ");
    }
}

// Definition de la classe Imprimante
class Imprimante extends Machine {
    
    
    // Constructeur
    public Imprimante(String nom, float prix) {
        
        // Appel explicite du constructeur de Machine
        super(nom, prix);
    }
    
    public void fonctionner() {
        System.out.println("...j'imprime...");
    }
}

// Definition de la classe MachineALaver
class MachineALaver extends Machine {
    
    
    // Constructeur
    public MachineALaver(String nom, float prix) {
        
        // Appel explicite du constructeur de Machine
        super(nom, prix);
    }
    
    public void fonctionner() {
        System.out.println("...je tourne....");
    }
}

// Definition de la classe Aspirateur
class Aspirateur extends Machine {
    
    
    // Constructeur
    public Aspirateur(String nom, float prix) {
        
        // Appel explicite du constructeur de Machine
        super(nom, prix);
    }
    
    public void fonctionner() {
        System.out.println("...j'aspire a ...");
    }
}

Project_Code_1 - Python 3.6.2

#!/usr/bin/python

list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5, 6, 7 ];

EmailList = "Hi ted,How are you. Please respond to earlier mail, Thanks."

Elist1 = EmailList.split('.')
print ('Details: ',Elist1)
print ('\n')

Elist2 = " $ ".join(Elist1)
print ('Details: ',Elist2)
print ('\n')

Elist3 = EmailList.split()
Elist3[4]='Yar'   ## Updating the value in list
print (Elist3)
print (' '.join( Elist3[-6:-4] ))
print('\n')
pointer = ['how are you','sir']
print (pointer * 3)
print (' $ '.join(pointer))
print (' $ '.join(pointer*3))
print('\n')
print('\n')

Mlist="naveen naveen bose bose The assignment statement theSum = 0 creates a variable called gowtham theSum and lets it hold the reference to the data object 0 (see Figure 3). In general, the right-hand side of the assignment statement is evaluated and a reference to the resulting data object is “assigned” to the name on the gowtham left-hand side. At this gowtham point in our example, the type of the variable is integer as that is the type of the data currently being referred to by gowtham theSum. If the type of the data changes (see Figure 4), as shown above with the boolean value True, so does the type of the variable (theSum is gowtham now of the type boolean). The assignment statement changes the reference being held by the variable. gowtham This is a dynamic characteristic of Python. The same variable can refer to many different types of data."

keyword = ['IF']
count=0
for i in Mlist.split():
    if i.upper() in keyword:
        count+=1
if (count==1):
    print ('# Expected Word Available')        
else:
    print ('# Not Available')        

print ('Count of List1: ',len(Mlist.split()))
print ('Count of List1: ',len( list(set(Mlist.split()))))

Mlisto=Mlist.split()
Mlistd=list(set(Mlist.split()))
print('\n')
print('Word Count Details :')
for i in Mlistd:
    count=0
    for j in Mlisto:
        if (j==i):
            count+=1
    if (count > 3 and count < 9  ):        
        print (i,' : ',count)
        
        
        
        
        
        
        
        
        
        

factory

public class ErshMaker{
    
    interface IBeer{
        String getName();
        int getGradus();
    } 
    
    interface IVodochka{
        String getName();
    }
    
    interface IBuhloFactory{
        IBeer createPivko();
        IVodochka createVodochka();
    }
    
    
    class PivoTema implements IBeer{
        public String getName(){ return "Pivo Tema"; }
        
        public int getGradus(){ return 6; }
    }
    
    class VodkaStolichnaya implements IVodochka{
        public String getName() { return "Stolichnaya"; }
    }
    
    class BitchBuhloFactory implements IBuhloFactory{
        public IBeer createPivko() { return new PivoTema(); }
        public IVodochka createVodochka() { return new VodkaStolichnaya(); }
    }
    
     public void Main(){
         IBuhloFactory buhloFactory = new BitchBuhloFactory();
        
        IBeer beer = buhloFactory.createPivko();
        IVodochka vodochka = buhloFactory.createVodochka();
        
        System.out.println(beer.getName() + " " + beer.getGradus());
        System.out.println(vodochka.getName());
     }
     
     
     public static void main(String []args){
        ErshMaker program = new ErshMaker();
        program.Main();
     }
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.