Posts

Showing posts with the label Source Code

Monte Carlo method to compute an approximation to π - CS 222 Program 2

Image
The program Your program will use a Monte Carlo method to compute an approximation to π. In a Monte Carlo computation a sequence of random values is generated and run through a model of a system. For our simple program the random values will be used to plot points as described below. You will generate a random sequence of points in the unit square in the coordinate plane and see how many fall inside the unit circle. In the following figure seven of ten randomly generated points fell inside the circle. 7/10 = 0.7 then gives a (very) rough approximation to the ratio: (area of circle segment)/(area of unit square) = π/4 Thus 0.7 is an approximation to π/4 so 4 × 0.7 = 2.8 is an approximation to π. Larger numbers of points will give better approximations. The program will use the above method to compute approxiations to π four times: using 10 points, 100 points, 1000 points, and 1000000 points. It will report the results of each of the computations with the number of points used, the compu...

BMI Metric source code in Liberty basics

Image
Body Mass Index Implementation First Screen Result Screen [ setup . main . Window ] '-----Begin code for #main WindowWidth = 365 WindowHeight = 220 UpperLeftX = int ( ( DisplayWidth - WindowWidth ) / 2 ) UpperLeftY = int ( ( DisplayHeight - WindowHeight ) / 2 ) '-----Begin GUI objects code statictext # main . statictext1 , "Enter Your Name" , 15 , 52 , 102 , 20 TextboxColor$ = "white" textbox # main . name , 135 , 47 , 100 , 25 button # main . button3 , "Proceed Now!" , [ BMI ] , UL , 15 , 92 , 95 , 25 '-----End GUI objects code open "untitled" for window as # main print # main , "font ms_sans_serif 10" wait [ BMI ] print # main . name , "!contents? name$" m1$ = "Welcome " + name $ + " to BMI Calculator" notice m1$ close # main '-----Begin code for #1 WindowWidth = 550 WindowHei...

CSMA/CD Implementation in C++

Image
Background CSMACD stands for carrier Sense, Multiple Access, and Collision Detection. We use a simplified version where the protocol:  If a station is ready to send a message (according to a random number R1) it senses the carrier. The station can only sense the part of the carrier that is directly attached to the station. a. If the carrier is free the station start transmission of a random message (length R2 ticks) Otherwise, the station delays sensing the carrier by a random (R3) number of ticks. If a station detects collision it stops the current transmission and delays sensing the carrier by a random number of ticks (R4) As long as a station is attempting to send a message it does not generate new messages. The number and location of stations on the carrier, the carrier length in meters (R5), and the propagation time of a message through the carrier in tics/meter are given.  We assume that a tic is a time that it takes for a message to propagate a distance of one meter in ...

Highest GPA Calculator Using Stack and File Handling in C++

Highest GPA Calculator Introduction We want to write a C++ program that reads a data file consisting of each student’s GPA followed by the student’s name. The program then prints the highest GPA and the names of all of the students who received that GPA. The program scans the input file only once. Moreover, we assume that there is a maximum of 100 students in the class. Use a stack to write your code. Here is an example of the input/output operations. Input  The program reads an input file consisting of each student’s GPA, followed by the student’s name. Sample data is: 3.5 Bill 3.6 John 2.7 Lisa 3.9 Kathy 3.4 Jason 3.9 David 3.4 Jack Output  The highest GPA and all of the names associated with the highest GPA. For example, for the above data, the highest GPA is 3.9, and the students with that GPA are Kathy and David. CONTACT DETAILS For any other questions or other tasks please feel free to contact me via email: mhassnainjamil@gmail.com via WhatsApp: +92-324-7042178 via skype...

Very easy way to open and read any type of file in Java

Image
How I can open a file in Java, read it and print its content 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 import java.io.File ; import java.io.FileInputStream ; import java.io.BufferedInputStream ; import java.io.DataInputStream ; import javax.swing.JOptionPane ; public class FileReader { public static void main ( String [] args ) { File file = new File ( "C:\\Tasks.txt" ); try { BufferedInputStream bis ; DataInputStream dis ; try ( FileInputStream fis = new FileInputStream ( file )) { bis = new BufferedInputStream ( fis ); dis = new DataInputStream ( bis ); while ( dis . available () != 0 ) System . out . println ( dis . readLine ()); } bis . close (); dis . close (); } catch ( Exception e ){ JOptionPane . showMessageDialog ( null...

Heap Creation and Sorting in C++ with checks free source code

Image
Heap Data Structure Creation 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 # include < iostream > using namespace std ; void heapCreation ( int item ); void heapSorting (); int i , p , n = 0 ; const int heapSize = 100 ; int arr [ heapSize ]; void heapCreation ( int item ){ if ( n >= heapSize ){ cout << "Heap is saturated: Insertion is void" << endl ; return ; } else { int temp ; cout << item << endl ; n = n + 1 ; arr [ n ]= item ; i = n ; p = i / 2 ; while ( p > 0 && arr [ p ]< arr [ i ]){ temp = arr [ i ]; arr [ i ]= arr [ p ]; arr [ p ]= temp ; i = p ; p = p / 2 ; } } } void heapSorting (){ int item , j , temp , lchild , rchild , i = n ; cout <...

MS SQL Server Java Connection Source Code

Image
Source Code After running this code you will see the successful message as shown blow. Click here for Detailed Tutorial 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 import java.sql.Connection ; import java.sql.DriverManager ; import javax.swing.JOptionPane ; /** * * @author cs2it.blogspot.com */ public class RealEstate { /** * @param args the command line arguments */ public static void main ( String [] args ) { try { final String DATABASE_URL = "jdbc:sqlserver://localhost:1433;databaseNam=YourDatabaseName" ; Connection connection = DriverManager . getConnection ( DATABASE_URL , "YourUserName" , "Password" ); JOptionPane . showMessageDialog ( null , "Connection to database is Successful" ); } catch ( Exception e ){ JOptionPane . showMessageDialog ( null , e ); ...