Saturday, 28 December 2013
Monday, 23 December 2013
C# Gettter/Setter : GTK style
C# properties: Properties in C# are used to provide protection to the attributes of a class against direct reading or writing to it . By using properties we fix the rule that a particular attribute of the class can be read or written by means of property associated to that attribute.
These can be classified into 3 types:
Read only properties : We implement only the getter in the property implementation.
Syntax:
public String Name
{
get
{
return name;
}
}
Write only properties :We implement only the Setter in the property implementation.
Syntax:
public String Name
{
set{name=value;}
}
Auto implemented properties: This is the most commonly used type. we define the both getter and setter without any actual implementation.
Syntax:
Syntax:
public String Name
{
get
{
return name;
}
}
Write only properties :We implement only the Setter in the property implementation.
Syntax:
public String Name
{
set{name=value;}
}
Auto implemented properties: This is the most commonly used type. we define the both getter and setter without any actual implementation.
Syntax:
public String Name{get; set;}
Example: Accessing the Employee class with properties
Employee.cs
using System;
namespace lab1
{
public class Employee
{
private string name;
private long empid;
using System;
namespace lab1
{
public class Employee
{
private string name;
private long empid;
public Employee (String Name,long id){ name=Name;empid=id;}
public String Name
{
get
{
return name;
}
set{name=value;}
public String Name
{
get
{
return name;
}
set{name=value;}
}
public long Empid
{
get
{
return empid;
}
set{empid=value;}
public long Empid
{
get
{
return empid;
}
set{empid=value;}
}
public override string ToString ()
{
return (“Name:”+name+”\nEmployee ID:”+empid);
}
}
}
public override string ToString ()
{
return (“Name:”+name+”\nEmployee ID:”+empid);
}
}
}
Main.cs
using System;
using Gtk;
using Gtk;
namespace lab1
{
class MainClass
{
public static void Main (string[] args)
{
Employee E1=new Employee();
{
class MainClass
{
public static void Main (string[] args)
{
Employee E1=new Employee();
E1.Name=”kiran”;
E1.Empid=1234;
E1.Empid=1234;
Console.WriteLine(“Employee details\n {0}”, E1);
Console.WriteLine(“End of Employee details”,E1);
Console.WriteLine(“End of Employee details”,E1);
Console.ReadKey();
}
}
}
}
}
}
Sunday, 15 December 2013
wazzzzzzzzz up !
yahoooo 2014 approaching just half a month left !
any new year resolutions ...bucket lists??????
any new year resolutions ...bucket lists??????
Write a function for seat allocate and seat reserved.
public class SeatingPlan {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
boolean[][] seat = new boolean[10][20];
char[] alphabets=new char[]{'A','B','C','D','E','F','G','H','I','J'};
System.out.println("X = Seat occupied" + "\n\n" + " <<--------- SCREEN ---------- >>");
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 20; j++) {
String seating = (seat[i][j] == true) ? alphabets[i]+""+j+" [X] " : alphabets[i]+""+j+ " [0] ";
System.out.print(seating);
}
System.out.println("");
}
System.out.println("Please select a seat for your self");
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
String bookthisSeat=br1.readLine().toUpperCase();
char a=bookthisSeat.charAt(0);
int row=(int)a-65;
int col= Integer.parseInt(bookthisSeat.substring(1,2));
if (seat[row][col]==false)
{
seat[row][col]=true;
System.out.println("Congrats!! your ticket is booked and seat is now reserved for you");
}
else
System.out.println("sorry your ticket can not be booked this seat is already occupied.... Please fined another seat");
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
boolean[][] seat = new boolean[10][20];
char[] alphabets=new char[]{'A','B','C','D','E','F','G','H','I','J'};
System.out.println("X = Seat occupied" + "\n\n" + " <<--------- SCREEN ---------- >>");
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 20; j++) {
String seating = (seat[i][j] == true) ? alphabets[i]+""+j+" [X] " : alphabets[i]+""+j+ " [0] ";
System.out.print(seating);
}
System.out.println("");
}
System.out.println("Please select a seat for your self");
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
String bookthisSeat=br1.readLine().toUpperCase();
char a=bookthisSeat.charAt(0);
int row=(int)a-65;
int col= Integer.parseInt(bookthisSeat.substring(1,2));
if (seat[row][col]==false)
{
seat[row][col]=true;
System.out.println("Congrats!! your ticket is booked and seat is now reserved for you");
}
else
System.out.println("sorry your ticket can not be booked this seat is already occupied.... Please fined another seat");
}
}
Program a password validator which allows a user to set a password when it has a certain pattern(say has alphabhets, an uppercase, a lower case, a number, a special character)
public class Rotator {
void reverse(int a[], int n) {
for (int i = 0; i <= n - 1; i++) {
int temp;
temp = a[i];
a[i] = a[n - 1];
a[n - 1] = temp;
n--;
}
printArray(a);
}
void printArray(int a[]) {
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
}
public static void main(String[] args) throws IOException {
// TODO code application logic here
System.out.println("Enter the password");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String Password = br.readLine();
if (Password.length() >= 5 & Password.length() <= 12) {
for (int i = 0; i < Password.length(); i++) {
if (Character.isLetter(Password.charAt(i))) {
if (Character.isLowerCase(Password.charAt(i))) {
if (Character.isDigit(Password.charAt(i))) {
if (!(Password.charAt(i) == Password.charAt(i + 1))) {
if (!Character.isUpperCase(Password.charAt(i)) && !Character.isSpaceChar(Password.charAt(i))) {
System.out.println("Your password is valid");
}
else {
System.out.println("contains upper case or special character");
}
}
else {
System.out.println("contains patterns");
}
}
else {
System.out.println("Does not contains digit");
}
}
else {
System.out.println("Does not contains Lower Case Char");
}
}
}
}
else {
System.out.println("Invalid assword length ... plz try again with another password of length between 5-12");
}
}
}
void reverse(int a[], int n) {
for (int i = 0; i <= n - 1; i++) {
int temp;
temp = a[i];
a[i] = a[n - 1];
a[n - 1] = temp;
n--;
}
printArray(a);
}
void printArray(int a[]) {
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
}
public static void main(String[] args) throws IOException {
// TODO code application logic here
System.out.println("Enter the password");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String Password = br.readLine();
if (Password.length() >= 5 & Password.length() <= 12) {
for (int i = 0; i < Password.length(); i++) {
if (Character.isLetter(Password.charAt(i))) {
if (Character.isLowerCase(Password.charAt(i))) {
if (Character.isDigit(Password.charAt(i))) {
if (!(Password.charAt(i) == Password.charAt(i + 1))) {
if (!Character.isUpperCase(Password.charAt(i)) && !Character.isSpaceChar(Password.charAt(i))) {
System.out.println("Your password is valid");
}
else {
System.out.println("contains upper case or special character");
}
}
else {
System.out.println("contains patterns");
}
}
else {
System.out.println("Does not contains digit");
}
}
else {
System.out.println("Does not contains Lower Case Char");
}
}
}
}
else {
System.out.println("Invalid assword length ... plz try again with another password of length between 5-12");
}
}
}
Separate the negative and the positive integers from an array of numbers
public class SeparatePosNeg {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int array[] = new int[]{2, -7, -6, -9, -5, 11, -19, 8, 14, 17};
int neg,pos=0, temp;
int n = array.length;
for (int i = 0; i < n; i++)
{
if (array[i] > 0)
{
// System.out.println("--)))>"+i);
temp=array[i];
array[pos]=temp;
//System.out.println("-ppp->"+pos);
pos++;
//System.out.println("-->"+pos);
for(int k =i ; k >pos ;k--)
{
// System.out.println("-->"+i);
array[k]=array[k-1];
}
}
}
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int array[] = new int[]{2, -7, -6, -9, -5, 11, -19, 8, 14, 17};
int neg,pos=0, temp;
int n = array.length;
for (int i = 0; i < n; i++)
{
if (array[i] > 0)
{
// System.out.println("--)))>"+i);
temp=array[i];
array[pos]=temp;
//System.out.println("-ppp->"+pos);
pos++;
//System.out.println("-->"+pos);
for(int k =i ; k >pos ;k--)
{
// System.out.println("-->"+i);
array[k]=array[k-1];
}
}
}
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
}
Get the count of each vowel in a string
public class JavaApplication7 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
System.out.println("Please enter some text");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String names=br.readLine().toLowerCase();
StringTokenizer stk=new StringTokenizer(names);
String vowels="";
while(stk.hasMoreTokens()){
String c=stk.nextToken();
if(c.startsWith("a")||c.startsWith("e")||c.startsWith("i")||c.startsWith("o")||c.startsWith("u"))
vowels=vowels+" "+c;
}
System.out.println(vowels);
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
System.out.println("Please enter some text");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String names=br.readLine().toLowerCase();
StringTokenizer stk=new StringTokenizer(names);
String vowels="";
while(stk.hasMoreTokens()){
String c=stk.nextToken();
if(c.startsWith("a")||c.startsWith("e")||c.startsWith("i")||c.startsWith("o")||c.startsWith("u"))
vowels=vowels+" "+c;
}
System.out.println(vowels);
}
}
Get character count of each character in a string
public class CharInArray {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
int current=0,highest=0;
char highest_occuring_char=' ';
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String InputString=br.readLine();
String alphabet="abcdefghijklmnopqrstuvwxyz";
for(int i=0;i<26;i++){
for(int j=0;j<InputString.length();j++){
if(InputString.charAt(j)==alphabet.charAt(i))
current++;
}
if(current>highest){
highest=current;
highest_occuring_char=alphabet.charAt(i);
}
current=0;
}
System.out.println("The Highest occuring character is "+ highest_occuring_char);
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
int current=0,highest=0;
char highest_occuring_char=' ';
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String InputString=br.readLine();
String alphabet="abcdefghijklmnopqrstuvwxyz";
for(int i=0;i<26;i++){
for(int j=0;j<InputString.length();j++){
if(InputString.charAt(j)==alphabet.charAt(i))
current++;
}
if(current>highest){
highest=current;
highest_occuring_char=alphabet.charAt(i);
}
current=0;
}
System.out.println("The Highest occuring character is "+ highest_occuring_char);
}
}
Print in lojban
public class Lojban {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
String[] lojban={"no","pa","re","ci","vo","mk","xa","ze","bi","so"};
System.out.println("Enter a lojban number not less than or equal to 1,000,000");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String lojan=br.readLine();
int num;
String number;
for(int i=0;i<lojan.length();i=i+2){
number=lojan.substring(i, i+2);
// System.out.println(number);
for(int j=0;j<lojban.length;j++) {
if(lojban[j].equals(number))
{
System.out.println(j);
}
}
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
String[] lojban={"no","pa","re","ci","vo","mk","xa","ze","bi","so"};
System.out.println("Enter a lojban number not less than or equal to 1,000,000");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String lojan=br.readLine();
int num;
String number;
for(int i=0;i<lojan.length();i=i+2){
number=lojan.substring(i, i+2);
// System.out.println(number);
for(int j=0;j<lojban.length;j++) {
if(lojban[j].equals(number))
{
System.out.println(j);
}
}
}
}
}
Get the first non repeating alphabet from the given string by the user
public class FirstNonReapeating {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
String str = "rrrrrraaaaavvvvvi";
int[] count = new int[128];
char[] charArr = str.toLowerCase().toCharArray();
for (char c : charArr) {
count[c]++;
}
for (char c : charArr) {
if (count[c] == 1) {
System.out.println("First Non repeated character is : " + c);
break;
}
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
String str = "rrrrrraaaaavvvvvi";
int[] count = new int[128];
char[] charArr = str.toLowerCase().toCharArray();
for (char c : charArr) {
count[c]++;
}
for (char c : charArr) {
if (count[c] == 1) {
System.out.println("First Non repeated character is : " + c);
break;
}
}
}
}
Find out the combination of an element of each array gives a result 0. For example: array 1: {2,1,4,7} array 1: {3,-3,-8,0} array 1: {-1,-4,-7,6}
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int a1[] = new int[]{2, -3, 7, 8};
int a2[] = new int[]{4, -6, 2, -1};
int a3[] = new int[]{-6, 4, 7, 0};
sort(a1);
sort(a2);
sort(a3);
int sum;
for(int i=0;i<a1.length;i++){
int j=0,k=a3.length-1;
while(j<a2.length && k>0){
sum=a1[i]+a2[j]+a3[k];
if(sum>0)
--k;
else if(sum<0)
++j;
else if(sum==0)
System.out.println(a1[i]+"+"+a2[j]+"+"+a3[k]+"=0");
}
}
}
public static void sort(int[] a) {
int j;
boolean flag = true;
int temp;
while (flag) {
flag = false;
for (int i = 0; i < a.length - 1; i++) {
if(a[i]>a[i+1]){
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
flag = true;
}
}
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int a1[] = new int[]{2, -3, 7, 8};
int a2[] = new int[]{4, -6, 2, -1};
int a3[] = new int[]{-6, 4, 7, 0};
sort(a1);
sort(a2);
sort(a3);
int sum;
for(int i=0;i<a1.length;i++){
int j=0,k=a3.length-1;
while(j<a2.length && k>0){
sum=a1[i]+a2[j]+a3[k];
if(sum>0)
--k;
else if(sum<0)
++j;
else if(sum==0)
System.out.println(a1[i]+"+"+a2[j]+"+"+a3[k]+"=0");
}
}
}
public static void sort(int[] a) {
int j;
boolean flag = true;
int temp;
while (flag) {
flag = false;
for (int i = 0; i < a.length - 1; i++) {
if(a[i]>a[i+1]){
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
flag = true;
}
}
}
}
}
Copy file without comments
void RemoveComments(String inputFilePath, String outputFilePath) throws FileNotFoundException, IOException {
File in = new File(inputFilePath);
File out = new File(outputFilePath);
BufferedReader bufferedreader = new BufferedReader(new FileReader(in));
PrintWriter pw = new PrintWriter(new FileWriter(out));
String line = null, lineToRemove = null;
while ((line = bufferedreader.readLine()) != null) {
if (line.startsWith("/*") && line.endsWith("*/")) {
lineToRemove = line;
}
if (!line.trim().equals(lineToRemove)) {
pw.println(line);
pw.flush();
}
}
}
}
File in = new File(inputFilePath);
File out = new File(outputFilePath);
BufferedReader bufferedreader = new BufferedReader(new FileReader(in));
PrintWriter pw = new PrintWriter(new FileWriter(out));
String line = null, lineToRemove = null;
while ((line = bufferedreader.readLine()) != null) {
if (line.startsWith("/*") && line.endsWith("*/")) {
lineToRemove = line;
}
if (!line.trim().equals(lineToRemove)) {
pw.println(line);
pw.flush();
}
}
}
}
Friday, 1 February 2013
Java beginner : Print a number pyramid
the format of the pyramid should be like this:
1
123
12345
1234567
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package printpyramid;
/**
*
* @author Sunshine
*/
public class PrintPyramid {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int n=8;
for(int i=1;i<n;i=i+2){
for(int j=1;j<i+1;j++){
// System.out.println("");
System.out.println(j);
}
System.out.println(" ");
}
}
}
1
123
12345
1234567
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package printpyramid;
/**
*
* @author Sunshine
*/
public class PrintPyramid {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int n=8;
for(int i=1;i<n;i=i+2){
for(int j=1;j<i+1;j++){
// System.out.println("");
System.out.println(j);
}
System.out.println(" ");
}
}
}
Java Interview : Insert 1-100 numbers in an array of 99 cell and find out the missing number
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package arrayof99cells;
import java.io.IOException;
/**
*
* @author Sunshine
*/
public class Arrayof99cells {
int FindMinssing(int[] array){
int sum=0;
int sum_of_n_natural_nums=((array.length+2)*(array.length+1))/2;
System.out.println(array.length);
for(int i=0;i<array.length;i++){
sum_of_n_natural_nums-=array[i];
}
return (sum_of_n_natural_nums);
}
public static void main(String[] args) throws IOException {
// TODO code application logic here
int[] arrayof99cells=new int[]{1,2,3,5};
Arrayof99cells a=new Arrayof99cells();
int missing_number=a.FindMinssing(arrayof99cells);
System.out.println(missing_number);
}
}
Subscribe to:
Posts (Atom)