mohammedabdulbari / java-se Goto Github PK
View Code? Open in Web Editor NEWJava SE Student Challenges
Java SE Student Challenges
import java.lang.*;
class Rectangle
{
private double length;
private double breadth;
public Rectangle()
{
length=1;
breadth=1;
}
public Rectangle(double l,double b)
{
setLength(l);
setBreadth(b);
}
public Rectangle(double s)
{
length=breadth=s;
}
public void setLength(double l)
{
if(l>0)
length=l;
else
length=0;
}
public void setBreadth(double b)
{
if(b>0)
breadth=b;
else
breadth=0;
}
public double Area()
{
return length*breadth;
}
public double perimeter()
{
return 2*(length+breadth);
}
public double circumference()
{
return perimeter();
}
public boolean isSquare()
{
if(length==breadth)
return true;
else
return false;
}
}
class DataHidingConstructors
{
public static void main(String argms[])
{
Rectangle r1=new Rectangle(-1,-2);
System.out.println(r1.Area());
System.out.println(r1.perimeter());
System.out.println(r1.circumference());
System.out.println(r1.isSquare());
}
}
Sir, In the Interthread communication problem for one producer and multiple consumer there is a problem in the code. I have fixed the problem please review
class WhiteBoard {
boolean flag[] = new boolean[10];
String text;
int numberOfStudents = 0;
int count = 0;
public WhiteBoard() {
initializeFlag();
}
public void initializeFlag() {
for (int i = 0; i < 10; i++) {
flag[i] = false;
}
}
synchronized public void changeFlag(Student s) {
String name = s.name;
int idx = Integer.parseInt(name.substring(0, 1)) - 1; // Adjust index
flag[idx] = true;
}
synchronized public boolean checkFlag(Student s) {
String name = s.name;
int idx = Integer.parseInt(name.substring(0, 1)) - 1; // Adjust index
return flag[idx];
}
public void attendance() {
numberOfStudents++;
}
synchronized public void write(String t) {
System.out.println("Teacher is Writing " + t);
while (count != 0)
try {
wait();
} catch (Exception e) {
}
text = t;
initializeFlag();
count = numberOfStudents;
notifyAll();
}
synchronized public String read() {
while (count == 0)
try {
wait();
} catch (Exception e) {
}
String t = text;
count--;
if (count == 0) {
notify();
}
return t;
}
}
class Teacher extends Thread {
WhiteBoard wb;
String notes[] = { "Java is language", "It is OOPs", "It is Platform Independent", "It supports Thread", "end" };
public Teacher(WhiteBoard w) {
wb = w;
}
public void run() {
for (int i = 0; i < notes.length; i++)
wb.write(notes[i]);
}
}
class Student extends Thread {
String name;
WhiteBoard wb;
public Student(String n, WhiteBoard w) {
name = n;
wb = w;
}
public void run() {
String text = "";
wb.attendance();
do {
if (!wb.checkFlag(this)) {
text = wb.read();
wb.changeFlag(this);
System.out.println(name + " Reading " + text);
System.out.flush();
}
} while (!text.equals("end"));
}
}
public class Main {
public static void main(String[] args) {
WhiteBoard wb = new WhiteBoard();
Teacher t = new Teacher(wb);
Student s1 = new Student("1. John", wb);
Student s2 = new Student("2. Ajay", wb);
Student s3 = new Student("3. Kloob", wb);
Student s4 = new Student("4. Smith", wb);
t.start();
s1.start();
s2.start();
s3.start();
s4.start();
}
}
Why do we need to differentiate by writing f and d in the end of decimal number, if we had already declared datatypes float/double in the beginning??
like for
float values lies between 3.4028235E38
so decimal value greater than float limit can directly be written in double like this
double x=3.4028235E39 , why do we have to write f and d , if it can easily been differentiate by there limits.
Please help me with my issue.
Line 29 in b590bad
If we enter 4 then it will not work , So it should be :
for(int i=2;i<=n/2;i++)
One more condition at the top if the num ==1 then its not a prime.
In for loop of reversing array there should be i-- not i++.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.