Artificial Intelligence Programs For Programming in Python

Traveling Salesman Problem ( TSP )] Artificial Intelligence Programs For Master Leval Programming in Python from iter tools import permutations def distance(point1, point2): >>> distance([3,4],[0,0]) 5.0 >>> distance([3,6],[10,6]) 7.0 return ((point1[0] - point2[0])**2 + (point1[1] - point2[1])**2) ** 0.5 def total_distance(points): all the points are in the given order. >>> total_distance([[1,2],[4,6]]) 5.0 >>> total_distance([[3,6],[7,6],[12,6]]) 9.0 return sum([distance(point, points[index + 1]) for index, point in enumerate(points[:-1])]) def travelling_salesman(points, start=None): Time complexity is O(N!), so never use on long lists. >>> travelling_salesman([[0,0],[10,0],[6,0]]) ([0, 0], [6, 0], [10, 0]) >>> travelling_salesman([[0,0],[6,0],[2,3],[3,7],[0.5,9],[3,5],[9,1]]) ([0, 0], [6, 0], [9, 1], [2, 3], [3, 5], [3, 7], [0.5, 9]) """ if the start is None: start = points[0] return min([perm for perm ...

Java Programs learn with the All examples With Execute

Java Program List


Solution :
 03. Write a menu driven program to find given number is :
    i)      Pelindrome or not
    ii)     Odd or Even
    iii)    Armstrong or not
     iv)     Prime or not.


import java.util.Scanner;
class choose
{
public static void main(String Args[])
{
int a,n,f;

System.out.println("Menu");
System.out.println("\n1. Polindrome \n2. Even or Odd \n3. Armstrong or not \n4. Prime or not\n5. Exit");
System.out.print("\nEnter the Choosen No. : ");
Scanner ob=new Scanner(System.in);
a=ob.nextInt();

switch(a)
{
case 1:
System.out.print("\n\nEnter the 3digit no. : ");
n=ob.nextInt();
int r,sum=0,temp; 
  temp=n; 
  while(n>0){ 
   r=n%10;  //getting remainder
   sum=(sum*10)+r; 
   n=n/10; 
  } 
  if(temp==sum) 
   System.out.println("palindrome number "); 
  else 
   System.out.println("not palindrome");
break;
case 2:
System.out.print("\n\nEnter Digit. : ");
n=ob.nextInt();
  if ( n % 2 == 0 )
System.out.println("You entered an even number.");
  else
System.out.println("You entered an odd number.");
break;
case 3:
int c=0,temp1;
System.out.print("\n\nEnter Digit. : ");
n=ob.nextInt();
temp1=n;
while(n>0)
{
a=n%10;
n=n/10;
c=c+(a*a*a);
}
if(temp1==c)
System.out.println("armstrong number"); 
else
System.out.println("Not armstrong number");
break;
case 4:
int i,m=0,flag=0; 
System.out.print("\n\nEnter Digit. : ");
n=ob.nextInt();
m=n/2; 
for(i=2;i<=m;i++){ 
   if(n%i==0){ 
System.out.println("Number is not prime"); 
flag=1; 
break; 

  } 
  if(flag==0) 
  System.out.println("Number is prime"); 
break;
case 5:
break;
default:
System.out.println("Enter the correct no.");
break;
}
}
}


4. Write a program to accept two numbers from user and it should be menu driven program and perform all the arithemetic operations on that two numbers.

import java.util.Scanner;
class calculator
{
public static void main(String Args[])
{
int a;
double n1,n2;
double f;
System.out.println("*********************Calculator*********************");
System.out.println("\n1. Addition\n2. Subtraction\n3. Division\n4. Multiplication\n5. Exit");
System.out.print("\nEnter the Choosen No. : ");
Scanner ob=new Scanner(System.in);
a=ob.nextInt();

switch(a)
{
case 1:
System.out.print("\n\nEnter the first no. : ");
n1=ob.nextInt();
System.out.print("Enter the second no. : ");
n2=ob.nextInt();
f=n1+n2;
System.out.println("Answer is : "+f);
break;
case 2:
System.out.print("\n\nEnter the first no. : ");
n1=ob.nextInt();
System.out.print("Enter the second no. : ");
n2=ob.nextInt();
f=n1-n2;
System.out.print("Answer is : "+f);
break;
case 4:
System.out.print("\n\nEnter the first no. : ");
n1=ob.nextInt();
System.out.print("Enter the second no. : ");
n2=ob.nextInt();
f=n1*n2;
System.out.print("Answer Mul is : "+f);
break;
case 3:
System.out.print("\n\nEnter the first no. : ");
n1=ob.nextInt();
System.out.print("Enter the second no. : ");
n2=ob.nextInt();
f=n1/n2;
System.out.print("Answer Div is : "+f);
break;
case 5:
break;
default:
System.out.println("Enter the correct no.");
break;
}
}
}

7.Write a program to make use of interface in java.
interface i1{
void m5();

}
interface i2 extends i1
{
void m6();

}
interface i3 extends i2{

final int z =10;
void m7();
}
abstract class A{

A(){
System.out.println("A construcor");

}
void m1(){
System.out.println("Add Method1");
}
void m2(){
System.out.println("Add Method2");
}

}
class B extends A {

B(){
System.out.println("B construcor");

}
void m3(){
super.m1();
System.out.println("Add Method3");
}
void m4(){
super.m2();
System.out.println("Add Method4");
}

}

class C extends B implements i3 {

C(){
super();
System.out.println("C construcor");

}
public void m5(){
System.out.println("Add Method5");
}
public void m6(){
super.m3();
System.out.println("Add Method6");
}
public void m7(){
super.m4();
System.out.println("Add Method7 "+z);
}
public static void main(String arg[])
{
C c=new C();
c.m6();
c.m7();

}

}

 9. Create a simple java program to make use of String class and it's methods.

class strex
{
public static void main(String arg[]) {
String s1="Sachin Tendulkar";
String s2="INDIA";
String s3="";
String s4=new String("Narendra Modi");
String s5=new String();
String s6="Sachin Tendulkar";
String s7="india";
String s8=new String("Narendra Modi");

System.out.println("length of s1 : "+s1.length());

System.out.println("isempty() s1 : "+s1.isEmpty());

System.out.println("length of s2 : "+s2.length());

System.out.println("isempty() s2 : "+s2.isEmpty());

System.out.println("length of s3 : "+s3.length());
System.out.println("isempty() s3 : "+s3.isEmpty());

System.out.println("length of s5 : "+s5.length());
System.out.println("isempty() s5 : "+s5.isEmpty());

System.out.println("== Operator Compare : "+(s1==s6));
System.out.println("equals() s1 : "+s1.equals(s6));

System.out.println("== Operator Compare : "+(s3==s5));
System.out.println("equals() s3==s5 : "+s3.equals(s5));

System.out.println(" compareTo s1-s6 : "+(s1.compareTo(s6)));
System.out.println("compareToIgnoreCase() s1-s6 : "+(s1.compareToIgnoreCase(s6)));

System.out.println(" compareTo : s2-s7 "+(s2.compareTo(s7)));
System.out.println("compareToIgnoreCase() s2-s7 : "+(s2.compareToIgnoreCase(s7)));

System.out.println(" compareTo : s2-s1 "+(s2.compareTo(s1)));
System.out.println("compareToIgnoreCase() s2-s1 : "+(s2.compareToIgnoreCase(s1)));

System.out.println(" compareTo : s7-s2 "+(s7.compareTo(s2)));

System.out.println("compareToIgnoreCase() s7-s2 : "+(s7.compareToIgnoreCase(s2)));

System.out.println(" compareTo s4-s8 : "+(s4.compareTo(s8)));
System.out.println("compareToIgnoreCase() s4-s8 : "+(s4.compareToIgnoreCase(s8)));

}


}



10.Create a simple java program to make use of StringBuffer class and it's methods.

class Stringbufferex1

{  

public static void main(String args[])



/*StringBuffer s1="Hello "; /StringBuffer Cant support Literal it support only object(Stringbufferex1.java:7: error: incompatible types: String cannot be converted to
 StringBuffer)*/ 
StringBuffer s1=new StringBuffer("Hello");  //print Hello
StringBuffer s2=new StringBuffer("Marwadi ");  
StringBuffer s3=new StringBuffer("Educetion");  
StringBuffer s4=new StringBuffer("Collage");  
 

System.out.println(s1);  

// The append() method concatenates the given argument with this string.
System.out.println(s1.append(" Students"));  //Hello Students

// The insert() method inserts the given string with this string at the given position.
System.out.println(s2.insert(8,"Univercity"));  //Marwadi Univercity

// The replace() method replaces the given string from the specified beginIndex and endIndex
System.out.println(s3.replace(3,5,"ca"));  //Education

// The delete() method of StringBuffer class deletes the string from the specified beginIndex to endIndex
System.out.println(s4.delete(3,5));  //Colge

// The reverse() method of StringBuilder class reverses the current string.
System.out.println(s2.reverse());  //yticrevinU idawraM
System.out.println(s2.capacity());  //24

}

}


 11. Write a java program to make use of Math class and static import.



import static java.lang.Math.*;

public class math
{
public static void main(String arg[])
{
System.out.println("abs Function nagative : "+abs(-100));//  convert nagative to Positive
System.out.println("abs Function Positive : "+abs(1.50));

System.out.println("ceil Function nagative : "+ceil(-0.6)); //Upper 5.45 = 5 or 5.55 = 6
System.out.println("ceil Function Positive : "+ceil(0.6));

System.out.println("floor Function nagative : "+floor(-2.22));//bottom 5.45 = 5
System.out.println("floor Function Positive : "+floor(2.22));

System.out.println("sqrt only positive support Function : "+sqrt(4));//root of square

System.out.println("pow Function nagative : "+pow(-2,-5)); //-2 ^ -5
System.out.println("pow Function Positive : "+pow(2,5));//2^5

System.out.println("max Function nagative : "+max(-12,5));//find max
System.out.println("max Function Positive : "+max(2,5));

System.out.println("min Function nagative : "+min(-2,-5));//find min
System.out.println("min Function Positive : "+min(2,5));

System.out.println("Ceil Function : "+round(4.666));
System.out.println("Ceil Function : "+random());



}


}


12.Write a java program to make use of exception handling.

class ex6{

public static void main(String arg[])
{
int a=0,b=0;
int c;
try{
c  = a/b;
System.out.println(c);
}
catch(ArithmeticException e)
{

System.out.println("Divid arithmetic by "+e);

}
catch(Exception e)
{

System.out.println("Divid by "+e);

}

}



}


Download WordPress Book Easily Learn :Click Here

Comments

Popular posts from this blog

MongoDB Exercises Practice Solution Exercise First

Simple Basic Commands For MongoDB

Artificial Intelligence Programs For Programming in Python