Skip to content

Instantly share code, notes, and snippets.

@ErFaanHussain
Created September 17, 2015 15:29
Show Gist options
  • Save ErFaanHussain/a8b2aa88af78cb7f5747 to your computer and use it in GitHub Desktop.
Save ErFaanHussain/a8b2aa88af78cb7f5747 to your computer and use it in GitHub Desktop.
JAVA Assignment Programs
/**Create a method ArrayAddition(arr) that takes the array of numbers stored in arr and return the true if any
combination of numbers in the array can be added up to equal the largest number in the array otherwise return false.
The array may contain negative numbers.
**Question 3 *****
***@author ErFaan Hussain***
**/
package Question3;
import java.util.Scanner;
import java.util.Arrays;
class ArrayAddTest
{ private int[] takeArray()
{
Scanner scArr=new Scanner(System.in);
System.out.print("Enter length of Array:\t");
int len=scArr.nextInt();
int[] arr=new int[len];
System.out.print("Enter the elements of the Array:\t");
for(int i=0;i<len;i++)
{
arr[i]=scArr.nextInt();
}
return arr;
}
private boolean arrayAddition(int[] arr)
{
//Sort the Array to determine the largest element in it
Arrays.sort(arr);
int greatest=arr[arr.length-1];
int sum=0;
int i=0;
boolean status=false;
do
{ sum=sum+arr[i];
if(sum==greatest)
{
status=true;
}
else
{
i++;
}
} while(i<arr.length-1);
return status;
}
public static void main(String[] ar)
{
ArrayAddTest arrObj=new ArrayAddTest();
int[] arr=arrObj.takeArray();
boolean st=arrObj.arrayAddition(arr);
System.out.println("Result is:"+st);
}
}
/**Write a method MeanMode(arr,mode) that takes the array of numbers arr and another integer mode and returns 1
if the mode equals the mean of the numbers in the array, if they don’t equal each other the method should return 0.
***@author ErFaan Hussain***
***Question 5***
**/
package Question5;
import java.util.Scanner;
import java.util.Arrays;
class ArrayMeanMode
{
public int[] takeArray()
{
Scanner scArr=new Scanner(System.in);
System.out.print("Enter length of Array:\t");
int len=scArr.nextInt();
int[] arr=new int[len];
System.out.print("Enter the elements of the Array:\t");
for(int i=0;i<len;i++)
{
arr[i]=scArr.nextInt();
}
return arr;
}
public int takeMode(int[] arr)
{
System.out.print("Entered Array is:");
for(int el:arr)
{
System.out.print(el);
System.out.print(",");
}
Scanner in=new Scanner(System.in);
System.out.println(" ");
System.out.println("Enter mode of the Array");
int mode=in.nextInt();
return mode;
}
public short meanMode(int[] ar,int mode)
{
float mean=0;
float sum=0;
for(int ele:ar)
{
sum+=ele;
}
mean=sum/ar.length;
System.out.println("Mean of Array is:"+mean);
if(mean==mode) return 1;
else return 0;
}
public static void main(String[] args)
{
ArrayMeanMode arrMM=new ArrayMeanMode();
int[] arr=arrMM.takeArray();
int mode=arrMM.takeMode(arr);
short res=arrMM.meanMode(arr,mode);
System.out.println("Result is:"+res);
}
}
/***Create a method DashInsert(str) that inserts dashes(-) between each two odd numbers
and astreisks(*) between two even numbers in str.***
***Question 15***
***@author ErFaan Hussain***/
package Question15;
import java.util.Scanner;
class DashInsert
{
public String dashInsert(String str)
{
char[] charAr=str.toCharArray();
String result="";
for(int i=1;i<charAr.length;i++)
{
int value1=Character.getNumericValue(charAr[i-1]);
int value2=Character.getNumericValue(charAr[i]);
result+=value1;
if(value1%2!=0&&value2%2!=0)
{
result+="-";
}
else if(value1%2==0&&value2%2==0)
{
result+="*";
}
}
result+=charAr[charAr.length-1];
return result;
}
public String takeString()
{
Scanner sin=new Scanner(System.in);
System.out.println("Enter a String:");
String str=sin.nextLine();
return str;
}
public static void main (String[] args)
{
DashInsert dIns= new DashInsert();
String str=dIns.takeString();
System.out.println("Result is:"+dIns.dashInsert(str));
}
}
/***DistinctList() that takes array of numbers and determines no. of Duplicate entries in it. ***
***Question 14***
***@author ErFaan Hussain***
***/
package Question14;
import java.util.Scanner;
import java.util.Arrays;
class DistinctList
{
public int distinctList(int[] arr)
{
Arrays.sort(arr);
int prvElement=arr[0];
int dupCount=0;
int nxtElement=0;
for(int i=1;i<arr.length;i++)
{
nxtElement=arr[i];
if(prvElement==nxtElement)
{
dupCount+=1;
}
prvElement=nxtElement;
}
return dupCount;
}
public int[] getArray()
{
Scanner scArr=new Scanner(System.in);
System.out.print("Enter length of Array:\t");
int len=scArr.nextInt();
int[] arr=new int[len];
System.out.println("Enter the elements of the Array:\t");
for(int i=0;i<len;i++)
{
arr[i]=scArr.nextInt();
}
return arr;
}
public static void main(String[] args)
{
DistinctList dList=new DistinctList();
int[] num=dList.getArray();
System.out.println("Duplicate Count is:"+dList.distinctList(num));
}
}
/***Method which implements an abstract method divisorSum(int n) from an interface AdvancedArithmatic
and finds sum of divisors of an inputted number
***Question 7***
***@author ErFaan Hussain***
***/
package Question7;
import java.util.Scanner;
interface AdvancedArithmetic
{
public abstract int divisorSum(int n);
}
class MyCalculator implements AdvancedArithmetic
{
public int divisorSum(int n)
{
int resSum=0;
System.out.print("Divisors of "+n+" are:\t");
for(int i=1;i<=n;i++)
{
if(n%i==0)
{
resSum+=i;
System.out.print(i);
System.out.print(" ");
}
}
System.out.println(" ");
return resSum;
}
public int takeInput()
{
Scanner in=new Scanner(System.in);
System.out.print("Enter any number less than 1000:\t");
int num=in.nextInt();
if(num>1000)
{
System.out.println("\n!!!You entered number greater than 1000!!!");
System.out.println("Program Terminated, Run again");
System.exit(0);
}
return num;
}
public static void main(String[] args)
{
MyCalculator cal=new MyCalculator();
int num=cal.takeInput();
int result=cal.divisorSum(num);
System.out.println("Sum of Divisors of "+num+" is:"+result);
}
}
/***method FibonacciChecker returns 'yes' if inputted number is part of the fibonacci sequenceotherwise it returns 'no'***
**Question 11***
**@author ErFaan Hussain***
**/
package Question11;
import java.util.Scanner;
class FibonacciChecker
{
public String fibonacciChecker(int num)
{
int a=0,b=1,c=0;
while(c<num)
{
c=a+b;
a=b;
b=c;
}
if(c==num) return "Yes";
else return "No";
}
public int takeInput()
{
Scanner scn=new Scanner(System.in);
System.out.println("Enter a positive number:");
int n=scn.nextInt();
if(n<=0)
{
System.out.println("!!!You didn't entered a positive number!!!");
System.out.println("Program will exit, run again");
System.exit(0);
}
return n;
}
public static void main (String[] args)
{
FibonacciChecker fCheck=new FibonacciChecker();
int num=fCheck.takeInput();
System.out.println("Result is:"+fCheck.fibonacciChecker(num));
}
}
/***Question 8***
***@author ErFaan Hussain***
**Extending subclasses Student & Teacher from Person SuperClass and extending CollegeStudent subclass from Student
Initializing members through Constructors and Printing members through showCStd() & showTeacher() methods ***/
package Question8;
class Person
{
String name;
String address;
int age;
String contact;
String email;
String gender;
}
class Student extends Person
{
String clas;
String regNo;
int rollNo;
String dateOfAdmission;
}
class Teacher extends Person
{
double salary;
String subject;
Teacher(String nameIn,String addressIn,int ageIn,String contactIn,String emailIn,
String genderIn,double salaryIn,String subjectIn)
{
name=nameIn;
address=addressIn;
age=ageIn;
contact=contactIn;
email=emailIn;
gender=genderIn;
salary=salaryIn;
subject=subjectIn;
}
public void showTeacher(Teacher t1)
{
System.out.println("Details of Teacher are:");
System.out.println("Name:"+t1.name);
System.out.println("Address:"+t1.address);
System.out.println("Age:"+t1.age);
System.out.println("Gender:"+t1.gender);
System.out.println("Contact Number:"+t1.contact);
System.out.println("Email:"+t1.email);
System.out.println("Salary:"+t1.salary);
System.out.println("Subject:"+t1.subject);
}
}
class CollegeStudent extends Student
{
int currentYear;
String major;
CollegeStudent(String nameIn,String addIn,int ageIn,String contactIn,String emailIn,
String genderIn,String classIn,String regNoIn,int rNoIn,String dOfAdmIn,
int cYearIn,String majorIn)
{
name=nameIn;
address=addIn;
age=ageIn;
contact=contactIn;
email=emailIn;
gender=genderIn;
clas=classIn;
regNo=regNoIn;
rollNo=rNoIn;
dateOfAdmission=dOfAdmIn;
currentYear=cYearIn;
major=majorIn;
}
public void showCStd(CollegeStudent st1)
{
System.out.println(" ");
System.out.println("Details of Student are:");
System.out.println("Name:"+st1.name);
System.out.println("Address:"+st1.address);
System.out.println("Age:"+st1.age);
System.out.println("Gender:"+st1.gender);
System.out.println("Class:"+st1.clas);
System.out.println("Registration Number:"+st1.regNo);
System.out.println("Roll Number:"+st1.rollNo);
System.out.println("Date of Admission:"+st1.dateOfAdmission);
System.out.println("Current Year:"+st1.currentYear);
System.out.println("Major:"+st1.major);
System.out.println("Contact Number:"+st1.contact);
System.out.println("Email:"+st1.email);
}
}
class MainClass
{
public static void main(String[] args)
{
CollegeStudent st1=new CollegeStudent("Erfaan Hussain","Awantipora",20,"9858888875","erfaan06@hotmail.com"
,"male","Grad","CS/2015/522",48,"01-April-2014",2,"Computer Applications");
Teacher t1=new Teacher("Nazir Ahmed","Awantipora Pulwama",30,"9055010101",
"nazirahmad@gmail.com","Male",30000.00,"Computer Science");
t1.showTeacher(t1);
st1.showCStd(st1);
}
}
/***Our application involves objects that can move. Define an interface called movable,
containing the signatures of various movement methods. So define the following abstract methods to be implemented
by the subclass: moveUp(),moveDown(),moveLeft(),moveRight().
Derive a subclass called MovablePoint provide implementation to all the abstract methods declared in the interface.***
***Question 9***
***@author ErFaan Hussain***
***/
package Question9;
interface Movable
{
public abstract void moveUp();
public abstract void moveDown();
public abstract void moveLeft();
public abstract void moveRight();
}
class MovablePoint implements Movable
{
public static void main(String[] args)
{
MovablePoint point=new MovablePoint();
point.moveUp();
point.moveDown();
point.moveLeft();
point.moveRight();
}
public void moveUp()
{
System.out.println("Point moved Up");
}
public void moveDown()
{
System.out.println("Point moved Down");
}
public void moveLeft()
{
System.out.println("Point moved Left");
}
public void moveRight()
{
System.out.println("Point moved Right");
}
}
/**
@author ErFaan Hussain
****Create a method LetterChanges(str) that takes the str parameter being passed and modify it using the following algorithm:
Replace every letter in the string with the letter following it in the alphabet.Then capitalize every vowel in the new string and
finaly return this modified string.
**/
package Question1;
import java.util.Scanner;
import java.lang.*;
class letterMod
{
public String takeInput()
{
Scanner scanStr=new Scanner(System.in);
System.out.println("Enter a String");
String inputString=scanStr.nextLine();
return inputString;
}
public String letterChanges(String str)
{
/***Taking ASCII equivalent of each character in string and
incrementing it. then turning ASCII value back to character value
and storing it back in a new String "outputString" ***/
String outputString=new String();
for(int i=0;i<str.length();i++)
{
char ch1=str.charAt(i);
int asciiVal=(int) ch1;
if(asciiVal==122)
{
asciiVal=97;
}
else if(asciiVal==32)
{
asciiVal=32;
}
else
{
asciiVal++;
}
char ch2=(char) asciiVal;
outputString=outputString.concat(Character.toString(ch2));
}
System.out.println("Place incremented String is:\t\t"+outputString);
/***Turning vowels into UpperCase in "outputString"
and putting them in final String "finOutputString"
which will be returned***/
String finOutputStr=new String();
for(int j=0;j<outputString.length();j++)
{
char ch3=outputString.charAt(j);
if (ch3=='a'||ch3=='e'||ch3=='i'||ch3=='o'||ch3=='u')
{
char ch4=Character.toUpperCase(ch3);
finOutputStr=finOutputStr.concat(Character.toString(ch4));
}
else
{
finOutputStr=finOutputStr.concat(Character.toString(ch3));
}
}
return finOutputStr;
}
public void showString(String strFinal)
{
System.out.println("Final String is(Vowels in UpperCase):\t"+strFinal);
}
public static void main(String[] ars)
{
letterMod leterObj=new letterMod();
String inputString=leterObj.takeInput();
String outString=leterObj.letterChanges(inputString);
leterObj.showString(outString);
}
}
/**method NumberSearch(str) that takes a String & searches for all the numbers in the string, then adds them together,
then return that final number divided by the total amount of letters in the String.
***Question 10***
***@author ErFaan Hussain***
***Method NumberSearch that takes a String & searches for all the numbers in it***
***/
package Question10;
import java.util.Scanner;
class NumberSearch
{
public int numberSearch(String str)
{
char[] charAr=str.toCharArray();
int numberCount=0;
int sum=0;
String resNumbers=new String();
for(Character ch:charAr)
{
if(Character.isDigit(ch))
{
numberCount+=1;
String ch1=Character.toString(ch);
sum +=ch;
resNumbers=resNumbers.concat(" ");
resNumbers=resNumbers.concat(ch1);
}
}
System.out.println("There are "+numberCount+" numbers in the String");
System.out.println("The numbers in the string are:"+resNumbers);
return (sum/numberCount);
}
public String takeString()
{
Scanner sin=new Scanner(System.in);
System.out.println("Enter a String:");
String str=sin.nextLine();
return str;
}
public static void main (String[] args)
{
NumberSearch nSearch = new NumberSearch();
String str=nSearch.takeString();
int result=nSearch.numberSearch(str);
System.out.println("result is:\t"+result);
}
}
/**Create a method SecondGreatLow(arr) take the array of numbers and return the second largest and second lowest numbers,
separated by a space. The array will not be empty and will contain at least 2 numbers.
**@author ErFaan Hussain**
**Question 6**
**/
package Question6;
import java.util.Scanner;
import java.util.Arrays;
class SecondLarLow
{
public int[] takeArray()
{
Scanner scArr=new Scanner(System.in);
System.out.print("Enter length of Array:\t");
int len=scArr.nextInt();
int[] arr=new int[len];
System.out.print("Enter the elements of the Array:\t");
for(int i=0;i<len;i++)
{
arr[i]=scArr.nextInt();
}
return arr;
}
public static int[] removeDuplicates(int[] arr)
{
Arrays.sort(arr);
int[] resArray=new int[arr.length];
int prvElement=arr[0];
resArray[0]=prvElement;
int uniqueCount=1;
int nxtElement=0;
for(int i=1;i<arr.length;i++)
{
nxtElement=arr[i];
if(prvElement!=nxtElement)
{
resArray[i]=nxtElement;
uniqueCount=uniqueCount+1;
}
prvElement=nxtElement;
}
int[] newResArray=new int[uniqueCount];
int i=0;
int j=0;
do
{
if(resArray[j]!=0)
{
newResArray[i]=resArray[j];
i=i+1;
j=j+1;
}
else
{
j=j+1;
}
}
while(j<resArray.length&&i<=uniqueCount);
// System.out.println("Unique element count is:"+uniqueCount);
return newResArray;
}
public String secondGreatLow(int[] arr)
{ int[] arrNew=removeDuplicates(arr);
int secLowest=0;
int secGreatest=0;
if(arrNew.length>2)
{
secLowest=arrNew[1]; //second Lowest element
secGreatest=arrNew[arrNew.length-2]; //second Greatest element
}
else if(arrNew.length==2)
{
secLowest=arrNew[arrNew.length-1];
secGreatest=arrNew[arrNew.length-2];
}
String result=Integer.toString(secLowest);
String res2=Integer.toString(secGreatest);
result=result.concat(" ");
result=result.concat(res2);
return result;
}
public static void main(String[] args)
{ SecondLarLow sllObj=new SecondLarLow();
int[] arr=sllObj.takeArray();
String result=sllObj.secondGreatLow(arr);
System.out.println("Second Lowest & Second Largest elements of entered Array are:\t"+result);
}
}
/**Create a method SimpleAdding(num) add up all the numbers from 1 to num.
**Question 2***
***@author ErFaan Hussain***
***/
package Question2;
import java.util.Scanner;
class SimAdd
{
public void simpleAdding(int num)
{
int res=0;
for(int i=1;i<num;i++)
{
res=i+res;
}
System.out.println("Addition of numbers from 1 to "+num+" is:\t"+res);
}
public int takeInput()
{ System.out.println("Enter an Integer upto which you want Addition from 1");
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
return num;
}
public static void main(String[] ar)
{ SimAdd numAdd=new SimAdd();
int num=numAdd.takeInput();
numAdd.simpleAdding(num);
}
}
@WaseemHilal
Copy link

jammu trained java master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment